// =============
// = FUNCTIONS =
// =============

// landing flash
function itChangePage(url) {
	window.location.href = url;
}

// ======================
// = FOOTER POSITIONING =
// ======================
footer = {
	initialdocheight : 0,
	contentdiv       : 0,
	contentoffset    : 0,
	contentheight    : 0,
	docheight        : 0,
	windowheight     : 0,
	init : function(){
		footer.initialdocheight = footer.setvalues();
		footer.setfooterposition();
	},
	resize : function(){
		footer.setvalues();
		footer.setfooterposition();
	},
	setvalues : function(){
		footer.contentdiv    = $('#wrapper');			// get content element
		footer.contentoffset = parseInt(footer.contentdiv.offset().top);					// get content's top offset
		footer.contentheight = parseInt(footer.contentdiv.height());              // get content's height
		footer.docheight     = footer.contentoffset + footer.contentheight; // get document's height
		footer.windowheight  = parseInt($(document).height());       // get window's height
		return footer.docheight;
		/*
		footer.contentdiv    = document.getElementById('wrapper');			// get content element
				footer.contentoffset = footer.contentdiv.offsetTop;					// get content's top offset
				footer.contentheight = footer.contentdiv.offsetHeight;              // get content's height
				footer.docheight     = footer.contentoffset + footer.contentheight; // get document's height
				footer.windowheight  = document.documentElement.clientHeight;       // get window's height
				return footer.docheight;
		*/
	},
	setfooterposition : function(){
		// if the document's height is less than the window's height :
		if (footer.windowheight > footer.docheight) {
			// if (window.console) console.log('if');
			  // console.log('true', footer.windowheight,'-', footer.docheight, (footer.windowheight - footer.docheight));
			$('#wrapper').css({'height': (footer.windowheight - footer.contentoffset)+ 'px' });
			if (jQuery.browser.msie) {
				$('#wrapper').css({'height': (footer.windowheight - footer.contentoffset) + 20+ 'px' });
			}
			$('#footer').css({'position':'absolute','top': (footer.windowheight - document.getElementById('footer').offsetHeight) + 'px'});
		}else{
			// if (window.console) console.log('else');
			 // console.log('false', footer.windowheight, '-', footer.docheight, (footer.windowheight - footer.docheight));
			$('#wrapper').css({'height': 'auto'});
			// alert(footer.initialdocheight + ' - ' + footer.contentoffset + ' = ' + (footer.initialdocheight - footer.contentoffset));
			$('#footer').css({'position':'static'});
		}
	},
	debug : function(){
		$('#footer').html(
				'footer.contentdiv : ' + footer.contentdiv + ', ' +
				'footer.contentoffset : ' + footer.contentoffset + ', ' +
				'footer.contentheight : ' + footer.contentheight + ', ' +
				'footer.docheight : ' + footer.docheight + ', ' +
				'footer.windowheight : ' + footer.windowheight + ', ' +
				'footer relative position : ' + (footer.windowheight - footer.contentoffset) 
			);
		// console.log(footer.contentdiv);
		// console.log(footer.contentoffset);
		// console.log(footer.contentheight);
		// console.log(footer.docheight);
		// console.log(footer.windowheight);
	}
}

/*
	Folding blocs for News, Events, etc.
*/
foldingblocks = {
	blocheight : 22, 
	linkselector : 'h3 a.title',
	init : function(blocheight,linkselector){
		var self = this;
		if (blocheight) {
			self.blocheight = blocheight;
		};
		if (linkselector) {
			self.linkselector = linkselector;
		};
		$('.inbloc').each(function(i){
			$(this).data('originalheight', $(this).height());
			$(this).height(self.blocheight);
			$(self.linkselector, $(this)).toggle(function(){
				var $bloc = $('.inbloc:eq('+i+')');
				$('.inbloc:eq('+i+')').animate({'height':$bloc.data('originalheight')+"px"}, 'slow');
				$(this).addClass('close');
			}, function(){
				var $bloc = $('.inbloc:eq('+i+')');
				$('.inbloc:eq('+i+')').animate({'height':self.blocheight+"px"}, 'slow');
				$(this).removeClass('close');
			});
			$(this).css({'height': self.blocheight+'px'});
		});
	}
}

