/////////////////////////////////////////// BASIC FUNCTIONS, DON'T EDIT ///////////////////////////////////////////

// Starts loading these functions on loading the page:
window.addEvent('domready', function() {
									
	// Enables the JS errorconsole when on the internal testing area:
	if (document.location.toString().match('ontwikkelding')) {
		enableErrorConsole = true;
	} else {
		enableErrorConsole = false;
	}
	
	// Define wether to use Firebug as the JS debugger (true) or the custom errorconsole (false):
	useFireBugAsErrorConsole = true;
	
	if (window.ie) {
		useFireBugAsErrorConsole = false;
	}
	if (useFireBugAsErrorConsole == false) {
		initConsole();
	}
	
	init();
});

function init() {
	initRemoveLinkLines();
	initExternalLinks();
	initCustomFunctions();
}



// Check for Internet Explorer for Mac:
var IEMAC = (navigator.userAgent.indexOf('Mac') != -1 && navigator.userAgent.indexOf('MSIE') != -1);
// Check for browser supporting W3CDOM:
var W3CDOM = (document.createElement && document.getElementsByTagName && !IEMAC);

// Error console:
if (W3CDOM) {
	function initConsole() {
		if (enableErrorConsole) {
			var console = document.createElement('div');
			console.id = 'errorConsole';
			var errorConsoleTitle = document.createElement('h2');
			errorConsoleTitle.innerHTML = 'JavaScript Error Console:';
			console.appendChild(errorConsoleTitle);
			var errorList = document.createElement('ol');
			errorList.id = 'errorList';
			console.appendChild(errorList);
			document.body.appendChild(console);
		}
	}
	function error(message) {
		if (enableErrorConsole && !window.ie && useFireBugAsErrorConsole == true) {
			window.console.log(message);
		} else if (enableErrorConsole) {
			var newMessage = document.createElement('li');
			newMessage.innerHTML = message;
			var console = document.getElementById('errorList');
			console.appendChild(newMessage);
		}
	}
}

