/**
 * Copyright Levi9 Global Sourcing 2009
 */

function switchAll(chk) {
	var checkAll = document.getElementById("searchall");
	for (var i=0; i<chk.length; i++) {
		if (chk[i].name!="searchall"){
			if (chk[i].checked && !checkAll.checked){
				chk[i].checked = false;
			}else if (!chk[i].checked && checkAll.checked){
				chk[i].checked = true;
			}
		}	
	}
}

function switchSearchAllOnOff(current){
	var checkAll = document.getElementById("searchall");
	if(!current.checked && checkAll.checked){
		checkAll.checked = false;
	}
}
function toggleDynamic() {
	titleDynamicDiv = document.getElementById("dynamic");
	$(titleDynamicDiv).toggleClass("opened");
	dynamicDivContent = document.getElementById("dynamic_content");
	$(dynamicDivContent).toggle("slow");
}

/**
 * Utility function to get query from URL.
 * @param name of query string to find
 * @return value of specific name from query string
 */
function getQueryStringFromUrl(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	return results == null ? -1 : parseInt(results[1]);
}

/**
 * Radi buttons juggling on zoekresultaten page.
 * @return
 */
function handleRadioButtons(idSuffix, parentId) {
	if (idSuffix == null) idSuffix = "";
	
	$("#dynamic" + idSuffix).addClass("search_result_detail_dynamic");
  	$("#dynamic_content" + idSuffix).hide();
	$("#bent_ja" + idSuffix).click(function() {
		dynamicDivContent = document.getElementById("dynamic_content" + idSuffix);
		var currentHeight = $(dynamicDivContent).height();
		$('#bent_ja_container' + idSuffix).removeClass();
		$('#bent_ja_container' + idSuffix).addClass('betekenen_input_radio_selected');
		$('#bent_nee_container' + idSuffix).removeClass();
		$('#bent_nee_container' + idSuffix).addClass('betekenen_input_radio');
		$("#selected_nee" + idSuffix).css({
			"display": "none" 
		});			
		$("#selected_ja" + idSuffix).css({
			"display": "block"						
		});
		recalculateParentHeight(idSuffix, parentId, currentHeight);
		trackClickWithGoogleAnalytics("yes");
	});
	$("#bent_nee" + idSuffix).click(function() {
		dynamicDivContent = document.getElementById("dynamic_content" + idSuffix);
		var currentHeight = $(dynamicDivContent).height();
		$('#bent_ja_container' + idSuffix).removeClass();
		$('#bent_ja_container' + idSuffix).addClass('betekenen_input_radio');
		$('#bent_nee_container' + idSuffix).removeClass();
		$('#bent_nee_container' + idSuffix).addClass('betekenen_input_radio_selected');
		$("#selected_nee" + idSuffix).css({
			"display": "block" 
		});			
		$("#selected_ja" + idSuffix).css({
			"display": "none"						
		});
		recalculateParentHeight(idSuffix, parentId, currentHeight);
		trackClickWithGoogleAnalytics("no");
	});
}

function trackClickWithGoogleAnalytics(eventName){
	if (!notAUserClick){
		pageTracker._trackEvent("Call to action click", location.pathname, eventName);
	}
}

function recalculateParentHeight(idSuffix, parentId, previousHeight){
	if (parentId != null && parentId != ''){
		dynamicDivContent = document.getElementById("dynamic_content" + idSuffix);
		
		var currentHeight = $(dynamicDivContent).height();
		if (currentHeight == null){
			currentHeight = 0;
		}
		var parentHeight = $("#"+parentId).height();
		
		var difference = currentHeight - previousHeight;
		var animationSpeed = 500;
		var newHeight = parentHeight + difference;
		$("#" + parentId).animate({height: newHeight + "px"}, animationSpeed);			
	}
}

