$(window).ready(function(){
	replaceMainmenuBtns();
});

// onload handler 
$(window).load(function(){
	replaceHeaders();
	setupSearch();
	if ($('#globalNav')[0]) { 
		setupTabs(); // for dietisten productinfo
	}
	xidAccordion("xidAccordion");
	setMainmenuActiveState();
	
});


// activate current chapter in main menu
function setMainmenuActiveState(){	
	switch (getCurrentChapter()){
		case "producten":
			$("#mm_producten").addClass("buttonactive");
			break;
		case "nieuws":
			$("#mm_nieuws").addClass("buttonactive");
			break;
		case "voeding_en_gezondheid":
			$("#mm_voeding_en_gezondheid").addClass("buttonactive");
			break;
		case "historie":
			$("#mm_historie").addClass("buttonactive");
			break;
		case "faq":
			$("#mm_faq").addClass("buttonactive");
			break;
		case "contact":
			$("#mm_contact").addClass("buttonactive");
			break;
		case "zoeken":
			$("#mm_zoeken").addClass("searchbuttonactive");
			break;
	}
}


// setup search opening & closing & submitting
function setupSearch(){
	$("#search").hide();
	var opened = false;
	
	$(".search").click(function() {
		if (!opened){
			$("#search").show(600);
			$(".koekContainer").addClass("koekContainerFix");
			$("#mm_zoeken").addClass("searchbuttonactive");
		} else {
			$("#search").hide(600);
			$(".koekContainer").removeClass("koekContainerFix");
			$("#mm_zoeken").addClass("search");
		}
		opened = !opened;
	});
	
	$("#search div img").click(function() {
		$("#searchform").submit();									   
	});
}

// get the current chapter and page from the url
function getCurrentChapter(){
	var urlPath = location.pathname;
	var urlPathArray = urlPath.split('/');
	var currentChapter = urlPathArray[urlPathArray.length - 2];
	return currentChapter;
}


// replaces mainmenu text with flash content
function replaceMainmenuBtns(){
	$("#mainMenu").find(".button span").each(function(i) {
		var txt = this.innerHTML;
		var lnk = this.parentNode;
		var width = $(this).parents("a").width();
		menuitem = new SWFObject(rootpath + "swf/text/mainMenu.swf", "button", width, "25", "6", "#ffffff");
		menuitem.addVariable("txt", txt);
		menuitem.addVariable("lnk", lnk);
		
		// check if this is the current chapter
		if (this.parentNode.id == "mm_" + getCurrentChapter()){
			menuitem.addVariable("active", "true");
		} else {
			menuitem.addVariable("active", "false");
		}
		menuitem.addParam("wmode", "transparent");
		menuitem.addParam("quality", "best");
		menuitem.write(this);
	});
}	

// replaces header text with flash content
function replaceHeaders(){
	$("h1").each(function(i) {
		var txt = this.innerHTML;
		col = ($(this).css("color"));
		col = col = format_colour(col);
		col = col.substring(1, 7);
		h1 = new SWFObject(rootpath + "swf/text/h1.swf", "h1", "615", "40", "6", "#ffffff");
		h1.addVariable("txt", txt);
		h1.addVariable("color", col);
		h1.addParam("wmode", "transparent");
		h1.addParam("quality", "best");
		h1.write(this);
	});
}

// dietisten product menu
var tabs;
function setupTabs(){
	tabs = new Array('bkv', 'mb', 'eg', 'fk', 'continue', 'yb');
	for(var q = 0; q < tabs.length; q++) {
		$("#" + tabs[q]).click(function() {
			showProduct(this.id);
			this.blur();
		});
	}
	// showProduct('bkv'); // initial tab
}
function showProduct(prod){
	for (var q = 0; q < tabs.length; q++) {
		if (prod == document.getElementById(tabs[q]).id){
			$("#" + tabs[q]).attr("class", "active");
			$("#productinfo").load("productinfo/" + tabs[q] + ".php", null, ajaxLoadReady);
		} else {
			$("#" + tabs[q]).attr("class","inactive");
		}
	}
}
function ajaxLoadReady(){
	$(".pInfo").accordion({
		active: false,
		header: '.accordion',
		selectedClass: 'accordion_active',
		navigation: false,
		alwaysOpen: false
	});
}



// COLOR TOOLS

// Formats colours (unifies colors to hex)
function format_colour(colour){
	var returnColour = "#ffffff";
	
	// Make sure colour is set and not transparent
	if(colour != "" && colour != "transparent"){
		// RGB Value?
		if(colour.substr(0, 3) == "rgb"){
			// Get HEX aquiv.
			returnColour = rgb2hex(rgb2Array(colour)[0], rgb2Array(colour)[1], rgb2Array(colour)[2]);
		} else if (colour.length == 4 && colour.charAt(1) == "#") {
			// 3 chr colour code add remainder
			returnColour = "#" + colour.substring(1, 2) + colour.substring(1, 2) + colour.substring(2, 3) + colour.substring(2, 3) + colour.substring(3, 4) + colour.substring(3, 4);
		} else {
			// Normal valid hex colour
			returnColour = colour;
		}
	}
	return returnColour;
}


// Returns an array of rbg values
function rgb2Array(rgbColour){
	// Remove rgb()
	var rgbValues = rgbColour.substring(4, rgbColour.indexOf(")"));
	
	// Split RGB into array
	var rgbArray = rgbValues.split(",");
	
	return rgbArray;
}


// Convert rgb to hexadecimal
function rgb2hex(red, green, blue){
	var hex = new Array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
	var rgb = new Array();
	var k = 0;
	for (var i = 0; i < 16; i++) {
		for (var j = 0; j < 16; j++) { 
			rgb[k] = hex[i] + hex[j]; 
			k++;
		}
	}
 	return('#' + rgb[Number(red)] + rgb[Number(green)] + rgb[Number(blue)]);
}