/**
 * jQuery by Gerrit
 *
 * Changelog:
 * 2008-09-25: pagewrapper moved into static dom (tki)
 */
jQuery(function($) {

	
	/* fix morelink style for IE */
	if($.browser.msie) {		
		$('dd.active a').each(function (i, e) {
			$(e).append('&nbsp;<img width="8" height="8" src="css/img/check.gif" alt=""/>');
			e.style.backgroundImage = "none";
		});		
		$('.morelink').each(function (i, e) {
			$(e).append('&nbsp;<img class="morelink-icon" width="4" height="7" src="css/img/arrow-right.gif" alt=""/>');
			e.style.backgroundImage = "none";
		});
	}
	
	/* Translation: Read Strings */
	strShowmore = $('#str-showmore').text();
	strShowless = $('#str-showless').text();

	/* Print buttons */
	$('.b-print').click(function(){ window.print(); return false; });
	$('.b-layout-print').click(function(){ window.print(); return false; });
	$('.b-print-url-preview').click(function(){ window.open(this.href); return false; });

	/* Detail-Tabnavi for product pages */
	$('dl.detailtabs').each(function(){
	
		$(this).children('dd').hide();
		$(this).children('dt:first-child').addClass('active').parent().find("dd:first").show();
		$(this).children('dt').each(function(i){
			text = $(this).text();
			
			$(this).empty().html(	'<span>' + text + '</span>').click(function(event){
				var target    = event.target;
				if(target.tagName == "SPAN") target = target.parentNode;
				var className = $(target).attr('id');
				$(this).parent().find('dt').removeClass('active');
				$(this).parent().find('dd.top').hide();
				$(this).addClass('active').parent().find("." + className).show();
				return false;
			});
		});
	});

	/* Show and Hide Detail Salon Information */
	$('div.salonfacts a.morelink').click(function(){
		$(this).toggleClass('down');
		$(this).parent().parent().next().next().slideToggle(500);
		return false;
	});


	/* Show Help text when hovering elements */
	$('.help').hover(function(){
		temptext = $(this).attr('title');
		$(this).attr('title', '');
		$('body').append('<div id="helpbox">'+temptext+'</div>');
		$().mousemove(function(e){
			$('#helpbox').css('top', e.pageY-$(window).scrollTop()+17).css('left',e.pageX-$(window).scrollLeft()+7);
		});
	}, function(){
		$('#helpbox').remove();
		$(this).attr('title', temptext);
	});


	/* Start Lightbox */
	$('.lightbox').click(function(){

		var lb_URL    = this.href;
		var lb_width  = '766';
		var lb_height = '501';
		var lb_top    = 20;

		if(lb_URL.indexOf('dimension/') != -1) {
			var dimension = lb_URL.substring(lb_URL.indexOf('dimension/')+10);
			if(dimension.substring(0,1) == 'p') {
				lb_width  = '415';
				lb_height = '530';
			} else {
				lb_width  = '530';
				lb_height = '345';
			}
		} else if($(this).attr('coords') != '') {
			var coords = $(this).attr('coords').split(',');
			lb_width  = coords[0];
			lb_height = coords[1];
			
			if(coords.length > 2) {
				lb_top = coords[2];
			}
		}

		return openLightbox(lb_URL, lb_width, lb_height, lb_top);
	});

	/* Start Videobox */
	$('.videobox').click(function(){

		var lb_URL    = this.href;
		var lb_width  = '536';
		var lb_height = '345';
		var lb_top    = 20;

		return openLightbox(lb_URL, lb_width, lb_height, lb_top);
	});

	/* Start audiobox */
	$('.audiobox').click(function(){

		var lb_URL    = this.href;
		var lb_width  = '536';
		var lb_height = '78';
		var lb_top    = 20;

		return openLightbox(lb_URL, lb_width, lb_height, lb_top);
	});

	/* Start eMagBox */
	$('.emagbox').click(function(){

		var lb_URL    = this.href;
		var lb_width  = '935';
		var lb_height = '680';
		var lb_top    = 20;
		var lb_title  = this.title;

		if(lb_title == "") lb_title = "&nbsp;";
		
		return openMagbox(lb_URL, lb_width, lb_height, lb_top, lb_title);
	});
	

	/* Add to Download Cart */
	$('.todownloadcart').click(function(){

		return addToDownloadCart(this.href);
	});

	$(".result").click(function(){
		$(".result").hide();
	});

	/* ------------------------- product navigation ------------------ */


	/* show and hide level 2 navigation block */
	$('.navlevel-01 > li').hover(function(){
		$('.navlevel-02').hide();
		$(this).find('.navlevel-02').show();
		if ($(this).is('.navitem-02') == false) {
			$(this).find('.navlevel-02').css('height','auto');
		}
	}, function(){
		$(this).find('.navlevel-02').hide();
	});

	/* show and hide level 3 navigation block */
	$('.navlevel-03 > li:first-child').parent().parent().addClass('has-children');
	$('.navlevel-02 > li').click(function(){
		//$('.clicked').removeClass('clicked');
		//$(this).find('span').addClass('clicked');
		$(this).find('.navlevel-03').addClass('protectme');
		if ($(this).parent().parent().is('.navitem-02') == false) {
			$(this).parent().find('.navlevel-03').not('.protectme').slideUp(200);
			$(this).find('.navlevel-03').slideToggle(200).removeClass('protectme');
		} else {
			$(this).parent().find('.navlevel-03').hide();
			$(this).find('.navlevel-03').show().removeClass('protectme');
		}
	});


	/* show default products after loading page */
	$('.nav-main .navitem-02 .navlevel-02 > li').click(function(){
		$('.nav-main .navitem-02 .navlevel-02 > li.showbydefault').removeClass('showbydefault');
		
		// add showbydefault to first list element if not done before
		if($(this).find('.navlevel-03 > li.showbydefault').length == 0) {
				$(this).find('.navlevel-03 > li:first').addClass('showbydefault');
		}
	});
	$('.nav-main .navitem-02 .navlevel-02 > li > ul').bind("click", function(event) {
		event.stopPropagation();
	});

	$('.nav-main .navitem-02-01 .navlevel-03 > li').not('.showbydefault').mouseover(function(){
		$('.nav-main .navitem-02-01 .navlevel-03 > li.showbydefault').removeClass('showbydefault');
	});
	/* handle viewing of default menu */
	$('.nav-main .navitem-02 a:first').mouseover(function(){
		$('#defaultcoverage').addClass('showbydefault');
		$('#defaultbrand').addClass('showbydefault');
		$('.clicked').removeClass('clicked');
		$('#defaultcoverage').find('span').addClass('clicked');
		var brandlist = $('.nav-main .navitem-02').find('li.showbydefault ul.navlevel-03 > li');
		var prodlist  = $('.nav-main .navitem-02').find('li.showbydefault ul.navlevel-03 li.showbydefault ul.navlevel-04 > li');
		resizeNavi(brandlist, prodlist, 0);
		$('li.navitem-02 ul.navlevel-03').hide();
		$('li.showbydefault ul.navlevel-03').show();
	});


	/* 
	 * More or less brands visible in product category 1 
	 * Option is saved in a cookie 
	 */
	if($.cookie('more_products') == '1') {
		$('li.navitem-02-01 ul.navlevel-03').append('<li class="navimore"><a href="#" class="fakebutton button159 more">'+strShowless+'</a></li>');
	} 
	else {
		$('li.navitem-02-01 ul.navlevel-03').append('<li class="navimore"><a href="#" class="fakebutton button159 less">'+strShowmore+'</a></li>');
		$('li.navimore').prev().hide().addClass('closed').prev().hide().addClass('closed');
	}	
	$('li.navimore > a').click(function(){
		var brandlist = $(this).parent().parent().children('li');
		var prodlist = $(this).parent().parent().children('li:first-child > ul > li');
		// save the option in cookie
		var date = new Date();
        date.setTime(date.getTime() + (100 * 24 * 60 * 60 * 1000));
		if (this.className == "fakebutton button159 less") {
				this.className = "fakebutton button159 more";
				$(this).text(strShowless).parent().prev().removeClass('closed').prev().removeClass('closed');
				resizeNavi(brandlist, prodlist, 50);
				$(this).parent().prev().slideDown(200).prev().slideDown(200);                
                $.cookie('more_products', '1', { path: '/', expires: date });
		} else {
			this.className = "fakebutton button159 less";
			$(this).text(strShowmore).parent().prev().addClass('closed').prev().addClass('closed');
			$(this).parent().prev().slideUp(200).prev().slideUp(200);
            $.cookie('more_products', '0', { path: '/', expires: date });
		}
		return false;
	});

	/* Trigger Resize Product navigation when clicking 2nd level Tabs */
	$('li.navitem-02 > ul > li > span').click(function(){
		$('li.navitem-02 ul.navlevel-03').hide();
		$(this).parent().find('ul.navlevel-03').show();
		var brandlist = $(this).parent().find('ul.navlevel-03 > li');
		var prodlist = $(this).next().find('li:first-child li');
		resizeNavi(brandlist, prodlist, 200);
	});

	/*Datepicker*/
	if ($('input.date').length) {

		/* calculate default date */
		var defdt = new Date();
		defdt.setDate(defdt.getDate()+1);
		var day = defdt.getDay();
		if (day==0) {
			defdt.setDate(defdt.getDate()+2);
		}

		var defdt_string = defdt.getDate() + '.' + (1 + defdt.getMonth()) + '.' + defdt.getFullYear();

		$("input.date").datepicker($.extend({
			minDate: 1
		},
		$.datepicker.regional["de"], {
		    showStatus: true,
		    showOn: "both",
		    buttonImage: "img/calendar.gif",
		    buttonImageOnly: true,
		    defaultDate: defdt

		}));

	}

});


