var base_href = 'http://' + location.host;
$(document).ready(function() {
	
	if($.browser.mozilla) {
		$('html').addClass('mozilla');
	}
	
	$('li.search span').hide();
	
	$('#main_navi > li').mouseenter(function(){
		$(this).children('ul').show();
		$(this).addClass('pseudo_active');
	}).mouseleave(function(){
		$(this).children('ul').hide();
		$(this).removeClass('pseudo_active');
	});

	
	$('#perform-komfortsuche').submit(function() {
		$('#search_results').find('p').empty();
		$('#search_results').find('p').addClass('loadingpetersen');
		
		var query_string = '?search=true';
		$('input', this).each(function() {
			if($(this).is(':checked')) {
				query_string += '&options[]=' + $(this).val();
			}
		});
		
		$.get("/search/komfortsuche" + query_string, function (data) {	
			$('#search_results').html(data);
		});
		return false;
	});
	
	$("#product_img").hover (
		function () {
			$(".zoom_in").fadeIn();
		},
		function () {
			$(".zoom_in").fadeOut();
		}
	);
	
	$("#product_img").click(function() {
		doLightbox($(this), 'zoom');
	});
	
	$(".product_thumbs a").click(function() {
		if(!$(this).parent().hasClass('no')) {
			doLightbox($(this), 'detail_zoom');
		}
	});
	
	$(".technical").click(function() { return doInfoLightbox($(this), ''); return false; });
	$(".benefits").click(function() { return doInfoLightbox($(this), ''); return false; });
	$(".accessoires").click(function() { return doInfoLightbox($(this), 'true'); return false; });
	$(".download").click(function() { return doInfoLightbox($(this), ''); return false; });
	$(".logistics").click(function() { return doInfoLightbox($(this), ''); return false; });
	$(".faqs").click(function() { return doInfoLightbox($(this), 'faq'); return false; });
	$(".tests").click(function() { return doInfoLightbox($(this), 'tests'); return false; });
	
	$(".macafe_video_lbox").click(function() { return doLightbox($(this), 'macafe_video'); return false; });
	
	
		$('#search_query').focus(function() {
			if($(this).val() == 'Suchbegriff(e) eingeben') {
				$(this).val('');
				$(this).removeClass('smooth');
			} else {}
		});
		$('#search_query').blur(function() {
			if($(this).val() == '') {
				$(this).val('Suchbegriff(e) eingeben');
				$(this).addClass('smooth');
			} else {}
		});
		
		$('#search_query').focus(function() {
			if($(this).val() == 'Search the site') {
				$(this).val('');
				$(this).removeClass('smooth');
			} else {}
		});

	
	$('.ie .required').prepend('* ');
	
	$('.show-answer').click(function() {
		$('#' + $(this).attr('rel')).toggle();
		
		if($(this).hasClass('bold')) {
			$(this).removeClass('bold');
		} else {
			$(this).addClass('bold');
		}
	});
	
	var slides = $('.sliderchange li').length;
	if(slides > 1) {
		setInterval("slides()", 8000); //alle X Sekunden z.B. 10 Sek. = 10000 millisekunden
	}
});

function slides() {
	var first = $('.sliderchange li:first');
	var current = $('.sliderchange li.active');
	var next = current.next();
	
	if(next.hasClass('show')) {
		current.removeClass('active');
		next.addClass('active');
		
		next.fadeIn('slow');
		current.hide();
	} else {
		current.removeClass('active');
		first.addClass('active');
		
		first.fadeIn('slow');
		current.hide();
	}
}

function removeInfoLightbox() {
	$('#infolightbox').remove();
	$('#infolightboxbg').remove();
}


function detailimageforward(amount) {
	current = parseInt($('.zsvisible').attr('rel'));
	next = (current+1);
	
	if(next > amount) {
		next = 1;
	}
	
	$('.zsvisible').removeClass('zsvisible');
	$('.detailzoom').each(function() {
		if($(this).attr('rel') == next) {
			$(this).addClass('zsvisible');
			$(this).fadeIn();
		}
	});
	$('#imageno').empty();
	$('#imageno').html(next);
}

