function stopJS(){
	return isIE6();
};

function isIE6(){
	return jQuery.browser.msie && jQuery.browser.version <= "6.0";
}

function isIE7(){
	return jQuery.browser.msie && jQuery.browser.version == "7.0";
}

var theCarousel_itemList = [];

/**
* We use the initCallback callback
* to assign functionality to the controls
*/
function carousel_initCallback(carousel) {
	
};

function carousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, theCarousel_itemList.length);
    carousel.add(i, carousel_getItemHTML(theCarousel_itemList[idx - 1],i, carousel));
    if(!isIE6())
    {
	    $('#' + i).bind('click', function(inEvent) {
			carousel.scroll(jQuery.jcarousel.intval(this.id) - 1);
			if(inEvent.target.href) {
				return true;
			}
			return false;
		});
    } else {
    	$('#' + i).bind('click', function(inEvent) {
			document.location = $(inEvent.target).parent().find("a")[0].href;
		});
    }
};

function carousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function carousel_getItemHTML(item, i, carousel)
{
	if(item.url == 'http://www.rademacher.de/media/gfx/transparent.gif'){ 
		$("div.jcarousel-prev").css("visibility","hidden");
		$("div.jcarousel-next").css("visibility","hidden");
		try {
			return '<div id="' + i + '"><img src="' + item.url + '" width="' + carousel.options.minWidth + 'px" height="' + carousel.options.minHeight + 'px" alt="' + item.title + '" class="carousel_image"/><a href="" class="carousel_detail_link" style="display:none"></a></div>';
		} catch(e) {
			return "";
		}
	} 
	else 
	{
		try {
			return '<div id="' + i + '"><img src="' + item.url + '" width="' + carousel.options.minWidth + 'px" height="' + carousel.options.minHeight + 'px" alt="' + item.title + '" class="carousel_image"/><a href="' + item.link + '" class="carousel_detail_link" style="display:none">' + item.text + '</a></div>';
		} catch(e) {
			return "";
		}
	}
	
};


$(document).ready(function(){
	/**
	 * Initialize carousel on category site and not on detail product
	 */
	if($('.image_container_carousel') && $('.image_container_carousel').length > 0 && $('.image_container_right').length == 0 && $('#carousel li a.carousel_image_link').length > 0){// && !stopJS()) {
		
		itemCount = $('#carousel li a.carousel_image_link').length >= 4 ? 4 : $('#carousel li a.carousel_image_link').length;
		
	
		jQuery.each($('#carousel li a.carousel_image_link'),
			function(i, inElement) {
				theCarousel_itemList.push({link:inElement.href});
			}
		);
		
		jQuery.each($('#carousel li a.carousel_detail_link'),
				function(i, inElement) {
	                theCarousel_itemList[i].text = $(this).text();
				}
		);		
					
		jQuery.each($('#carousel li a.carousel_image_link img'),
			function(i, inElement) {
				theCarousel_itemList[i].url = inElement.src;
				theCarousel_itemList[i].title = inElement.alt;
			}
		);
	
		theCarousel_itemList.unshift(theCarousel_itemList.pop());
		
		try {
			$('#carousel li').remove();
		} catch(e){
		}
		
	
		
		jQuery('#carousel').jcarousel({
	        wrap: 'circular',
	        scroll: 1,
	        visible: itemCount,
	        minWidth: 165,
	        minHeight: 200,
	        maxWidth: 330,
	        maxHeight: 400,
	        initCallback: carousel_initCallback,
	        itemVisibleInCallback: {onBeforeAnimation: carousel_itemVisibleInCallback},
	        itemVisibleOutCallback: {onAfterAnimation: carousel_itemVisibleOutCallback}
	    });
		

	    
	}
});

