/*Variables globales*/
var count = -1;
/* chargement flash */
var flashvars = {}; 
var params = {wmode:'transparent',allowscriptaccess:'always',loop:'true'};
var attributes = {};

function zoomProduit(idZoom)
{
	$(idZoom).show('scale');
	return false;	
}

function createInputHidden(selector,name,value) {
	$(selector).append('<input type="hidden" name="' + name + '" value="' + value + '">');
}

var pgItemsPerPage = 10;

function handlePaginationClick(page_index, jq) {
	var new_content = '';
	var max_elem = Math.min((page_index+1) * pgItemsPerPage, $('#zsearchresulttab tr').length);
	$('#zsearchresultdisplay').empty();
	new_content = $('#zsearchresultheader tr').clone();
    $('#zsearchresultdisplay').append(new_content);
	for(var i=page_index*pgItemsPerPage;i<max_elem;i++) {
		new_content = $('#zsearchresulttab tr:eq('+i+')').clone();
    	$('#zsearchresultdisplay').append(new_content);
    }
    return false;
}

$(document).ready(function(){
 
	$("#zsearchform").submit(function(event) {
		/* stop form from submitting normally */
		event.preventDefault();
		if ($("#zsearchquery").val().length >= 3) {
			$.post(custom_search_path,
				$("#zsearchform").serialize(),
				function(data){
		 			$.colorbox({
						html:   data,
						open:   true,
						iframe: false,
						innerHeight:"520px",
						innerWidth:"670px",
						opacity:0.75
					});
					$(".product-pager").pagination($('#zsearchresulttab tr').length, {
						items_per_page:pgItemsPerPage,
						callback:handlePaginationClick,
						load_first_page:true,
						prev_text:pg_prev,
						next_text:pg_next
					});
					if ($('#zsearchresulttab tr').length <= pgItemsPerPage)
						$(".product-pager").hide();
				},
				"html");
		} else {
			jAlert(searchErrorMsg,alertTitle);
		}
	});
 
	/* lien en popup */
	$(".popup").colorbox({width:"60%", height:"80%", opacity:0.75, iframe:true});
	
	/* lien en popup swf */
	$(".flashanim").css({ backgroundColor:'#B5072C' });
	$(".flashanim").wrap('<div style="display:none;"></div>');  
	$(".popupswf").colorbox({opacity:0.75, inline:true});
	/* zoom fiche accessoire */
	// $(".fiche-zoom").colorbox();
	
	/* onglets fiche accessoire */
	if ( $("#tabs").length ){
		$("#tabs").tabs();
		$("#onglet1 a").click(function() {
			if(!$('#tabs-3').is(':visible')){
				$('#fiche-accessoires-reco').show();
				$('#accessoires-produit').show();
			}
		});
		$("#onglet2 a").click(function() {
			if(!$('#tabs-3').is(':visible')){
				$('#fiche-accessoires-reco').show();
				$('#accessoires-produit').show();
			}
		});
		$("#onglet3 a").click(function() {
			if($('#tabs-3').is(':visible')){
				$('#fiche-accessoires-reco').hide();
				$('#accessoires-produit').hide();
			}
		});
	}
	$("#zsearchquery").click(function() {
		if($(this).val() == searchDefaultValue){
			$(this).val("");
		}
	});
	$("#zsearchquery").blur(function() {
		if($(this).val() == ""){
			$(this).val(searchDefaultValue);
		}
	});
  
	/* png transparent pour ie6 */
	var nom = navigator.appName;
	var version = navigator.appVersion;
	ie6 = (nom == 'Microsoft Internet Explorer' && version.indexOf('MSIE 6')>-1 ) ? true:false;
	if(ie6) {supersleight.limitTo('workarea'); supersleight.init();}
});


function setPage(Zahl) {
  document.productform.target = '';
  document.productform.page.value = Zahl;
  document.productform.next.value = "products";
  document.productform.submit();
}

function setPageSize(Zahl) {
  document.productform.target = '';
  document.productform.itemPageSize.value = Zahl;
  document.productform.next.value = "setPageSize";
  document.productform.submit();
}