// This function is used in order for certain links to open in a new window without the direct target attribute:
function initExternalLinks() { 
	if (!document.getElementsByTagName) return; 
	var anchors = document.getElementsByTagName("a"); 
	for (var i=0; i<anchors.length; i++) { 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}

// This function removes the dotted lines around links, but breaks the Tab button on the keyboard: less accessability:
function initRemoveLinkLines() {
	$$('a').addEvent('focus', function(){this.blur()});
}





/////////////////////////////////////////// EDIT CUSTOM JAVASCRIPT BELOW THIS LINE ///////////////////////////////////////////

// Add all your custom function which need to be initialized to this function:
function initCustomFunctions() {
	generateHomepageCarrousel();
	keepLoggedInCheckbox();
	initFaqAccordion();
	mySpykerTabNavigation();
	subCategorySlidePanels();
}

// Generate the Flash Carrousel on the homepage:
function generateHomepageCarrousel() {
	if ($('carrousel')) {
		var so = new SWFObject("../flash/carrousel/carrousel.swf", "movie", "639", "400", "8", "#ffffff");
		so.addParam("wmode", "transparent");
		so.addVariable("xmlURL", "../flash/carrousel/icons.xml");
		so.write("carrousel");
	}
}

// Checkbox interaction setting the value depending on its checked-state:
function keepLoggedInCheckbox()  {
	if ($('keep_logged_in')) {
		$('keep_logged_in').addEvent('click', function(){
			if ($('keep_logged_in').checked) {
				$('keep_logged_in').value = 'keep_me_logged_in';
			} else {
				$('keep_logged_in').value = 'log_out_after_session';
			}
		});
	}
}

// Initializes the accordion used on the FAQ page:
function initFaqAccordion() {
	if ($('faq_accordion')) {
		var myClickables = $ES('.clickable', 'faq_accordion');
		var myHideables = $ES('.hideable', 'faq_accordion');
		
		var myAccordion = new Accordion(myClickables, myHideables, {
			opacity : true,
			openClose : true,
			show : 0,
			onActive : function(el){openPanel(el)},
			onBackground : function(el){closePanel(el)},
			display : false,
			alwaysHide : true
		});
	}
}

// Action while opening a FAQ accordion panel:
function openPanel(thisEl) {
	thisEl.getParent().getParent().addClass('opened');
	error(thisEl.getParent().getParent());
}

// Action while closing a FAQ accordion panel:
function closePanel(thisEl) {
	thisEl.getParent().getParent().removeClass('opened');
	error(thisEl.getParent().getParent());
}

// Defines the navigation of the tabs on the My Spyker page:
function mySpykerTabNavigation() {
	if($('my_spyker_nav')) {
		var mySpykerNavListItems = $ES('li', 'my_spyker_nav');
		$E('a', 'favorites').addEvent('click',function() {
			mySpykerNavListItems.each(function(mySpykerNavListItem){mySpykerNavListItem.removeClass('active')});
			mySpykerNavListItems[0].addClass('active');
			$('my_spyker_favorites').removeClass('hide_my_spyker_panel');
			$('my_spyker_profile').addClass('hide_my_spyker_panel');
			$('my_spyker_order_history').addClass('hide_my_spyker_panel');
		});
		$E('a', 'profile').addEvent('click',function() {
			mySpykerNavListItems.each(function(mySpykerNavListItem){mySpykerNavListItem.removeClass('active')});
			mySpykerNavListItems[1].addClass('active');
			$('my_spyker_favorites').addClass('hide_my_spyker_panel');
			$('my_spyker_profile').removeClass('hide_my_spyker_panel');
			$('my_spyker_order_history').addClass('hide_my_spyker_panel');
		});
		$E('a', 'order_history').addEvent('click',function() {
			mySpykerNavListItems.each(function(mySpykerNavListItem){mySpykerNavListItem.removeClass('active')});
			mySpykerNavListItems[2].addClass('active');
			$('my_spyker_favorites').addClass('hide_my_spyker_panel');
			$('my_spyker_profile').addClass('hide_my_spyker_panel');
			$('my_spyker_order_history').removeClass('hide_my_spyker_panel');
		});
	}
}

// Defines the interaction of the panels with submenus which slide out when clicked:
function subCategorySlidePanels() {
	var catSlides = $$('.cat_slide');
	var catSlideCount = 0;
	var catSlideZIndex;
	var slidePanelOffSet = -155;

	catSlides.each(function(catSlide){
		catSlideCount++;
		catSlideZIndex = (catSlides.length - catSlideCount) + 1;
		catSlide.setStyle('zIndex', catSlideZIndex);

		var subcatPanel = $E('.subcat_panel', catSlide);
		var subcatTab = $E('.subcat_tab', subcatPanel);
		subcatTab.addEvent('click',function(){
			catSlides.each(function(thisCatSlide){
				var thisSubcatPanel = $E('.subcat_panel', thisCatSlide);
				
				if (thisCatSlide != catSlide) {
					var thisSubcatPanelFX = new Fx.Styles(thisSubcatPanel, {duration: 1000, wait: true, transition: Fx.Transitions.Cubic.easeInOut});

					thisCatSlide.removeClass('active');
					
					if (thisSubcatPanel.hasClass('opened')) {
						thisSubcatPanelFX.start({'marginLeft': [0,slidePanelOffSet]}).chain(function(){
							thisSubcatPanel.removeClass('opened');
						});
					}
				}
			});
			
			var fx = new Fx.Styles(subcatPanel, {duration: 1000, wait: true, transition: Fx.Transitions.Cubic.easeInOut});
			
			if (subcatPanel.getStyle('marginLeft') != (slidePanelOffSet + 'px')) {
				catSlide.removeClass('active');
				fx.start({'marginLeft': [0,slidePanelOffSet]}).chain(function(){
					subcatPanel.removeClass('opened');
				});
			} else {
				catSlide.addClass('active');
				fx.start({'marginLeft': [slidePanelOffSet,0]}).chain(function(){
					subcatPanel.addClass('opened');
				});
			}
		});
	});
}
