/**
 *	Akzo Nobel portal
 *	--------------------------
 */


(function($) {
	
	var AkzoNobel = {
		// init, called onload of the dom
		init:function() {

			// domenabled
			$('body').addClass('domenabled');


			var toggleSelects = function(toggle){
				if(/msie 6/i.test(navigator.userAgent)) {
					$('select').css({
						visibility: toggle? 'visible':'hidden'
					});
				}
			}
			
			//randomize the icons.
			var dtList = new Array();
			var ddList = new Array();
			$("#brands dl dt").each(function(index){
			
				dtList[index] = $(this).html();
				ddList[index] = $("#brands dl dt:eq("+index+")").next().html();
			});
			
			$("#brands dl dt").each(function(index){
				var rndNum = Math.ceil(Math.random() * dtList.length - 1);
				$(this).html(dtList[rndNum]);
				$("#brands dl dt:eq("+index+")").next().html(ddList[rndNum]);
				dtList.splice(rndNum, 1);
				ddList.splice(rndNum, 1);
			});
			//$("#brands").css("display", "block");
			//$("#brands").show("slow");
			
			
			$("#brands dl dt a").mouseover(function(){ 
				var oldSrc = $(this).find("img").attr('src');
				var newSrc = brandsCol[findIndexOf(oldSrc,brandsBW)];
				$(this).find("img").attr('src', newSrc);
			}).mouseout(function(){ 
				var oldSrc = $(this).find("img").attr('src');
				var newSrc = brandsBW[findIndexOf(oldSrc,brandsCol)];
				$(this).find("img").attr('src', newSrc);
			});

			// initialize navigation
			$('#navigation #nav_main').simpleMenu({
				menuType:		'div',
				switchDelay:	250,
				
				// custom toggle, uses fades
				toggleMenu: function(item, toggle) {
					var list = $(this.menuType + '.nav_sub:first', item);
					list[toggle? 'show' : 'hide']();

					if(toggle && this.item) { 
						OverlayManager.toggle(this.root, true);
					} 
					
					if(!toggle && !this.item) { 
						OverlayManager.toggle(this.root, false);
					}

					toggleSelects(!toggle);
					$(item)[toggle? 'addClass' : 'removeClass'](this.activeClass);
				}
			});


			// initialize cases
			$('#showcase-selector').showcase({
				viewer: '#showcases',
				player: '#video-player',
				flash:	'CaseVideoApp'
			});
			
			// initalize brands
			if (document.getElementById("brands")) {
				$('#brands').iconList();
			}
			
			// initialize secitons
			if (document.getElementById("sections")) {
				$('#sections ul').sectionList();
			}

			// rounded corners
		    this.roundElements(document);
			
			// Close changeWebsiteLink selector
			$("#changeWebsiteLink").toggle(function() {
				$(this).addClass('open').next('#nav_global_container').fadeIn()
			}, function() {
				$(this).removeClass('open').next('#nav_global_container').fadeOut()
			});
			
			// Close changeWebsiteLink via close button
			$("#closesiteselectorbutton").click(function() {
				$("#changeWebsiteLink").click();
			});
			
			$(".list_links li").hover(function() {
				$(this).addClass('hover');
			}, function() {
				$(this).removeClass('hover');
			});

			$(".list_links li").click(function(){
				window.open($(this).find("a").attr("href"), $(this).find("a").attr("target"));
				return false;
			});
		},

		roundElements:function(context) {
			$('div.rounded', context).prepend(
				'<span class="top"></span>'
			).append(
				'<span class="btm"></span>'
			);
		}
	}

	if(!window.console) {
		window.console = {
			log:function(){},
			info:function(){},
			debug:function(){}
		}
	}


	var msie = /msie (6|7|8)/i.test(navigator.userAgent);
	$.fn.nonIEfadeIn = function() {
		if(msie) {
			// this.show();
			this.each(function(){
				$(this).css({height: this.parentNode.offsetHeight+'px'}).show();
			})
		} else {
			this.fadeIn.apply(this, arguments);
		}
		return this;
	}

	$.fn.nonIEfadeOut = function() {
		if(msie) {
			this.hide();
		} else {
			this.fadeOut.apply(this, arguments);
		}
		return this;
	}
	
	window.OverlayManager = {
		msie: /msie (6|7)/i.test(navigator.userAgent),
		zIndex:11,
		origins: [],
		toggle:function(node, toggle, z) {
			var origin = this.request(node);
			origin.active = toggle;

			var active = false;
			for(var i=0; i<this.origins.length; i++) {
				if(this.origins[i].active) {
					active = true;
					break;
				}
			}

			if(toggle) {
				var overlays = $('.overlay');
				overlays.css({zIndex: z || this.zIndex});
				//Option to add time for fadein effect of overlay (in ms?)
				overlays.nonIEfadeIn();
			}

			if(!toggle && !active) {
				var overlays = $('.overlay');
				//Option to add time for fadeout effect of overlay
				overlays.nonIEfadeOut();
			}
		},

		request: function(node) {
			for (var i=0; i<this.origins.length; i++) {
				if(this.origins[i].node == node) {
					return this.origins[i];
				}
			}
			var origin = {
				node: node,
				active: false
			}

			this.origins.push(origin);
			return origin;
		}
	}
	
		// bind init to dom onload
	$(function(){
		AkzoNobel.init();
	});
	
})(jQuery);


/**
 *	Flash video functions
 *	--------------------------
 */

FlashVideo = {
	request:function(embed, url) {
		this.embed = embed;
		this.requestedVideo = url;
	},

	play:function(url) {
		console.log('play: '+url)
		this.embed.setVideoSource(url);
		this.embed.playVideo(url);
	},

	poll:function() {
		if(this.requestedVideo) {
			this.play(this.requestedVideo);
			this.requestedVideo = null;
		}
	}
}
 
// Dispatched when the state of the video-player changes.
function onVideoStatus() {
	var jsCall = "";
	
	for(var i = 0; i < arguments.length; i++) {
    	if(i>0) {
    		 jsCall += ", ";
    	}
     
		jsCall += "\'" + arguments[i] + "\'";
	}
	
	console.log("onVideoStatus(" + jsCall + ")");
}

// Dispatched when the size of the video-player changes.
function onWindowStatus(status) {
	var app = window.showCases;
	if(/close_window/i.test(status)) {
		app && app.handleClick(null, 'showcase-close');
	} else if(/window_full_screen/i.test(status)) {
		app && app.handleClick(null, 'showcase-fullscreen');
	}
}

// Will be called when the application has been initialized, and is ready to play a video.
function onApplicationStatus(status) {
	if(/complete/i.test(status)) {
		FlashVideo.poll();
	}
}

// Define your custom statistics tracker function here.
function trackerFunction() {
	var jsCall = "";
	
	for(var i = 0; i < arguments.length; i++) {
    	if(i>0) {
    		 jsCall += ", ";
    	}
     
		jsCall += "\'" + arguments[i] + "\'";
	}
	
	//alert("trackerFunction(" + jsCall + ")");
}

function findIndexOf(thisString,thisArray){
	for(var x=0; x<thisArray.length;x++){
		if(RegExp(thisArray[x]).test(thisString)){
		return x;
	}
}
}

$(document).ready(function() {

	$(".contentoverlay .close, .contentoverlayactivator").click(function(e) {
		ToggleContentOverlay();
	});	
});

function ToggleContentOverlay() {
	$(".contentoverlayback, .contentoverlay").toggle();
	//Initial check when showing page. If no cookie is set hide dropdown boxes to prevent IE6 error
		if($.browser.msie && $.browser.version == 6){
		$('#content_main select').toggle();
	}

}