function setUnit(ItemId, Index) {
  document.productform.itemId.value = ItemId;
  document.productform.next.value = "itemUnits";
  document.productform.submit();
}

function setNext(Next) {
  document.productform.target = '';
  document.productform.next.value = Next;
  document.productform.submit();
}

function doQueryAlt(queryStr) {
  document.NavForm.query.value = queryStr;
  location.href = '/seb/catalog/query/(layout=6_3_63_52_4_8_2&uiarea=2&ctype=areaDetails&carea=42FD87111BA2B7459B82312E77D3C998)/.do' + '?query=' + escape(queryStr);
}

function noOfchecks() {
  no=0;
  for (i=0;i<=count;i++) {
    name="item["+i+"].selected";
    if (document.productform.elements[name].checked) no=no+1;
  }
  return no;
}

function addSingleItem(Itemkey) {
  document.productform.target = '';
  document.productform.next.value = 'addToBasket';
  document.productform.itemkey.value = Itemkey;
  document.productform.submit();
}

function seeSingleItem(Itemkey) {
  document.productform.target = '';
  document.productform.next.value = 'seeItem';
  document.productform.itemkey.value = Itemkey;
  document.productform.submit();
}

function order(sort_order) {
  document.productform.target = '';
  document.productform.next.value = 'order';
  document.productform.order.value = sort_order;
  document.productform.submit();
}

function getMoreFunctions(Itemkey,Next) {
  if (Next == 'leaflet') {
    document.productform.target = '';
  }
  else {
    document.productform.target = '';
  }
  document.productform.itemkey.value = Itemkey;
  document.productform.next.value = Next;
  document.productform.submit();
}

function popUpCompare() {
  l = noOfchecks();
  if (l<2) {
    alert("S&eacute;lectionnez d'abord des produits");
    return;
  }
  window.open("","compareWindow","menubar=no, directories=no, height=500, width=750, scrollbars=yes, status=no, toolbar=no, resizable = yes ");
  document.productform.target = 'compareWindow';
  document.productform.next.value ='compareItems';
  document.productform.action='/seb/catalog/updateItems5/(layout=6_3_63_52_4_8_2&uiarea=2&ctype=areaDetails&carea=42FD87111BA2B7459B82312E77D3C998)/.do'
  document.productform.submit();
}

function lancerWOSizedFlash (chemin,couleur) {
  if (chemin != undefined){
    if (chemin.lastIndexOf(".swf") != -1){
      var codeflash = '<br/><OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
      + 'CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
      + '<PARAM NAME="MOVIE" VALUE="' + chemin + '">'
      + '<PARAM NAME="PLAY" VALUE="true">'
      + '<PARAM NAME="LOOP" VALUE="false">'
      + '<PARAM name=bgcolor value = ' + couleur + '>'
      + '<PARAM NAME="QUALITY" VALUE="high">'
      + '<PARAM NAME="MENU" VALUE="false">'
      + '<EMBED SRC="' + chemin + '"'
      + 'PLAY="true"'
      + 'LOOP="false"'
      + 'QUALITY="high"'
      + ' bgcolor=' + couleur
      + 'MENU="false"'
      + 'TYPE="application/x-shockwave-flash"'+ 'PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">'
      + '</EMBED>'+ '</OBJECT><br/>';
      document.write(codeflash);   // Ecrire l'animation flash
    }
  }
}

function lancerflash (chemin,largeur,hauteur,couleur) {
  if (chemin != undefined){
    if (chemin.lastIndexOf(".swf") != -1){
      var codeflash = '<br/><OBJECT CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
      + 'WIDTH="' + largeur +' " HEIGHT="' + hauteur + '"'
      + 'CODEBASE="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
      + '<PARAM NAME="MOVIE" VALUE="' + chemin + '">'
      + '<PARAM NAME="PLAY" VALUE="true">'
      + '<PARAM NAME="LOOP" VALUE="false">'
      + '<PARAM name=bgcolor value = ' + couleur + '>'
      + '<PARAM NAME="QUALITY" VALUE="high">'
      + '<PARAM NAME="MENU" VALUE="false">'
      + '<EMBED SRC="' + chemin + '"'
      + 'WIDTH="' + largeur +' " HEIGHT="' + hauteur + '"'
      + 'PLAY="true"'
      + 'LOOP="false"'
      + 'QUALITY="high"'
      + ' bgcolor=' + couleur
      + 'MENU="false"'
      + 'TYPE="application/x-shockwave-flash"'+ 'PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash">'
      + '</EMBED>'+ '</OBJECT><br/>';

      document.write(codeflash);   // Ecrire l'animation flash
    }
  }
}

	// places footer below the longest section
