/*global $, document, window, clearTimeout, setTimeout */

var Accordion = function() {
	
		/* Open/close accordion */
		this.openCloseAccordion = function (e) {
			e.preventDefault();
			var li = jQuery(this).parent();
			if(li.size() > 0 && li.find("ul").size() > 0) {
				if(li.hasClass("active")) {
					li.removeClass("active");
					li.find("ul").addClass("hiddenContent");
				}
				else {
					li.addClass("active");
					li.find("ul.hiddenContent").removeClass("hiddenContent");
				}
			}
		};
		
		/* Accordion's initialisation */
		this.initAccordions = function () {
			jQuery("ul.JSaccordionList > li").removeClass("active");
			jQuery("ul.JSaccordionList > li > ul").addClass("hiddenContent");
			jQuery("ul.JSaccordionList > li > a").bind("click", this.openCloseAccordion);
		};
		
		this.initAccordions();
}

/* Display/hide Popin */
function closePopinHandler(e) {
	e.preventDefault();
	var target = jQuery('#' + jQuery(this).attr('rel'));
	if (jQuery.browser.msie && jQuery.browser.version <= 6) {
		jQuery('#A_body select').css("visibility","visible");
	}
	jQuery('#bgPopin').hide();
	target.hide();
}

function closePopinHandlerNoBackGround(e) {
	e.preventDefault();
	var target = jQuery('#' + jQuery(this).attr('rel'));
	if (jQuery.browser.msie && jQuery.browser.version <= 6) {
		jQuery('#A_body select').css("visibility","visible");
	}
	target.hide();
}

function openPopin(idTarget) {
	var target = jQuery('#' + idTarget);
	if (jQuery.browser.msie && jQuery.browser.version <= 6) {
		jQuery('#bgPopin').css({ height: jQuery('#theDocument').height()});
		jQuery('#A_body select').css("visibility","hidden");
	}
	jQuery('#bgPopin').show();
	/*target.css('top', jQuery(window).height()/2-target.height()/2);*/
	
	var jWindow = jQuery(window);
	/* target.css('left', jWindow.width() / 2 - target.width() / 2 + jWindow.scrollLeft()); */
	target.css('top', jWindow.height() / 2 - target.height() / 2 + jWindow.scrollTop());
	
	target.show();
}

function openPopinHandler(e) {
	e.preventDefault();
	openPopin(jQuery(this).attr('rel'));
}


/* Open link in a new window */
function openInNewWindow (e) {
	window.open(this);
}

function displayHistoryBlock(event){
	event.preventDefault();
	if (!jQuery(this).hasClass("active")) {
		jQuery(this).parent().addClass("active")
						.siblings(".active").removeClass("active");
						
		var blockUrl = jQuery(this).attr('href').split("#")[1];
		jQuery('#'+blockUrl)	.show()
						.siblings(".magHistoryContent").hide();
	}
} 

/* */
function emptyFieldValue(){
	var fieldValue = jQuery(this)[0].defaultValue;
	if (jQuery(this).attr("readonly") == "") {
		if (jQuery(this).attr("value") ==  fieldValue){
			jQuery(this).val("");
			jQuery(this).bind("blur",function(){
				if(!jQuery(this).val().length){
					jQuery(this).val(fieldValue);
				}
			});
		}
	}
}

function emptyFieldValueBis(){
	var fieldValue = jQuery(this)[0].defaultValue;
	if (jQuery(this).attr("readonly") == "" && jQuery(this).hasClass("defaultValues")) {
		if (jQuery(this).attr("value") ==  fieldValue){
			jQuery(this).val("");
			jQuery(this).bind("blur",function(){
				if(!jQuery(this).val().length){
					jQuery(this).val(fieldValue);
				}
			});
		}
	}
}