function detailimageback(amount) {
	current = parseInt($('.zsvisible').attr('rel'));
	previous = (current-1);
	
	if(previous < 1) {
		previous = amount;
	}
	
	$('.zsvisible').removeClass('zsvisible');
	$('.detailzoom').each(function() {
		if($(this).attr('rel') == previous) {
			$(this).addClass('zsvisible');
			$(this).fadeIn();
		}
	});
	$('#imageno').empty();
	$('#imageno').html(previous);
}


function zoomimageforward(amount) {
	current = parseInt($('.zsvisible').attr('rel'));
	next = (current+1);
	
	if(next > amount) {
		next = 1;
	}
	
	$('.zsvisible').removeClass('zsvisible');
	$('.zoomslide').each(function() {
		if($(this).attr('rel') == next) {
			$(this).addClass('zsvisible');
			$(this).fadeIn();
		}
	});
	$('#imageno').empty();
	$('#imageno').html(next);
}

function zoomimageback(amount) {
	current = parseInt($('.zsvisible').attr('rel'));
	previous = (current-1);
	
	if(previous < 1) {
		previous = amount;
	}
	
	$('.zsvisible').removeClass('zsvisible');
	$('.zoomslide').each(function() {
		if($(this).attr('rel') == previous) {
			$(this).addClass('zsvisible');
			$(this).fadeIn();
		}
	});
	$('#imageno').empty();
	$('#imageno').html(previous);
}

function removeLightbox() {
	$('#lightbox').remove();
	$('#lightboxbg').remove();
}

function doInfoLightbox(element, maxHeight) {
	$('#infolightbox').remove();
	var documentHeight = $(window).height();
	var documentHeight2 = $(document).height();
	var documentWidth = $(document).width();
	
	$('body').append('<div id="infolightboxbg" onclick="removeInfoLightbox()"></div>');
	$('#infolightboxbg').css('width', documentWidth + 'px');
	
	var boxwidth = 350;
	var boxwidth_ratio = (documentWidth-100)/2;
	
	$('#infolightboxbg').css('height', documentHeight2 + 'px');
	
	$('#infolightboxbg').fadeIn();
	
	if(element.attr('class') == 'accessoires') {
		boxwidth = 480;
	}
	if(element.attr('class') == 'faqs') {
		maxHeight = 'true';
	}
	
	$.get("/products/getdetails?product_id=" + element.attr('rel') + '&type=' + element.attr('class'), function (data) {	
		
		if(maxHeight == 'true') {
			css_xtra = 'height:'+(documentHeight-100)+'px !important;';
			css_xtra += 'overflow:auto;';
		} else {
			css_xtra = '';
		}
		
		$('body').append('<div id="infolightbox" style="'+css_xtra+'" class="'+element.attr('class')+'"></div');
		$('#infolightbox').html(data);
		
		$('#infolightbox').css('width', boxwidth + 'px');
		$('#infolightbox').css('top', ($(window).scrollTop()+30) + 'px');
		$('#infolightbox').css('left', boxwidth_ratio + 'px');
	});
	
	return false;
}

