//-------------------------------------------------------------------------------------------
// PEPS Rollover class
//------------------------------------------------------------------------------------------ 
PEPS = {};
  PEPS.rollover = 
  {
   init: function()
   {
      this.preload();
      
      $(".ro").children("img").hover(
         function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); }, 
         function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.ro').children("img").each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   { 
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '1' + src.match(/(\.[a-z]+)$/)[0]; 
   },

   oldimage: function( src )
   { 
      return src.replace(/1\./, '.'); 
   }
};

$(function(){  

  //-------------------------------------------------------------------------------------------
  // IE6 SUPPORT, fix PNG images
  //------------------------------------------------------------------------------------------ 
  if(scripts_isIE6)
  {
      $(window).load( function() { 

		$(document).pngFix();

      });

      var contentHeight = $('div#main-box-content').height();
      if(contentHeight<370) {$('div#main-box-content').height('370');}
  }


  $(window).load( function() { 
     //var contentHeight = $('div#main-box-content').height();
     //$('div#main-box-content').height(contentHeight - 100);
     //$('div#main-box-content').css('overflow-y','hidden');

     /* var contentHeight = $('div#main-box').height();
     $('div#main-box').height(contentHeight - 120); */
     $('div#main-box').css('overflow-y','hidden');
  });
   
  //-------------------------------------------------------------------------------------------
  // Initialize PEPS rollover class
  //------------------------------------------------------------------------------------------ 
  PEPS.rollover.init();
  
  $("#left-nav ul li").mouseover(function(){
      $(this).css("cursor", "pointer");
  });
  
  $("#left-nav ul li").mouseout(function(){
      $(this).css("cursor", "default");
  });
  
  $(".lnavRo").mouseover(function(){
      $(this).css("background","#aed1e3 url('/images/lnav-selected.gif') repeat-x 0px 0px");
  });
  
  $(".lnavRo").mouseout(function(){
      $(this).css("background","#ededed url('/images/lnav.gif') repeat-x 0px 0px");
  });
  
  $(".lnavRo").click(function(){
      var url = $(this).children("div").children("a").attr("href");
      window.location = url;
  });


  $('select#sel-lang').change(function(){
	var gotourl = $(this).val();
	if(gotourl!="") window.location = gotourl;
  });

  //-------------------------------------------------------------------------------------------
  // setup printer click
  //------------------------------------------------------------------------------------------ 
  $('#utilPrint').click(function(){
	printPage()
  });

});
  
//-------------------------------------------------------------------------------------------
// POSITION DIV IN THE CENTER OF WINDOW
//------------------------------------------------------------------------------------------
function centerLyr(divID)
{
    $('#'+divID).css("position","absolute");
 
    var newTop = ( $(window).height() - $('#'+divID).height() ) / 2+$(window).scrollTop();
    if(newTop<0) newTop = 0;

    var newLeft = ( $(window).width() - $('#'+divID).width() ) / 2+$(window).scrollLeft();
    if(newLeft<0) newLeft = 0;

    $('#'+divID).css("top", newTop + "px");
    $('#'+divID).css("left", newLeft + "px");
}

//-------------------------------------------------------------------------------------------
// RE-POSITION DIV IN THE CENTER OF WINDOW ON WINDOW RESIZE
//------------------------------------------------------------------------------------------
function rePositionLyr(status,divID) 
{
    switch(status) {
	case true:
		$(window).bind('resize', function() {
			centerLyr(divID);
		});
		//scroll is not used here
		//$(window).bind('scroll', function() {
		//	centerLyr(divID);
		//});
	    break;
	case false:
		$(window).unbind('resize');
		//scroll is not used here
		//$(window).unbind('scroll');
	    break;
    }
}

//-------------------------------------------------------------------------------------------
// POSITION OVERLAY IN THE CENTER OF WINDOW
//------------------------------------------------------------------------------------------
function showPopupDivCenter(divID) 
{
    if (scripts_isIE6) $('#'+divID).bgiframe();
    var toShow = $("#"+divID); 
    toShow.css({"z-index":"9000","visibility":"visible"});
    centerLyr(divID);
    rePositionLyr(true,divID);
}

//-------------------------------------------------------------------------------------------
// OPEN DIV OVERLAY AND POSITION CENTER
//------------------------------------------------------------------------------------------
function openDivCenter(divID,closeBtnID)
{
    var toShow="";
    // Build the background overlay div
    toShow += "<div class='site_overlay'></div>";
    $('body').append(toShow);

    $('div.site_overlay').css('height',$(document).height()).bind('click',function(){
	closeDiv(divID);
    });

    $('div.site_overlay').css('opacity',0).fadeTo('slow',0.65, function(){
	showPopupDivCenter(divID);
	$('div.site_overlay').css("z-index","9000");
	$('#'+divID).css("z-index","9500");
    });

	
    if(closeBtnID!=""){
        $("#"+closeBtnID).bind('click',function(){
	        closeDiv(divID);
        });
    }
}