function toggleDynamic(idSuffix, parentId) {
	var calculateParentHeight = parentId != null && parentId != '';
	
	titleDynamicDiv = document.getElementById("dynamic" + idSuffix);
	dynamicDivContent = document.getElementById("dynamic_content" + idSuffix);
	
	if (calculateParentHeight){
		var isOpened = $(titleDynamicDiv).hasClass("opened");
		var dynamicBoxHeight = $(dynamicDivContent).height();
		var parentHeight = $("#" + parentId).height();
		var animationSpeed = 500;
		var newHeight = null;
		if (isOpened){
			newHeight = parentHeight - dynamicBoxHeight;
		}else{
			newHeight = parentHeight + dynamicBoxHeight;
		}
		$("#" + parentId).animate({height: newHeight + "px"}, animationSpeed);
	}
	
	$(titleDynamicDiv).toggleClass("opened");
	$(dynamicDivContent).toggle("slow");
}
 
function loadCssFile(path){
	var fileref=document.createElement("link");
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", path);
	if (typeof fileref!="undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);	
	}
}

function loadAccordion(index) {
	$("#sub_navigation_2").accordion({ collapsible: true, autoHeight: false});
	if(index != 0) {
		$("#sub_navigation_2").accordion('activate',index);
	}
	
	$("#sub_navigation_1 a").click(function(){
		$("#sub_navigation_2").hide();
	})
 	if($("#sub_navigation_2").hasClass('active')) {
 		$("#sub_navigation_2").slideUp('fast').removeClass('active');
	} else {
		$("#sub_navigation_2").slideDown('fast').addClass('active');
	}
}

function getBrowserVersion() {
	return $.browser.version;
}

function isIe6() {
	return ($.browser.msie && this.getBrowserVersion() == '6.0');
}

/**
* Handle home page category menu
*/
function handleHomePageCategoryElementMenu() {
	$('.homepage-navigator-button').hover(
			function(){
				$(this).css("cursor","pointer");
				$('#' + this.id + "_menu").slideDown('fast');
			},
			function(){
				$('#' + this.id + "_menu").slideUp('fast');
			});
}

function handleHintForSearchField(fieldId) {
	$('#' + fieldId).val($('#' + fieldId).attr('title')).addClass('blur');
	$('#' + fieldId).focus(function() {
			// See if the input field is the default
			if ($(this).val() == $(this).attr('title')) {
				$(this).val('');
				$(this).removeClass('blur');
			}
		}).blur(function() {
			if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
			$(this).addClass('blur');
		}
	});
}

function handleEnterButton(e, sendButtonId) {
	if(e.keyCode == 13) {
		$("#" + sendButtonId).click();
		return false;
	}
}

function getSelectedSection() {
	return $.trim($("div#headerMainMenu ul li.selected a").text());
}

function getSelectedCategory() {
	return $("div.left_menu a.selected").parent().parent().parent().prev().text();
}

function getSelectedSubcategory(){
	return $("div.left_menu a.selected").text();
}

function writeSection() {
	var section = this.getSelectedSection();
	$.each($("input.c_section"),function(){
		$(this).val(section);
	});
}

function writeCategory() {
	var category = this.getSelectedCategory();
	$.each($("input.c_category"),function(){
		$(this).val(category);
	});
}

function writeSubCategory() {
	var subcategory = this.getSelectedSubcategory();
	$.each($("input.c_subcategory"),function(){
		$(this).val(subcategory);
	});
}

function reduceSectionsToOneSection() {
	if ($("input.c_section") != null){
		var sections = $("input.c_section").val();
		if (sections != null && sections != undefined){
			var sectionsArray = sections.split(",");
			if (sectionsArray != null){
				$("input.c_section").val($.trim(sectionsArray[0]));
			}
		}
	}
}

function reduceCategoriesToOneCategory() {
	if ($("input.c_category") != null){
		var categories = $("input.c_category").val();
		if (categories != null){
			var categoryArray = categories.split(",");
			if (categoryArray != null){
				$("input.c_category").val($.trim(categoryArray[0]));
			}
		}
	}
}

function reduceSubcategoriesToOneSubcategory() {
	if ($("input.c_subcategory") != null){
		var subcategories = $("input.c_subcategory").val();
		if (subcategories != null){
			var subcategoryArray = subcategories.split(",");
			if(subcategoryArray != null){
				$("input.c_subcategory").val($.trim(subcategoryArray[0]));
			}
		}
	}
}

function prepareDataForCopernica() {
	this.reduceSectionsToOneSection();
	this.reduceCategoriesToOneCategory();
	this.reduceSubcategoriesToOneSubcategory();
}