
$(document).ready(function(){
  var addPos = location.search.substring(4);
  var currentPosition = (addPos == '')? 0: eval(addPos-1);
  var slideWidth = 610;
  var slides = $('.slide');
  var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
    .wrapAll('<div id="slideInner"></div>')
    // Float left to display horizontally, readjust .slides width
    .css({
      'float' : 'left',
      'width' : slideWidth
    });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  $('#slideshow')
    .prepend('<span class="control" id="leftControl"></span>')
    .append('<span class="control" id="rightControl"></span>');

  // Hide left arrow control on first load
  manageControls(currentPosition);
  
  // Set the correct round number
  changeConseils(currentPosition);
  
  // Go to the initial slide
  $('#slideInner').animate({
    'marginLeft' : slideWidth*(-currentPosition)
  });

  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', function(){
    // Determine new position
    currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
    
    // Hide / show controls
    manageControls(currentPosition);
    
    // Set the correct round number
    changeConseils(currentPosition);
    
    // Move slideInner using margin-left
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
    if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
    // Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
  }
  
  function changeConseils(id){
      var elt = document.getElementById('conseils_content_num_ean');
      var tab = elt.getElementsByTagName("div");
      for(var i = 0; i < 11; i++){
          tab[i].id = '';
          if(i == id)
              tab[i].id = 'conseils_num_on_ean';
      }
  }
});

var activeClip;

function Load(Id, Lang){	
	if(window.location.href == "http://www.wer-bisch.ch/")
		window.location.replace("http://www.wer-bisch.ch/index_de.html");

	var params = window.location.search.slice(1,window.location.search.length);
	params = params.split("&");

    for(var i = 0; i < params.length; i++){
		var param = params[i].split("=");
		if(param[0] == "clipId"){
			var clipId = param[1];
			if(clipId == 1 || clipId == 2 || clipId == 3)
				activeClip = clipId;
			else
				activeClip = Id;
       	}else
			activeClip = Id;
	}
	
    if(activeClip == 1){
		DisableClip("2", Lang);
		DisableClip("3", Lang);
	}else if(activeClip == 2){
		DisableClip("1", Lang);
		DisableClip("3", Lang);
	}else if(activeClip == 3){
		DisableClip("1", Lang);
		DisableClip("2", Lang);
	}    
}

//Fonction spéciale pour les 2 pages index, qui permet de vérifier si la dernière version Flash est activé
//sur l'ordinateur. Cette fonction concerne que les pages index pour la simple raison qu'il y a la diffusion
//des vidéos.
function LoadAndControlFlash(Id, Lang, reqVerStr,msg){
	if(window.location.href == "http://www.wer-bisch.ch/")
		window.location.replace("http://www.wer-bisch.ch/index_de.html");

	var params = window.location.search.slice(1,window.location.search.length);
	params = params.split("&");

    for(var i = 0; i < params.length; i++){
		var param = params[i].split("=");
		if(param[0] == "clipId"){
			var clipId = param[1];
			if(clipId == 1 || clipId == 2 || clipId == 3)
				activeClip = clipId;
			else
				activeClip = Id;
       	}else
			activeClip = Id;
	}
	
    if(activeClip == 1){
		DisableClip("2", Lang);
		DisableClip("3", Lang);
	}else if(activeClip == 2){
		DisableClip("1", Lang);
		DisableClip("3", Lang);
	}else if(activeClip == 3){
		DisableClip("1", Lang);
		DisableClip("2", Lang);
	}
	
	with(navigator){
		var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
		var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
		if (!isIE || !isWin){
			var flashVer = -1;
			if (plugins && plugins.length > 0){
				var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
				desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
				if (desc == "") flashVer = -1;
				else{
					var descArr = desc.split(" ");
					var tempArrMajor = descArr[2].split(".");
					var verMajor = tempArrMajor[0];
					var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
					var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
					flashVer =  parseFloat(verMajor + "." + verMinor);
				}
			}

			else if (userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 4.0;
				var verArr = reqVerStr.split(",");
				var reqVer = parseFloat(verArr[0] + "." + verArr[2]);

				if (flashVer < reqVer){
					if (confirm(msg))
						if(Lang == "fr"){
							window.location = "http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
						} else if(Lang == "de"){
							window.location = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash&Lang=German";
						}
						
				}
	    }
	}	
}

//Fonction qui met à jour l'état du clip
function UpdateClip(Id, Lang){
	activeClip = Id;
		if(activeClip == 1){
			DisableClip("2", Lang);
			DisableClip("3", Lang);
        }else if(activeClip == 2){
			DisableClip("1", Lang);
			DisableClip("3", Lang);

        }else if(activeClip == 3){
			DisableClip("1", Lang);
			DisableClip("2", Lang);
        }
}

//Fonction qui permet de charger la vidéo Flash en fonction du bonton sur lequel
//l'utilisateur appuye et dans la langue souhaité.
function UpdateVideo(Id, Lang, titre){
	activeClip = Id;
	var tkiNoLang = "tki" + Id + Lang + ".flv";
	document.getElementById("titre").innerHTML = titre;
	document.getElementById("player1").innerHTML = "<object type='application/x-shockwave-flash' width='450' height='370' wmode='transparent' data='videos/flvplayer.swf?file=" + tkiNoLang + "&autoStart=true'>" +
												       "<param name='movie' value='videos/flvplayer.swf?file=" + tkiNoLang + "&autoStart=true' />" +
												       "<param name='wmode' value='transparent' />" +
												   "</object>"; 
}

function DisableClip(Id, Lang){
	if(Id == activeClip)
		return;
	if(Lang == 'fr')
		document.getElementById("imgClip" + Id).src = "img/logo_clip" + Id + "_off.gif";
	else if(Lang == 'de')
		document.getElementById("imgClip" + Id).src = "img/logo_videoclip" + Id + "_off.gif";
	document.getElementById("txtClip" + Id).className = "txtClipOff";
	/*document.getElementById("link1Clip" + Id).className = "linkClipOff";
	document.getElementById("link2Clip" + Id).className = "linkClipOff";*/
}

function EnableClip(Id, Lang){
	if(Lang == 'fr')
		document.getElementById("imgClip" + Id).src = "img/logo_clip" + Id + "_on.gif";
	else if(Lang == 'de')
		document.getElementById("imgClip" + Id).src = "img/logo_videoclip" + Id + "_on.gif";
	document.getElementById("txtClip" + Id).className = "txtClipOn";
	/*document.getElementById("link1Clip" + Id).className = "linkClipOff";
	document.getElementById("link2Clip" + Id).className = "linkClipOff";*/
}

function DisableMenu(Name){
	document.getElementById("bt_" + Name).src = "img/" + Name + "_off.gif";
}

function EnableMenu(Name){
	document.getElementById("bt_" + Name).src = "img/" + Name + "_on.gif";
}

function checkMailSending(){
	/* TODO */
}