function doLightbox(element, typ) {
	$('#lightbox').remove();
	var documentHeight = $(document).height();
	var documentWidth = $(document).width();
	
	$('body').append('<div id="lightboxbg" onclick="removeLightbox();"></div>');
	$('#lightboxbg').css('width', documentWidth + 'px');
	
	
	if(typ == 'detail_zoom') { 
		linker = 'productimagesdetail?image_id=' + element.attr('rel') + '&product_id=' + $('#product_thumbs_check').attr('rel');
		var boxwidth_ratio_calc = 450;
	} else if(typ == 'faq') {
	} else if(typ == 'zoom') {
		linker = 'productimageszoom?product_id=' + element.attr('rel');		
		var boxwidth_ratio_calc = 960;
	} else {
		var boxwidth_ratio_calc = 960;
	}
	
	if(typ == 'macafe_video') {
		var boxwidth_ratio = (documentWidth-boxwidth_ratio_calc)/2;
		var boxwidth = (documentWidth-(2*boxwidth_ratio));
		var maxwidth = (boxwidth-80);
		var imageWidth = $('#lightbox-image').width();
		var imageHeight = $('#lightbox-image').height();
		
		var haight = documentHeight < imageHeight ? (imageHeight+200) : (documentHeight+200);
		$('#lightboxbg').css('height', haight + 'px');
		$('#lightboxbg').show();
		
		$('body').append('<div id="lightbox" class="'+element.attr('class')+'"></div');
		$('#lightbox').html('<object width="560" height="349"><param name="movie" value="http://www.youtube.com/v/EAOrzGjC-5I?version=3&amp;hl=de_DE" /><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><embed type="application/x-shockwave-flash" width="775" height="450" src="http://www.youtube.com/v/EAOrzGjC-5I?version=3&amp;hl=de_DE" allowscriptaccess="always" allowfullscreen="true"></embed></object><p><a href="javascript:;" onclick="removeLightbox();">schliessen</a></p>');
	
		$('#lightbox').css('width', boxwidth + 'px');
		$('#lightbox').css('top', ($(window).scrollTop()+20) + 'px');
		$('#lightbox').css('left', boxwidth_ratio + 'px');
		$('#lightbox').fadeIn();
	} else {
		var boxwidth_ratio = (documentWidth-boxwidth_ratio_calc)/2;
		var boxwidth = (documentWidth-(2*boxwidth_ratio));
		var maxwidth = (boxwidth-80);
		var imageWidth = $('#lightbox-image').width();
		var imageHeight = $('#lightbox-image').height();
		
		var haight = documentHeight < imageHeight ? (imageHeight+200) : (documentHeight+200);
		$('#lightboxbg').css('height', haight + 'px');
		$('#lightboxbg').show();
		
		$.get("/products/" + linker, function (data) {	
			$('body').append('<div id="lightbox" class="'+element.attr('class')+'"></div');
			$('#lightbox').html(data);
		
			$('#lightbox').css('width', boxwidth + 'px');
			$('#lightbox').css('top', ($(window).scrollTop()+20) + 'px');
			$('#lightbox').css('left', boxwidth_ratio + 'px');
			$('#lightbox').fadeIn();
			
			var current_id = 0;
			if(typ == 'detail_zoom') {
				var allcount = 0;
				var current = 1;
				$('#product_thumbs a').each(function() {
					if(element.attr('rel') == $(this).attr('rel')) {
						$('#current-image-no').html(current);
						current_id = element.attr('rel');
					}
					current = current+1;
					allcount = allcount+1;
				});
				$('#all-images-count').html(allcount);
				
				/* links */
				$('#product_thumbs a').each(function() {
					if($(this).attr('rel') == current_id) {
						if($(this).prev().attr('rel')) {
							previous_id = $(this).prev().attr('rel');
						} else {
							previous_id = $(this, 'a:last').parent().attr('rel');
						}
						if($(this).next().attr('rel')) {
							next_id = $(this).next().attr('rel');
						} else {
							next_id = $(this, 'a:first').parent().attr('rel');
						}
					}
				});
				$('#detimg-back').attr('rel', previous_id);
				$('#detimg-for').attr('rel', next_id);
			}
		});
	}
	
	return false;
}
	
function clickdetimgback()  {
	var id = $('#detimg-back').attr('rel');
	$.get("/products/changefulldetailimage?image_id=" + id, function (data) {	
		$('#fullview-detailimage-frame').attr('src', data);
	});
}

function clickdetimgfor()  {
	var id = $('#detimg-for').attr('rel');
	$.get("/products/changefulldetailimage?image_id=" + id, function (data) {	
		$('#fullview-detailimage-frame').attr('src', data);
	});
}

function stristr(haystack, needle, bool) {
    // Finds first occurrence of a string within another, case insensitive  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/stristr
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfxied by: Onno Marsman
    // *     example 1: stristr('Kevin van Zonneveld', 'Van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: stristr('Kevin van Zonneveld', 'VAN', true);
    // *     returns 2: 'Kevin '
    var pos = 0;

    haystack += '';
    pos = haystack.toLowerCase().indexOf( (needle+'').toLowerCase() );
    if (pos == -1){
        return false;
    } else{
        if (bool) {
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}

function showCountryInfo(country) {
	$('.country-info').hide();
	$('.country-info').each(function() {
		if($(this).hasClass(country)) {
			$(this).show();
		}
	});
}
