// ==============
// = NAVIGATION =
// ==============
nav = {
	init : function(){
		$('#nav ul#sections li ul').hide();
		$("#nav ul#sections>li:has(ul)").click(function () {
			if (window.console) console.log($(this));
			$('#nav ul#sections li ul').slideUp('slow');
			if($(">ul", $(this)).is(":hidden")) {
				$('>ul', $(this)).slideDown("slow");
			}else {
				$('>ul', $(this)).slideUp("slow");
			}
			return false;
		});
		$('#nav ul#sections>li.active>ul').show();
		$("#nav ul#sections>li>ul>li>a").click(function(){
			window.location.href = $(this).attr('href');
			return false; // prevents the event bubbling
		});
	}
}

// ==============
// = HOME BOXES =
// ==============
homeboxes = {
	init : function(){
		var self = this;
		// $('ul.categorie li a.linkto div').hide();
		$('ul.categorie li a.linkto').each(function(){
			$('div', $(this)).hide();
			$span = $('<span class="white_arrow"></span>');
			$(this).append($span);
			$span.hide();
		});
		$('ul.categorie li a.linkto').hover(function(){
			self.show($(this));
		},function(){
			self.hide($(this));
		});
	},
	show : function(caller){
		$('div', caller).show();
		caller.stop().animate({backgroundColor:'#e03427'});
		$('span.white_arrow', caller).fadeIn();
	},
	hide : function(caller){
		$('div', caller).hide();
		caller.stop().animate({backgroundColor:'#2d2d2d'});
		$('span.white_arrow', caller).fadeOut();
	}
}

// ==================
// = BOOKMARKS PAGE =
// ==================
bookmark = {
	init : function(get_url){
		$.ajax({
			url: get_url,
			success: function(data){
				if (data) {
					// $('#navbookmarks span').html(' ('+data+')');
				};
			}
		});
	}
}

url = {};
// ===================
// = PRINT THIS PAGE =
// ===================
printable = {
	uri : '',
	init : function(){
		var self = this;
		$('a#print').click(function(){
			//			http://192.168.2.10:8888/juju/maisonc/en/our_stores#?view=print
			parseUri.options.strictMode = true;
			url = parseUri(window.location.href);
			resultUrl = url.protocol+'://'+url.authority+url.path+'?view=print';
			// if (window.console) console.log(uriObj.);
			window.open(resultUrl, "print","location=1,status=1,scrollbars=1,width=700,height=300");
			// if (window.console) console.log(window.location.href + '?view=print');
			return false;
		});
	},
	print : function(){
		window.print();
	}
}

// ===========
// = PRODUCT =
// ===========
product = {
	init : function(get_url, add_url){
		var self = this;
		bookmark.init(get_url);
		productId = $('#product_id').attr('value');
		$('#addtobookmarks').click(function(){
			$.ajax({
				type: 'POST',
				url: add_url,
				data: 'product_id='+productId,
				success: function(result) {
					// if (window.console) console.log(result);
					if (result != 0) {
						self.animate(result);
					};
				}
			});
			return false;
		});
		// adjust :
		self.adjustToWidth();
	},
	animate : function(number_of_elements){
		$bookmarkLink = $('#navbookmarks');
		$('span', $bookmarkLink).html(' ('+number_of_elements+')');
	},
	adjustToWidth : function(){
		$product = $('#produit');
		$product.css({
			width: $('img', $product).width()+25
		});
	}
}

// ================
// = PRODUCT LIST =
// ================
productList = {
	init : function(){
		var self = this;
		$('ul.categorie li').each(function(){
			$span = $('<span class="white_arrow"></span>');
			$('a', $(this)).hover(function(){
				self.show($(this));
			},function(){
				self.hide($(this));
			}).append($span);
			$span.hide();
		});
	},
	show : function(caller){
		caller.stop().animate({backgroundColor:'#e03427'});
		$('span.white_arrow', caller).fadeIn();
	},
	hide : function(caller){
		caller.stop().animate({backgroundColor:'#2d2d2d'});
		$('span.white_arrow', caller).fadeOut();
	}
}

// =========
// = SHOPS =
// =========
shop = {
	init : function(open_text,close_text){
		var self = this;
		$('div.top:eq(0) p.voirlacarte a').html(close_text);
		$('#map2,#map3').hide().parent('div.divmap').addClass('nopadding');
		$('div.top p.voirlacarte a').click(function(){
			$caller = $(this);
			$map = $caller.closest('div.top').next('.map_conteneur');
			if ($map.is(':hidden')) {
				$(this).parent('.divmap').addClass('nopadding');
				$map.slideDown('slow',function(){
					$(this).parent('.divmap').removeClass('nopadding');					
					$caller.html(close_text);					
				});
			} else {
				$(this).parent('.divmap').removeClass('nopadding');
				$map.slideUp('slow', function(){
					$(this).parent('.divmap').addClass('nopadding');										
					$caller.html(open_text);					
				});
			};
			return false;
		});
	}
}

// ============
// = CATALOGS =
// ============
catalogs = {
	init : function(){
		var self = this;
		$('#telechargement li a:not(.inactive)').hover(function(){
			self.haut($(this));
		}, function(){
			self.out($(this));
		});
	},
	haut : function(caller){
		caller.stop().animate({backgroundColor:'#000'});
	},
	out : function(caller){
		caller.stop().animate({backgroundColor:'#3c3c3c'});
	}
}

/*
	Initialize a Google-Map :
*/
function initMap(selector, longitude, latitude, img_point){
	jQuery('#'+selector).jmap('init', {'mapCenter':[longitude,latitude], 'mapZoom':18});
	var map = new GMap2(document.getElementById(selector));
	map.setCenter(new GLatLng(longitude,latitude), 15);
  var point = new GLatLng(longitude,latitude);
	
	var iconG = new GIcon();
 	// iconG.image = '../img/img_ui/mc_map_icon.png';
	iconG.image = img_point;
  // iconG.shadow = 'mc_map_icon.png';
  iconG.iconSize = new GSize(26, 32);
  // iconG.shadowSize = new GSize(22, 20);
  iconG.iconAnchor = new GPoint(6, 20);
  iconG.infoWindowAnchor = new GPoint(0, 6);

	var point2 = new GLatLng(longitude,latitude);
	var marker2 = new GMarker(point2, iconG);
  map.addOverlay(marker2);
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	return map;
}

/*
function log(s) {
   if(console) {
      if(navigator.userAgent.toLowerCase().indexOf("applewebkit") != -1) {
         console.log(s);
      } else {
         console.log.apply(this,arguments);
      }
   }
}
*/

$(function(){
	if ($('body.accueil').length == 0) { // if we are NOT in the Home page :
		/*
			Share This:
		*/
		var object = SHARETHIS.addEntry({},{button:false});
		var element = document.getElementById("share");
		object.attachButton(element);
		/*
			PRINT THIS PAGE :
		*/
		printable.init();
	}
	
	// inactive links :
	$('.inactive').click(function(){
		return false;
	}).css({'cursor':'default'});
});



function positionFooter(){
	if($("#content").height() > $(window).height()) {
		$("#footer").css({
			position: "absolute",
			top:($("#content").height()+50)
		});
	}else {
		$("#footer").css({
			position: "absolute",
			top:(($(window).scrollTop()+$(window).height())-$("#footer").height())+"px"		
		});
	}	
}

$(window)
	.load(positionFooter)
	.scroll(positionFooter)
	.resize(positionFooter);
$(document).ready(positionFooter);