$(document).ready(function(){
	if($('#logo_right'))
	{
		$('#logo_right').css('z-index', '5');
	}
	
	if($('#MapIframe')) {
		$('#MapIframe').show();
	}
	
	styleLanguageSelect();
	applyFaqSliders();
		
	
	/**
	 * Handle JS login-functionalities
	 */
	if($("#login_pw") && $("#login_pw").length > 0) {
		//JS-activated:
		$("#login_pw").show();
		$('#login_pw_type_password').hide();
		//Switch inputfield to type == password
		$("#login_pw").bind("focus", function(e){
			$("#login_pw").hide();
			$('#login_pw_type_password').show().select();
		});
		$(".tf_input_login_user").bind("focus", function(e){
			e.target.value = "";
			$(".tf_input_login_user").unbind("focus");
		});
	}
	//login_pw_imagebox
	if($("#login_pw_imagebox") && $("#login_pw_imagebox").length > 0) {
		//JS-activated:
		$("#login_pw_imagebox").show();
		$('#login_pw_type_password_imagebox').hide();
		//Switch inputfield to type == password
		$("#login_pw_imagebox").bind("focus", function(e){
			$("#login_pw_imagebox").hide();
			$('#login_pw_type_password_imagebox').show().select();
		});
		$(".tf_input_login_user").bind("focus", function(e){
			e.target.value = "";
			$(".tf_input_login_user").unbind("focus");
		});
	}
	
	if($(".tf_input_search")) {
		$(".tf_input_search").bind("focus", function(e){
			e.target.value = "";
			$(".tf_input_search").unbind("focus");
		});
	}
	
	/*
	if($('#text_searchfield'))
	{
		$('#text_searchfield').autofill({
								value: 'Suchbegriff hier eingeben',
								//defaultTextColor: '#f00',
								//activeTextColor: '#000'
		});
	}
	*/

	$('#text_size li').click(
		function changeFontSize(e) {
			jQuery.get('/storeSession.php?action=font&size=' + e.currentTarget.id, function(data){
				$('body').css({'font-size':data+'%'});
			});
	});
	
	if($('div#product_world_accordion')) 
	{	
		try {
			$('div#product_world_accordion').accordion({header: '.accordion_head', event: 'click', collapsible:true,active:null});
			$($('.accordion_head')[0]).css('margin-top',0);
		} catch(e) {}
	}
	
	/**
	 * Initialize ProductWorld selectboxes
	 */
	if($('#categoryselectbox').length > 0 && $('#categorydetailselectbox').length > 0)
	{	
		try {
			// imagebox or content_right width  
			if($('.content_right_teaser_product_search').length > 0) {
				$('#categoryselectbox').selectbox({'bgImageActiv':'bg_selectbox_open.png','bgImage':'bg_selectbox_close.png','inputLength':33});
				$('#categorydetailselectbox').selectbox({'bgImageActiv':'bg_selectbox_open.png','bgImage':'bg_selectbox_close.png','inputLength':33});
			} else {
				$('#categoryselectbox').selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png','inputLength':22});
				$('#categorydetailselectbox').selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png','inputLength':22});
			}
			
			$('#categorydetailselectbox_input').hide();
			
			$('#categorydetailselectbox_container ul li').each(function(){
				if(!$(this).hasClass('default')) {
					this.style.display = "none";
				}
			});
		
			$('#categoryselectbox_container ul li').bind('click', function(e){
				$('#categorydetailselectbox_input')[0].value = $('#categorydetailselectbox_container ul li')[0].id;
				$('#categorydetailselectbox_container ul li').each(function(){
					this.style.display = ($(this).hasClass('default') || $(this).hasClass(e.currentTarget.id)) ? "block" : "none";
				});
				if($(this).hasClass('default')) {
					$('#categorydetailselectbox_input').hide();
					$('.product_search_submit').hide();
				} else {
					$('#categorydetailselectbox_input').show();
					$('.product_search_submit').show();
				}	
				
			});
			/*
			$('#categoryselectbox_hover').hover(
					function(){
						$('#categoryselectbox_input').triggerHandler('focus');
					},
					function(){
						$('#categoryselectbox_input').trigger('blur');
					}
			);
			
			$('#categorydetailselectbox_hover').hover(
					function(){
						$('#categorydetailselectbox_input').triggerHandler('focus');
					},
					function(){
						$('#categorydetailselectbox_input').trigger('blur');
					}
			);
			*/
			$('#categorydetailselectbox_container ul li').bind('click', function(e){
				if($('#categorydetailselectbox')[0].value.indexOf("/") == 0) {
					window.location.href = "http://" + window.location.host + $('#categorydetailselectbox')[0].value;
				} else {
					//$($('#product_search_error')[0]).slideDown("slow");
					return false;
				}
			});

			/*$('.product_search_submit').bind('click', function(){
				if($('#categorydetailselectbox')[0].value.indexOf("/") == 0) {
					window.location.href = "http://" + window.location.host + $('#categorydetailselectbox')[0].value;
				} else {
					$($('#product_search_error')[0]).slideDown("slow");
					return false;
				}
				
			});
			*/
		} catch(e) {
			//console.log(e);
		}
	}
	
	if($('#countryselect').length > 0) {
		$('#countryselect').selectbox({'bgImageActiv':'bg_selectbox_form_open.png','bgImage':'bg_selectbox_form_close.png'});
	}
	
	if($('faq_select_model').length > 0) {
		$('.faqMainTheme').each(function(){
			this.style.display = "none";
		});
		
		//$('#faq_select').selectbox();
		$('#faq_select').selectbox({'bgImageActiv':'bg_selectbox_open.png','bgImage':'bg_selectbox_close.png'});
		
		$('#faq_select_container ul li').bind('click', function() {
			$('.faqMainTheme').each(function(){
				if(this.style.display != "none") {
					$(this).hide();
				}
			});
			$('#faq_' + this.id).show();
		});
	}
	
	
	//Display Instructions for Archive-Selectboxes (not needed, if javascript is deactivated)
	$('h2.archive_instructions').show();
	
	//Hide Archive-Content via JavaScript
	$('div.archive_content_box').hide();
	
	//Archive Selectboxes
	if($('select.archive') && $('select.archive').length > 0) {

		//Apply styled selectboxes
		$('select.archive').selectbox({'bgImageActiv':'bg_selectbox_open.png','bgImage':'bg_selectbox_close.png','inputLength':33});
		
		//Hide all selectboxes and show just the root
		$('input.selectbox').hide();
		$('input#structure_base_input').show();
		
		//Click-Handler for Selectboxes
		$('.selectbox-wrapper ul li').click(function() {
			var $parentDiv = $(this).parent().parent().parent().parent();
			$parentDiv.find('div.childselects input').hide();
			
			//Display child-select-box if present
			$parentDiv.find('div.childselects input#'+$(this).attr('id')+'_input').show();
			
			//Hide all contentboxes
			$('div.archive_content_box').hide();
			
			//Display child-content-box if present
			$('div#'+$(this).attr('id')+'_content').show();
			
			//Reset Default-Value
			var defaultValue = $parentDiv.find('div.childselects div#'+$(this).attr('id')+'_container ul li:first').html();
			$parentDiv.find('div.childselects input#'+$(this).attr('id')+'_input').val(defaultValue);
		});
	}

	if($('#ChangeImageBoxButton')) {
		$('#ChangeImageBoxButton').bind('click',  
			function(event) {
				event.stopPropagation();
				event.preventDefault();
			}
		);
	}
	if($('#ContactForm').length > 0) {
		$($('select[name=theme]')[0]).selectbox({'bgImageActiv':'bg_selectbox_form_open.png','bgImage':'bg_selectbox_form_close.png',onChangeCallback:contactFormThemeListener});
		$($('select[name=salutation]')[0]).selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png'});
		$($('select[name=title]')[0]).selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png'});
		jsForm();
		if(isIE6()){
			$(document).pngFix(); 
		}
	}

	function contactFormThemeListener(args)
	{
		if(args.selectedVal == 0 || args.selectedVal == 2)
		{
				
			$("#products input:checkbox").each(function() {
				$(this).attr('checked', false);
			});
			
			$('#product_search').slideUp();
			
			$('#products').slideDown();
			
			if(args.selectedVal == 2)
			{
				$("#products input:checkbox").click(function() {
					if((!$('#product_5').attr('checked') && $("#products input:checked").length > 0) || ($('#product_5').attr('checked') && $("#products input:checked").length > 1))
					{
						$('#product_search').slideDown();
					}
					
					if($('#product_5').attr('checked') && $("#products input:checked").length <= 1)
					{
						$('#product_search').slideUp();
					}
					
					if($("#products input:checked").length == 0)
					{
							$('#product_search').slideUp();
					}
				});				
			}
		}
		else
		{
			$('#products').slideUp();
		}
	}
	
	$('#product_search_field').keyup(function() {
		if($(this).val().length >= 4)
		{
			$('#product_search_link a').attr('href', $('#search_link').attr('href') + $('#product_search_field').val());
			$('#product_search_link').slideDown();
			//alert($('#product_search_link').text().replace(/&searchterm&/, $('#product_search_field').val()));
		}
		else
		{
			$('#product_search_link').slideUp();
		}
	});
	
	if($('#RegistrationForm').length > 0) {
		$($('select[name=industrialsector]')[0]).selectbox({'bgImageActiv':'bg_selectbox_form_open.png','bgImage':'bg_selectbox_form_close.png'});
		$($('select[name=salutation]')[0]).selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png'});
		$($('select[name=title]')[0]).selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png'});
		jsForm();
		if(isIE6()){
			$(document).pngFix(); 
		}
	}
	if($('#CustomerSatisfactionForm').length > 0) {
		$($('select[name=salutation]')[0]).selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png'});
		$($('select[name=title]')[0]).selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png'});
		jsForm();
		if(isIE6()){
			$(document).pngFix(); 
		}
	}
	if($('#ServiceForm').length > 0) {
		$($('select[name=salutation]')[0]).selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png'});
		$($('select[name=title]')[0]).selectbox({'bgImageActiv':'bg_image_container_selectbox_open.png','bgImage':'bg_image_container_selectbox_close.png'});
		jsForm();
		if(isIE6()){
			$(document).pngFix(); 
		}
	}
	if($('form[name=login]').length > 0) {
		$('#NoJsFormLoginSubmit').hide();
		$('#JsFormLoginSubmit').show();
	}
	if($('form[name=logout]').length > 0) {
		$('#NoJsFormLogoutSubmit').hide();
		$('#JsFormLogoutSubmit').show();
	}
	if($('form[name=password]').length > 0) {
		$('#NoJsFormSubmit').hide();
		$('#JsFormSubmit').show();
	}
	if($('form[name=changepassword]').length > 0) {
		$('#NoJsFormSubmit').hide();
		$('#JsFormSubmit').show();
		jsForm();
	}
	if($('form[name=newpassword]').length > 0) {
		$('#NoJsFormSubmit').hide();
		$('#JsFormSubmit').show();
	}
	if($('#NewsletterForm').length > 0) {
		$('#NoJsNewsletterFormSubmit').hide();
		$('#JsNewsletterFormSubmit').show();
	}
	if($('form[name=search]').length > 0) {
		$('#NoJsFormSearchSubmit').hide();
		$('#JsFormSearchSubmit').show();
	}
	if($('form[name=installersearch]').length > 0) {
		$('#NoJsFormInstallerSearchSubmit').hide();
		$('#JsFormInstallerSearchSubmit').show();
	}
	if($('form[name=loginimagebox]').length > 0) {
		$('#NoJsFormLoginImageBoxSubmit').hide();
		$('#JsFormLoginImageBoxSubmit').show();
	}
	if($(".tf_input")) {
		$(".tf_input").bind("focus", function(e){
			e.target.value = "";
//			$(e.target).unbind("focus");
		});
	}

	
});

