/**************************************************
Document: fonction.js
Version:  2.0
Rev.:     1
Date:     Mar-10 2011
***************************************************/

// SHOW HIDE LAYER AVAILABLE PRODUCTS
function expandContent(id) {
	if (document.getElementById(id).style.display == 'none') {
		document.getElementById(id).style.display = 'block';
		if(document.getElementById("titles_block"))document.getElementById("titles_block").style.display = 'none';
	} else {
		document.getElementById(id).style.display = 'none';
		if(document.getElementById("titles_block"))document.getElementById("titles_block").style.display = 'block';
	}
}

// TOGGLE ADVANCED SEARCH
function change() {
	if (document.getElementById("showhide").style.display == "none") {
		document.getElementById("showhide").style.display = "inline";
		document.getElementById("thetext").innerHTML = "Simple search";
	} else {
		document.getElementById("showhide").style.display = "none";
		document.getElementById("thetext").innerHTML = "Advanced search";
	}
}

// OLS: scrolling tabs
jQuery(function(){
	//Get our elements for faster access and set overlay width
	var div = jQuery('div#titles_block'),
		ul = jQuery('ul#titles'),
		ulPadding = 15;
	
	//Get menu width
	var divWidth = div.width();

	//Remove scrollbars	
	div.css({overflow: 'hidden'});
	
	//Find last image container
	var lastLi = ul.find('li:last-child');
	
	//When user move mouse over menu
	div.mousemove(function(e){
		//As text is loaded ul width increases,
		//so we recalculate it each time
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;	
		var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		div.scrollLeft(left);
	});
});

/**
 * Resize the product highlights so they all have the same height (stretched to the tallest)
 * Attached to the window.load event because in FF the ready event was too early.
 */
jQuery(window).load(function() {
	var tallest = 0;
	jQuery(".block_productsHighlights").each(
		function(){
			var h =jQuery(this).height();
			if(h > tallest) tallest = h;
		}
	);
	jQuery(".block_productsHighlights").height(tallest);
});


/* KaBox v 1.0.0 */
/* Jun 2009 Kahiloa Solutions et Communication */
/* Nikon Europe Project */

// FadeIn Effect

function fadeIn(objId,opacity) {
	if(document.getElementById(objId)){
		if (opacity <= 100) {
			setOpacity(objId, opacity);
			if (document.all) {
				opacity += 20;
			}			
			else {
				opacity += 10;
			}
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);
		}
	}
}

function setOpacity(obj, opacity) {
	if (document.all) {
		opacity = ((opacity == 100)?99.999:opacity) - 25;
	} else {
		opacity = ((opacity == 100)?99.999:opacity) - 60;
	}
	// IE 
	document.getElementById(obj).style.filter = "alpha(opacity="+opacity+")";
	// Older Safari, Konqueror
	document.getElementById(obj).style.KHTMLOpacity = opacity/50;
	// Older Firefox
	document.getElementById(obj).style.MozOpacity = opacity/50;
	// Safari, newer Firefox, CSS3
	document.getElementById(obj).style.opacity = opacity/50;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll);
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function overlayProductView(ImgSrc, elWidth, elHeight, elColor) {
	var KaBoxPosition = document.documentElement.scrollTop;

	// Global container
	var ImageContainer = document.createElement('div');
	ImageContainer.setAttribute('id', 'ContainerImg');
	
	if (document.all) {
		ImageContainer.style.cssText = 'background-color:'+ elColor + '; top:'+ KaBoxPosition +'px';
	} else {
		ImageContainer.setAttribute('style', 'background-color:'+ elColor + '; top:'+ KaBoxPosition +'px');
	}
	
	var pageSizeArr = getPageSize();
	var pageScrollArr = getPageScroll();
	// Create image
	var ImgContent = document.createElement('img');
	var WindowHeight = pageSizeArr[1];
	
	var x = Math.ceil((WindowHeight - elHeight) / 2);
	y = ((pageSizeArr[3])/2) + pageScrollArr[1] - elHeight/2; 
	if(y < 0) {
		y = 0;
	}	
	if (document.all) {
		ImgContent.style.cssText = 'background-color:#fff; margin-left:-'+ elWidth/2 +'px ; top:'+ y +'px;' + KaBoxPosition + 'px';
	} else {
		ImgContent.setAttribute('style', 'background-color:#fff; margin-left:-'+ elWidth/2 +'px ; top:'+ y +'px; height:'+ elHeight +'px; width:'+ elWidth +'px');
	}
	
	ImgContent.setAttribute('src', ImgSrc);
	ImgContent.setAttribute('id', 'ContainerImg2');
	ImgContent.setAttribute('width', elWidth);
	ImgContent.setAttribute('height', elHeight);	
	
	// Including elements
	document.body.appendChild(ImgContent);
	document.body.appendChild(ImageContainer);
	document.getElementById("ContainerImg").style.height = (arrayPageSize[1] + 'px');
	fadeIn('ContainerImg', 0); setOpacity('ContainerImg', 0);
	
	// Hiding body scroll bars
	document.body.style.overflow='hidden';
	
	// Removing box
	function removeBox() {
		document.body.style.overflow='visible';
		document.body.removeChild(ImageContainer);
		document.body.removeChild(ImgContent);
	}
	
	ImageContainer.onclick = removeBox;
}

/* 
*** GA ASYNC Event Tracker - Mar. 2011 ***
*/
var trackMyEvent = function (category, action, optlabel, optvalue) { 
//	alert('The call has been received with the following values:\nCategory: ' + category + '\nAction: ' + action + '\nLabel: ' + optlabel + '\nValue: '+  optvalue);
  	try {
  		_gaq.push(['_trackEvent', category, action, optlabel, optvalue]);
  		return true;
	} catch(err) { return false }
}

/* 
*** GA ASYNC Outbound Event Tracker - Mar. 2011 ***
*/
var trackOutboundEvent = function (link, category, action, optlabel, optvalue) {
	try {
		_gaq.push(['_trackEvent', category, action, optlabel, optvalue]);
		setTimeout('document.location = "' + link.href + '"', 100);
		return true;
	} catch(err){ return false }
}

		
		
jQuery(document).ready(function() {

	jQuery("#productRegistration").click(function() {
	  // Figure the current locale by look into the href attribute
	  var location = window.location.href;
	  var locale = location.split("/")[3];
	  trackMyEvent('prodregtab_product_eu','goto_prod_registration',locale + '_ver1');
	  _gaq.push('_link', this.href);
	});

});
