var base_href = 'http://' + location.host;
$(document).ready(function() {
	/*$('form#search').hide();
	
	$('li.search span').click(function() {
		$('form#search').toggle();
		$('form#search input').val('')
		$('form#search input').focus();
		$('form#search input').removeClass('smooth');
	});
	*/
	
	$('li.search span').hide();
	
	$('ul#navigation a').each(function() {
		if(stristr($(this).attr('href'), '.pdf')) {
			$(this).parent().addClass('pdf');
		}
	});
	
	$('#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() {
		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; });
	$(".videos").click(function() { return doInfoLightbox($(this), 'videos'); return false; });
	$(".options").click(function() { return doInfoLightbox($(this), 'options'); 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 {}
	});

	/** Product Slider **/
	$('a.next').mouseover(function() {
		$('#product_slider').everyTime(10, 'controlled', function() {
			var fullLength = $('#product_slider').css('width');
			fullLength = fullLength.replace(/px/, "");
			if(fullLength > 780) {
				var pos = $('#product_slider').position();
				var common = fullLength;
				if((Math.abs(pos['left'])+780) >= common) {
					var nextStep = pos['left'];
				} else {
					var nextStep = pos['left']-3;
				}
				$('#product_slider').css('left', nextStep);
			}
		});

	});
	
	$('a.next').mouseout(function() {
		$('#product_slider').stopTime('controlled');
	});
	
	$('a.previous').mouseover(function() {
		$('#product_slider').everyTime(10, 'controlled', function() {
			var pos = $('#product_slider').position();
			if(pos['left'] >= 0) {
				var nextStep = pos['left'];
			} else {
				var nextStep = pos['left']+3;
			}
			$('#product_slider').css('left', nextStep);
		});

	});
	
	$('a.previous').mouseout(function() {
		$('#product_slider').stopTime('controlled');
	});
	
	if($('#product_slider').length > 0) {
		var fullLength = $('#product_slider').css('width');
			fullLength = fullLength.replace(/px/, "");
		if(fullLength < 780) {
			$('.next').hide();
			$('.previous').hide();
		}
	}
	
	/*
	var widthMural = $('#other_products').width()-20;
	var widthWindow = $(window).width();
	var widthInner = $('#product_slider').width()+200;
	$('#product_slider').css('width', widthInner+'px');
	$('#product_slider').css('margin-left', "50px");
	
	var mouseBlubb = ((widthWindow-widthMural)/2)+100;			// Wieviel der Mausposition abgerechnet werden muss
	
	if((parseInt( $('#product_slider').width() )-200)>$('#other_products').width()) {
		$('#other_products').mousemove(function(e){
			var XmousePos = e.pageX-mouseBlubb;  
			var percentPos = ((XmousePos*1.3) * 100) / widthMural;
			var innerMove = ((widthInner-widthMural) * (percentPos-8)) / 100;
			$('#product_slider').css('left', '-' + innerMove + "px");
	    }); 
	    
	}
    
	/** Product Slider *
	if($('#product_slider').length > 0) {
		totalWidthOfBox = 0;  
		totalLeftOfActive = 0;     
		widthOfVisibleBox = $("#product_slider").width();   
		counton = true;
		if((parseInt( $('#product_slider').width() )-200)>$('#other_products').width()) {
			$('#product_slider a').each(function() {
				
				if(counton == true) {
					totalLeftOfActive += parseInt($(this).outerWidth(true));
				} 
				if($(this).hasClass('active')) {
					current_active = $(this);
					counton = false;
					$('#product_slider').css('left', '-'+totalWidthOfBox+'px');
				}
				
				totalWidthOfBox += parseInt($(this).outerWidth(true));  
			}); 
		}
	}

    */

	/** Product Slider *
	if($('.product_slider')) {
		totalWidthOfBox = 0;  
		totalLeftOfActive = 0;     
		widthOfVisibleBox = $("#product_slider").width();   
		counton = true;
		$('#product_slider a').each(function() {
			
			if(counton == true) {
				totalLeftOfActive += parseInt($(this).outerWidth(true));
			}
			
			totalWidthOfBox += parseInt($(this).outerWidth(true));   
			if($(this).hasClass('active')) {
				current_active = $(this);
				counton = false;
			}
		});     
		$("#product_slider").css('width', (totalWidthOfBox+40) + 'px')     
	    
		/*if (totalWidthOfBox > widthOfVisibleBox) {
			$('#product_slider a').wrapAll('<div id="imageSlide_Wrapper" style="position: relative; left: 25px; width: '+totalWidthOfBox+'px"></div>');
	
			outerToInnerRelation = (totalWidthOfBox - widthOfVisibleBox) / widthOfVisibleBox;
			$('#imageSlide_Wrapper').css('left', '-' + (totalLeftOfActive-500) + 'px');
	
			$('#product_slider').mousemove(function(event) {
			  $('#imageSlide_Wrapper').stop();
			  currentMouseposition = event.clientX - $('#product_slider').offset()['left'];
			  interpolatedPosition = currentMouseposition * outerToInnerRelation;     
 
	
			  if (interpolatedPosition <= (totalWidthOfBox - widthOfVisibleBox)) {
				  if(interpolatedPosition < (250 * outerToInnerRelation)) {
				    $('#imageSlide_Wrapper').css('left', '25px');
				  } else {
				    $('#imageSlide_Wrapper').css('left', '-'+interpolatedPosition+'px');
				  }
			  }
			});                                                   
		}
	}*/

	/**  E N D **/	
	
	$('.show-answer').click(function() {
		$('#' + $(this).attr('rel')).toggle();
	});
	
	/* lider
	$('.sliderchange img').hide();
	$('.sliderchange img.show').show();
	
	setInterval("sliderMan('.sliderchange')", 3600);
	 */
});

/*
function sliderMan(elementName) {
	var element = $(elementName);
	var elementActive = element.find('img.show');
	
	var elementActiveIndex = elementActive.attr('rel');
	var elementActiveNextIndex = (parseInt(elementActiveIndex)+1);
	
	if(elementActiveNextIndex > 5) {
		elementActiveNextIndex = 1;
	}
	
	$('img', element).each(function() {
		if($(this).hasClass('show')) {
			$(this).removeClass('show');
			$(this).fadeOut('slow');
		}
		
		if($(this).attr('rel') == elementActiveNextIndex) {
			$(this).addClass('show');
			$(this).fadeIn('slow');
		}
	});
}
*/

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') == 'videos') {
		boxwidth = 500;
	}
	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()+20) + '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').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 = 991;
	} else {
		var boxwidth_ratio_calc = 991;
	}
	
	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();
		}
	});
}