/* Resize Product navigation */
function resizeNavi(brandlist, prodlist, speed) {
// todo remove
}

/* Close Lightbox */
function closeLightbox() {

	//check if a lightbox is already open
	if(window.parent.document.getElementById('close') != null) {
		$(window.parent.document.getElementById('close')).fadeIn(400);
	}

	$('.iframewrapper').fadeOut(400, function(){
		$(this).remove();
	})
	$('#lb-background').fadeTo(400,0,function(){
		$(this).remove();
	});

	if($("#download-counter").length) {
		refreshDownloadCart();
	}

	return false;
}


/** 
 * stores the window title. from this it will be restored, after
 * the eMag changes it.
 */
var document_title = null;

/* Close Magbox */
function closeMagbox() {

	$('.magwrapper').fadeOut(400, function(){
		$(this).remove();
	})
	$('#lb-background').fadeTo(400,0,function(){
		$(this).remove();
	});

	document.title = document_title;
	
	return false;
}

function openLightbox(lb_URL, lb_width, lb_height, lb_top) {

	if(lb_top == null) {
		lb_top = 20;
	}

	//check if a lightbox is already open
	if(window.parent.document.getElementById('close') != null) {
		$(window.parent.document.getElementById('close')).fadeOut(400);
	}

	var lb_left   = ($('body').width() / 2) - (lb_width / 2);
	var lb_middle = ($('body').width() / 2);
	
	$('body').append('<div id="lb-background"></div>');
	$('body').append('<div class="iframewrapper" style="top: 250px; left: '+lb_middle+'px; height:10px; width: 0px;"><a href="#" class="iconbutton b-close" id="close">'+window.translate('Login schliessen')+'</a><iframe id="lb-content" src="'+lb_URL+'" width="'+lb_width+'" height="'+lb_height+'" frameborder="0"></iframe></div>');

	$('#lb-background').fadeTo(400, 0.7, function(){
		$('div.iframewrapper').animate({width: lb_width, left: lb_left},400,function(){
			$(this).animate({height: lb_height, top: lb_top},400);
		});
	});

	$('.iframewrapper a.b-close').click(closeLightbox);

	return false;
}