function jsForm() {
	try {
		$('#NoJsFormSubmit').hide();
		$('#JsFormSubmit').show();
		$('#Antibot').hide();
	} catch(e) {
	}
}

/**
 * Styles the language-chooser
 * 
 * For SEO-Purposes made as UL, this method
 * will display the language-chooser as selectbox.
 * 
 * @return null
 */
function styleLanguageSelect()
{	
	$('#languageselect').removeClass('language');
	$('#languageselect').addClass('language_box');
	
	mouseover = function()
	{
		$('#languageselect li').show();
		$('#languageselect').addClass('active');
	}
	
	mouseout = function()
	{
		$(this).find('li').hide();
		$(this).find('li.active').show();
		$(this).removeClass('active');
	}
	
	$('#languageselect li:first').mouseover(mouseover);
	$('#languageselect').hover(null,mouseout);
	
	//Disable click for active locale
	$('#languageselect li.active a').click(function(){return false;});
	
	
	
}


function applyFaqSliders()
{
	$('ul.faqList > li').addClass('closed');
	$('ul.faqList > li .answer').hide();
	
	$('ul.faqList > li').click(function(){
		$(this).find('.answer').slideToggle('fast');
		$(this).toggleClass('closed');
	});
}


/**
 * Get another ImageBox and display it
 * 
 * @param inUrl String
 * @return
 */