function placeFooter(){
	//alert("placeFooter");
	// Get height of the Header Element
	var headHeight = 	GetElementHeight(document.getElementById("header"));
	var addHeight = 0;

	// Determine largest height value of all possible content elements
	addHeight = CheckAddHeight("navigator", addHeight);
	addHeight = CheckAddHeight("workarea", addHeight);
	addHeight = CheckAddHeight("workareaNoRightNav", addHeight);
	addHeight = CheckAddHeight("miniBasket", addHeight);
	addHeight = CheckAddHeight("leaflet", addHeight);
	addHeight = CheckAddHeight("basketEntry", addHeight);
	addHeight = CheckAddHeight("checkout", addHeight);
	addHeight = CheckAddHeight("login", addHeight);

	// add content height to header height to determine complete shop height
	var topSpace = headHeight + addHeight;

	// move page footer element at the bottom of the page
	document.getElementById("pagefooter").style.top = topSpace + "px";
}

// Function to determine height of an element
function GetElementHeight (elm)	{
  if (!elm)
    return 0;

  if (document.layers) {
    if (!elm.height)
      elm.height = elm.clip.height;
    return elm.height;
  }
  else if (typeof(elm.style && elm.style.height) == 'number') {
    return elm.style.height;
  }
  else if (typeof(elm.style && elm.style.height) == 'string' && !isNaN(parseInt(elm.style.height))) {
    return parseInt(elm.style.height);
  }
  else if (elm.offsetHeight) {
    return elm.offsetHeight;
  }
  else if (typeof(elm.style && elm.style.pixelHeight) == 'number') {
    return elm.style.pixelHeight;
  }
  else if (elm.clientHeight) {
    return elm.clientHeight;
  }
  return 0;
}

// return divIds height if larger than addHeight
function CheckAddHeight (divId, addHeight)	{
	var theDiv = document.getElementById(divId);
	if (theDiv) {
		var divHeight = GetElementHeight(theDiv);
		if ( divHeight > addHeight ) return divHeight;
	}
	return addHeight;
}

function setIFrameHeight() {
	var iFrame = document.getElementById("contentIFrame");
	var iFrameLoc  = iFrame.src;
	var windowLoc = window.location;


	// If the page is on an external domain, we can't retrieve the height of the page.
	// Therefor, we have to set it to a static value.
	var iFrameHeight = "800";
		if (iFrameLoc.indexOf('http://') == -1 || iFrameLoc.indexOf(windowLoc.host) != -1){
			iFrameHeight = iFrame.contentWindow.document.body.scrollHeight;
		}

	//alert("ifrheight:"+iFrameHeight);
	iFrame.height = iFrameHeight + "px;";
}

function popup_zoom_open(img, name, param)
{
  monImage = new Image;
  monImage.src = img;

  //params = 'width=' +  monImage.width + ',height=' + monImage.height + ',' + param;
  params = 'width=312,height=312,' + param;

  var win = window.open(img, name, params);
  win.focus();

  return true;
}


function openAcccessKeysWindow()
{
	var accessKeysWindow = window.open('/seb/appbase/accesskeys.jsp',
									 'accessKeysWindow',
									 'width=500,height=400,screenX=200,screenY=300,titlebar=yes,resizable=yes,scrollbars=yes');
	accessKeysWindow.focus();
	return accessKeysWindow;
}

function help() {
  win_help = window.open('/seb/ecall/jsp/customer/common/frameset_help.jsp', '', 'width=700,height=600,scrollbars=yes,resizable=yes');
  win_help.focus();
}

function submitGo() {
  document.NavForm.query.value = '';
  document.NavForm.submit();
}

function callParent() {
  parent.parent.b2ctop.submitGo();
}