function openMagbox(lb_URL, lb_width, lb_height, lb_top, lb_title) {

	document_title = document.title;
	
	if(lb_top == null) {
		lb_top = 100;
	}

	var lb_left     = ($('body').width() / 2) - (lb_width / 2);
	var lb_middle   = ($('body').width() / 2);
	var frame_width = lb_width - 2;
	
	
	
	$('body').append('<div id="lb-background"></div>');
	$('body').append(
		'<div class="magwrapper" style="top: 250px; left: ' + lb_middle + 'px; height:10px; width: 0px;">' +
		'	<div class="head">' +
		'		<a href="#" class="iconbutton b-close" id="close">' + window.translate('Login schliessen') + '</a>' +
		'		<h1>' + lb_title + '</h1>' +
		'	</div>' +
		'	<iframe id="lb-content" src="' + lb_URL + '" width="' + frame_width + '" height="' + lb_height + '" frameborder="0"></iframe>' +
		'</div>');

	$('#lb-background').fadeTo(400, 0.7, function(){
		$('div.magwrapper').animate({width: lb_width, left: lb_left},400,function(){
			$(this).animate({height: lb_height, top: lb_top},400);
		});
	});

	$('.magwrapper a.b-close').click(closeMagbox);

	return false;
}


/* Close Iframe only */
function closeIframe() {
	$('.iframewrapper').fadeOut(400, function(){
		$(this).remove();
	})

	if($("#download-counter").length) {
		refreshDownloadCart();
	}

	return false;
}