/* Document's initialisation */
jQuery(document).ready(function() {

	if(jQuery('#TopicOfInterestMod').length > 0){
		jQuery('#btContinue').bind('click',function(){
			setTopicOfInterestCookie();
		});
	}
	
	if(jQuery('#chageSkinMod').length > 0){
		jQuery('#validateModifications').bind('click',function(){
			setSkinCookie();
		});
	}
	
	if(jQuery('.editCarProfile').length > 0){
		jQuery('#btContinue').bind('click',function(){
			setCarProfileValideCookie();
		});
	}
	
	if(jQuery('.carProfile').length > 0){
		jQuery('#btContinue').bind('click',function(){
			setCarProfileEditCookie();
		});
	}
	
	
	if(jQuery('.editMyProfile').length > 0){
		jQuery('#btContinue').bind('click',function(){
			setMyProfileCookie();
		});
	}
	
	if (jQuery('ul.JSaccordionList') != null) {
		new Accordion();
	}
	
	/* Popin */
	jQuery('.jsClosePopin').bind('click', closePopinHandler);
	jQuery('.jsOpenPopin').bind('click', openPopinHandler);
	
	/* Carousel */
	if (jQuery('.articleCarouselBlock').length) {
		jQuery('.articleCarouselBlock').each(function(){
			if (jQuery(this).find(".articleCarouselItem").length > 3) {
				jQuery(this).after("<a class=\"previousBt\"><span>Previous</span></a><a class=\"nextBt\"><span>Next</span></a>");
				var btNext = jQuery(this).siblings(".nextBt");
				var btPrevious = jQuery(this).siblings(".previousBt");
				jQuery('.articleCarouselBlock').jCarouselLite({
					visible: 3,
					speed: 800,
					btnNext: btNext,
					btnPrev: btPrevious
				});
			}
		});
	}
	
	/* eMag */
	if (jQuery('.magHistoryHeading').length) {
		var activeItem;
		if (jQuery('.magHistoryHeading .active').length) {
			activeItem = jQuery('.magHistoryHeading .active');
		}
		else {
			activeItem = jQuery('.magHistoryHeading > li:first');
			activeItem.addClass("active");
		}
		var urlhistoryDisplay = activeItem.find('a').attr("href").split("#")[1];
		jQuery('#' + urlhistoryDisplay).siblings(".magHistoryContent").hide();
		
		jQuery('.magHistoryHeading li a').bind('click', displayHistoryBlock);
	}
	
	/* Carousel */
	if (jQuery(".pushModCarousel").length) {
		jQuery(".pushModCarousel").each(function(){
			jQuery(this).after("<a class=\"previousBt\"><span>Previous</span></a><a class=\"nextBt\"><span>Next</span></a>");
			var btNext = jQuery(this).siblings(".nextBt");
			var btPrevious = jQuery(this).siblings(".previousBt");
			jQuery(this).jCarouselLite({
				btnNext: btNext,
				btnPrev: btPrevious,
				speed: 500,
				visible: 1
			});
		});
	}
	
	
	/* Login */
	jQuery("#j_username, #j_password").bind("focus", emptyFieldValue);
	
	/* edit Profile purchase month / year */
	jQuery("#purchaseMonth, #purchaseYear").bind("focus", emptyFieldValueBis);
	
	/* Mileage popin */
	if (jQuery("#mileagePopin") != null) {
		if (jQuery("#mileagePopin").hasClass("opening")) {
			openPopin("mileagePopin");
		}
	}
	
	/* Change Mileage popin */
	if (jQuery("#mileagePopin") != null && jQuery("#alertMileagePopinLink") != null) {
		jQuery(".alertMileagePopinLink").bind('click', function() {
			
			var url = jQuery('#mileagePopin #changeUrl').val();
			jQuery('#mileagePopin .popinContent').load(url,  function(evt) {
				openPopin("mileagePopin");	
			});
		});
	}
	
	/* Limitation des champs */
	if (jQuery("#annualmileage").length) {
		jQuery("#annualmileage").numeric();
	}
	
	if (jQuery("#currentmileage").length) {
		jQuery("#currentmileage").numeric();
	}
	
	if (jQuery("#purchaseMonth").length) {
		jQuery("#purchaseMonth").numeric();
	}
	
	if (jQuery("#purchaseYear").length) {
		jQuery("#purchaseYear").numeric();
	}
	
});