//-------------------------------------------------------------------------------------------
// CLOSE OPENED DIV OVERLAY
//------------------------------------------------------------------------------------------
function closeDiv(divID)
{
    $('div.site_overlay').remove();
    rePositionLyr(false,divID);
    var toHide = $("#"+divID); 
    toHide.css({"z-index":"0","visibility":"hidden"});
    if(divID=="all-products"){
	toHide.css("width", "1px");
	toHide.css("height", "1px");
    }
}

//-------------------------------------------------------------------------------------------
// LOAD FLASH OBJECT
//------------------------------------------------------------------------------------------
function loadFlashObject(divID, swfPath, swfID, w, h, version, bgcolor)
{
     var ua  = navigator.userAgent.toLowerCase();
     var mac = ua.indexOf("mac") != -1; ;
     var safari = jQuery.browser.safari;

     var so = new SWFObject(swfPath, swfID, w, h, version, bgcolor);
     
     so.addParam("wmode", "transparent");
     so.write(divID);
}


//-------------------------------------------------------------------------------------------
// PRINT FUNCTIONS
//------------------------------------------------------------------------------------------
function alertPrint() {
  var whichPlatform = navigator.userAgent;
  if (whichPlatform.indexOf("mac") != -1){
	alert("Your browser does not support automatic printing. Please press command + P on your keyboard to print.");
  } else {
	alert("Your browser does not support automatic printing. Please press control + P on your keyboard to print.");
  }
}

function printFrame(frm) {
  if (window.print){
	window.parent.eval(frm).focus();
	window.print();
  } else {
	alertPrint();
  }
}

function printPage(){
  if (window.print){
	window.print();
  } else {
	alertPrint();
  }
}

function openTool(part,flash){

	if(flash == "product"){
		page = "/produit-interactif.html";
	}
	else{
		page = "/programme-interactif.html";
	}
	
	var width = 1024;
	var height = 768;
	
	var value = "";
	
	if(part){
		
		value = "?section=" + part;
	
	}
		
	if(screen.width <= width || screen.height <= height){
		
		//width = screen.width * 0.8;
		//height = screen.height * 0.8;
	
	}
	
	var myWindow = window.open(page + value,'Nicotinell','location=no, toolbar=no, menubar=no, scrollbar=no,resizable=yes,width=' + width + ',height=' + height + '');

}


$(function(){  
	
        var url = window.location.search;
        var imgTimbres = "../images/substituts-nicotinell/button_timbres.png";
        var imgTimbres_hover = "../images/substituts-nicotinell/button_timbres_select.png";
        
        var imgGomme = "../images/substituts-nicotinell/button_gommes.png";
        var imgGomme_hover = "../images/substituts-nicotinell/button_gommes_select.png";
        
        var imgComprime = "../images/substituts-nicotinell/button_comprime.png";
        var imgComprime_hover = "../images/substituts-nicotinell/button_comprime_select.png";
        
        if (url != "") {
            if (url.indexOf("gomme") != -1) {
                $("#content .timbre").css("display","none");
                $("#content .gomme").css("display","block");
		$("#content .comprime").css("display","none");
                
                $(".bien-les-utiliser #top #gomme").addClass("selected");
                $(".bien-les-utiliser #top #gomme img:eq(0)").attr("src",imgGomme_hover);
                
                $(".bien-les-utiliser #top #timbre").removeClass("selected");
                $(".bien-les-utiliser #top #timbre img:eq(0)").attr("src",imgTimbres);
            }
        }
        
        
	$(".bien-les-utiliser #top div").click(function() {
		
		
		
		
		var id = $(this).attr("id");
		
		
		$(".bien-les-utiliser #top div").each(function(i) {
				$(this).removeClass("selected");
				
				if (i == 0) {
					$(this).find("img:eq(0)").attr("src",imgTimbres);
				}
				else if (i == 1) {
					$(this).find("img:eq(0)").attr("src",imgGomme);
				}
				else {
					$(this).find("img:eq(0)").attr("src",imgComprime);
				}
				
		});	
		
		$(this).addClass("selected");
		
		if (id == "timbre") {
			$("#"+id).find("img:eq(0)").attr("src",imgTimbres_hover);
		}
		else if (id == "gomme") {
			$("#"+id).find("img:eq(0)").attr("src",imgGomme_hover);
		}
		else {
			$("#"+id).find("img:eq(0)").attr("src",imgComprime_hover);
		}
		
		
		$("#content .timbre").css("display","none");
		$("#content .gomme").css("display","none");
		$("#content .comprime").css("display","none");
		
		$("#content ." + id).css("display","block");
	});
});