function openIframe(lb_URL, lb_width, lb_height) {

	var lb_left = ($('body').width() / 2) - (lb_width / 2);
	var lb_middle = ($('body').width() / 2);
	$('body').append('<div class="iframewrapper" style="top: 250px; left: '+lb_middle+'px; height:10px; width: 0px;"><a href="#" class="iconbutton b-close" id="close">Login schliessen</a><iframe id="lb-content" src="'+lb_URL+'" width="'+lb_width+'" height="'+lb_height+'" frameborder="0"></iframe></div>');

	$('div.iframewrapper').animate({width: lb_width, left: lb_left},400,function(){
		$(this).animate({height: lb_height, top: '100'},400);
	});

	$('.iframewrapper a.b-close').click(closeLightbox);

	return false;
}

/* add media file to download cart and refresh couner*/
function addToDownloadCart(cart_URL) {

	$.ajax({
		type: "POST",
		dataType: "json",
		url: cart_URL,
		beforeSend: function(){
			$(".result").hide();
			$(".loader").show();
		},
		cache: false,
		success: function(result){

			$(".result").hide();
			$(".loader").hide();

			$("#"+result.success).stop(true, true).show().fadeOut(5000);
			refreshDownloadCart();
		}
	});

	return false;

}

function refreshDownloadCart() {
	$("#download-box").load(baseUrl('download/index/cartinfo') + '?' + Math.random() +  ' #download-box > *');
}


function checkForSupportedBrowser() {
    var browserVersion = jQuery.browser.version.replace(/\./g,"");
    browserVersion = browserVersion + "000000";
    browserVersion = browserVersion.substring(0,4);
    // allowed Browserlist:
    // IE	>= 7
    // Firefox	>= 3
    // Opera	>= 9.00
    // Safari	>= 3.2.1
    if ((jQuery.browser.msie && browserVersion<"7000") ||
	(jQuery.browser.mozilla && browserVersion<"1900") ||
	(jQuery.browser.opera && browserVersion<"9000") ||
	(jQuery.browser.safari && browserVersion<"5252")) {
		var url = baseUrl('default/error/unsupported-browser');
		if($.cookie('unsupportedBrowser')!='yes') {
			if(!window.parent.isIframe()) {
				openLightbox(url, 400, 290, 50);
			}
		}
	}
}

function isIframe() {
    if($('.iframewrapper').length != 0) {
	return true;
    } else {
	return false;
    }
}