function changeImageBox(inUrl) {
	if(!changeImageBoxActive && $('#image_container .background').length <= 1) {
		clearInterval(imageBoxInterval);
		changeImageBoxActive = true;
		jQuery.getJSON(inUrl + "&json=true&imageId=" + ($('#image_container .background')[0].id).split("img")[1], function(data){
			var theImage = $($('#image_container .background')[0]);
			var theNewImage = theImage.clone(true).hide().insertAfter(theImage);
			theImage.fadeOut(1000,function(){$($('#image_container .background')[0]).remove()});
			theNewImage[0].src = data.imageSrc;
			theNewImage[0].id = "img" + data.id;
			$(theNewImage[0]).fadeIn(1000);
			//$('.image_container_detail_link a')[0].href = data.detailLink;
			clearInterval(imageBoxInterval);
			imageBoxInterval = window.setInterval("changeImageBox(imageBoxIntervalLink)", intervalImageBox);
			changeImageBoxActive = false;
		});
	}
	return false;
}

$(document).ready(function(){
	/**
	 * right content height bugfix
	 */
	function fixRCHeight() {
		var leftHeight = $($(".body .content_left")[0]).height();
		var rightHeight = $($(".body .content_right")[0]).height();
		if(leftHeight > rightHeight) {
			$($(".body .content_right")[0]).css('height', leftHeight+'px');
		}
	}
	if($('.sitemap').length < 1)
		fixRCHeight();
	
	
	if(isIE6() || isIE7()) {
		if($('IE6SearchButton') != null) {
			$('IE6SearchButton').hide();
		}
	}
	
	$(".article_lightbox").lightbox({'strings':{'closeTitle':localization.lightBoxCloseTitle}});
	
	if($('#installerSearchZip').length > 0 || $('#installerSearchCity').length > 0) {
		$('#countryselect_container ul li').bind('click', function(e){
			if(e.target.id == "de") {
				$('#installerSearchZip').slideDown();
				$('#installerSearchCity').slideDown();
				$('#installerSearchProductLines').slideDown();
			} else {
				$('#installerSearchZip').slideUp();
				$('#installerSearchCity').slideUp();
				$('#installerSearchProductLines').slideUp();
			}
		});
	}
});
$(document).ready(function(){ 
	$(document).pngFix(); 
});