foldingblocks_dealers = {
	blocheight : 22, 
	linkselector : 'h3 a.title',
	init : function(blocheight,linkselector){
		var self = this;
		if (blocheight) {
			self.blocheight = blocheight;
		};
		if (linkselector) {
			self.linkselector = linkselector;
		};
		$('.inbloc').each(function(i){
			$(this).data('originalheight', $(this).height());
			$(this).data('original_reduced', $('h3', $(this)).height() + 20);
			$(this).height($('h3', $(this)).height() + 20);
			$(self.linkselector, $(this)).toggle(function(){
				var $bloc = $('.inbloc:eq('+i+')');
				$('.inbloc:eq('+i+')').animate({'height':$bloc.data('originalheight')+"px"}, 'slow');
				$(this).addClass('close');
			}, function(){
				var $bloc = $('.inbloc:eq('+i+')');
				$('.inbloc:eq('+i+')').animate({'height':$bloc.data('original_reduced')+"px"}, 'slow');
				$(this).removeClass('close');
			});
			// $(this).css({'height': self.blocheight+'px'});
		});
	}
}

/*
	Dolls & Characters details sliding :
*/
dolls = {
	divHeight : 0,
	init : function(){
		var self = this;
		// cacher tous les divs de details :
		$('.item_details').hide();
		// quand on click sur un bouton .more_details :
		$('.modeltype a.more_details').click(function(){
			var $link = $(this);
			var divheight = 0;
			var anchor = $link.attr('href');
			if ($('#introtext').is(':visible')) { // if #introtext is visible :
				$('#rtcol').slideUp('slow');
				$('#introtext').slideUp('slow', function(){
					$divToHide = $('a[name="'+anchor.substring(1)+'"]').next('.item_details');
					self.divHeight = $divToHide.height();
					// $divToHide.slideDown('slow');
					self.showDiv($divToHide);
				});
			} else {	// if #introtext is not visible :
				var $visibleDiv = $('.item_details:visible');
				$visibleDiv.height(self.divHeight);
				$visibleDiv.slideUp('slow', function(){
					var $divToShow = $('a[name="'+anchor.substring(1)+'"]').next('.item_details');
					self.divHeight = $divToShow.height();
					// $divToShow.slideDown('slow');
					self.showDiv($divToShow);
				});
			};
			return false;
		});
	},
	showDiv : function(obj){
		if (jQuery.browser.msie) {
			var $galleryToShow = obj.find('.item_right').css({'visibility':'hidden'});
		}
		// if (window.console) console.log($galleryToShow);
		obj.slideDown('slow', function(){
			if (jQuery.browser.msie) {
				$galleryToShow.css({'visibility':'visible'});
			}
		});
		
	}
}

// =================
// = TOP LEVEL NAV =
// =================
firstnav = {
	init : function(){
		// fading effect on the menu buttons :
		$('ul#navtop a').append('<span class="hover" />').each(function(){
			var $span = $('> span.hover', $(this)).css({'opacity':0});
			$(this).hover(function(){
				// on hover
				$span.stop().fadeTo(500, 1);
			},function(){
				// off hover
				$span.stop().fadeTo(500, 0);
			});
		});
	}
}

brandsImageZoom = {
	percent : 3,
	init : function() {
		// if we are in IE6, dont try to be cool. IE6 is _not_ cool.
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
			return false;
		};
		// if we have a cool browser :
		var self = this;
		$('ul.model a').append('<span class="more"></span>').each(function(){
			var $span = $('> span.more', $(this)).css({'opacity':0});
			var $img           = $('div.image img', $(this));
			var originalWidth  = $img.width();
			var originalHeight = $img.height();
			// Events :
			$(this).hover(function(){
				// fade the More button :
				$span.stop().fadeTo(500, 1);
				// // calculate dimensions differences :
				// var wdiff = (originalWidth * self.percent)/100;
				// var hdiff = (originalHeight * self.percent)/100;
				// // animate the image :
				// $img.stop().animate({
				// 	left: '-'+(wdiff/2)+'px',
				// 	top: '-'+(hdiff/2)+'px',
				// 	width: originalWidth + wdiff,
				// 	height: originalHeight + hdiff
				// }, 'slow');
			},function(){
				// fade the More button :
				$span.stop().fadeTo(500, 0);
				// animate the image :
				// $img.stop().animate({
				// 	left: '0px',
				// 	top: '0px',
				// 	width: originalWidth,
				// 	height: originalHeight
				// }, 'slow');
			});
			// because of IE :
			$img.closest('a').click(function(){
				window.location.href = $(this).attr('href');
			});
		});
	}
}

/*
	PrettyGallery
*/
gallery = {
	init : function(){
		$gallery = $("ul.gallery");
		$gallery.prettyGallery({
			itemsPerPage : 1,
			of_label: '' /* The content in the page "1 of 2" */
		});
		// when we click on the little target :
		$('a#lightbox').click(function(){
			$('li', $gallery).each(function(){
				if ($(this).position().left == 0) {
					$("a[rel^='prettyPhoto[gallery]']", $(this)).triggerHandler('click');	
					return false;
				};
			});
			return false;
		});
	}
}