﻿      function initialize() {
          fillCombo("flashlightCombo", flashlights, curFlashlight);
          fillCombo("sceneCombo", scenes[curFlashlight], curScene);
          fillCombo("modeCombo", modes[curFlashlight][curScene], curMode);
          beamshotChanged();
          changeAbout();
      }
      
      function initialize2() {
          //fillCombo("flashlightCombo", flashlights, curFlashlight);
          fillCombo("sceneCombo", scenes[curFlashlight], curScene);
          fillCombo("modeCombo", modes[curFlashlight][curScene], curMode);
          beamshotChanged();
          changeAbout();
		  fillCompare();
      }
      
      function fillCombo(id, arr, selected) {
          var combo = document.getElementById(id);
          combo.options.length = 0;
          for (var i=0; i<arr.length; i++) {
            combo.options[i] = new Option(arr[i], i);
          }
          combo.options.selectedIndex = selected;
      }

      function beamshotChanged() {
        var path = "img/" + filenames[curFlashlight][curScene][curMode];
		changeImage("beamshot", path);
      }
      
      function changeImage() {
          document[changeImage.arguments[0]].src = changeImage.arguments[1];
		  if (curFlashlight!='undefined') {
			var mode = modes[curFlashlight][curScene][curMode];
			document[changeImage.arguments[0]].alt = flashlights[curFlashlight] + " " + mode + " - тест и сравнение фонарей";
			document[changeImage.arguments[0]].title = flashlights[curFlashlight] + " " + mode + " - тест и сравнение фонарей";
			}
      }
      
      function flashlightChanged(combo) {
          curFlashlight = combo.selectedIndex;
          curMode       = 0;
          
          if (scenes[curFlashlight][curScene]) {
          } else {
            curScene = 0;
          }

          fillCombo("sceneCombo", scenes[curFlashlight], curScene);
          fillCombo("modeCombo", modes[curFlashlight][curScene], 0);
          changeAbout();
          beamshotChanged();
      }
      
      function flashlightChanged2(selIndex) {
          curFlashlight = selIndex;
          curMode       = 0;
          
		  showLikeButtons(curFlashlight);
		  
          if(getMode>0) {
              curMode = getMode;
              getMode = 0;
          }
          
          if (scenes[curFlashlight][curScene]) {
          } else {
            curScene = 0;
          }
          
          
          $("#item_"+prevIndex).css("background-color","#000000");
          $("#item_"+selIndex).css("background-color","#585858");
          prevIndex = selIndex;

          fillCombo("sceneCombo", scenes[curFlashlight], curScene);
          fillCombo("modeCombo", modes[curFlashlight][curScene], 0);
          changeAbout();
          beamshotChanged();
          $("#stable_link").html(selIndex);
          $("#scene").html("&scene="+curScene);
          $("#mode").html("&mode="+curMode);
      }
      
      function sceneChanged(combo) {
          curScene  = combo.selectedIndex;
          curMode   = 0;
		  window.location = "?model=" + curFlashlight + "&scene=" + curScene + "&mode=" + curMode;
		  return;
          fillCombo("modeCombo", modes[curFlashlight][curScene], 0);
          beamshotChanged();
          changeAbout();
          $("#scene").html("&scene="+curScene);
      }

      function modeChanged(combo) {
          curMode = combo.selectedIndex;
		  window.location = "?model=" + curFlashlight + "&scene=" + curScene + "&mode=" + curMode;
		  return;
          beamshotChanged();
          $("#mode").html("&mode="+curMode);
      }

      function changeViewMode(viewMode) {
          var beamshotImage = document.getElementById("beamshotImage");
          beamshotImage.style.width  = "650";
          beamshotImage.style.height = "400";
      }
      
      function changeAbout() {
          for ( var i in scenes_rus ) {
              if (scenes_rus[i] == scenes[curFlashlight][curScene]){
                  jQuery('#sceneAbout').html(scenes_descr[i]);
              } 
          }
      }

      function addCompare() {
          if (curFlashlight!='undefined') {
            var mode = modes[curFlashlight][curScene][curMode];
            compare_name.push(flashlights[curFlashlight] + " " + mode);
            compare_img.push("img/" + filenames[curFlashlight][curScene][curMode]);
			saveCompare();
            listCompare();
          }
      }
	  
	  function saveCompare() {
		setCookie("compare_name",compare_name, 1);
		setCookie("compare_img",compare_img, 1);
	  }
	  
	  function fillCompare() {
		var compare_name_ = getCookie("compare_name");
		var compare_img_ = getCookie("compare_img");
		if (compare_name_) {
			compare_name = compare_name_.split(',')
		} else { compare_name = []; }
		if (compare_img_) {
			compare_img = compare_img_.split(',')
		} else { compare_img = []; }
		listCompare();
	  }

      function listCompare() {
          var list ="";
          for (var i=0;i<compare_name.length;i++) {
              list = list + "<div><div style='float:left;border-bottom:1px solid grey'><span class='delete' onclick='DeleteCompare(" + i + ")'><font color='red'>X </font></span>" +  compare_name[i] +"</div></div>";
          }
          $("#compare_list").html(list);
		  $(".compare_add_button").attr("disabled","");
      }

      function clearCompare() {
          compare_name = [];
          compare_img = [];
		  saveCompare();
          listCompare();
      }

      function Compare() {
          if (compare_name.length == 0 ) return;
          var list ="";
          for (var i=0;i<compare_name.length;i++) {
              list = list + "<div style='float:left;margin-left:10px;max-width:47%'>" +"<font color='white'><center>" + compare_name[i] + "</center></font><img width='750px' style='max-width:100%' src='" + compare_img[i] + "'></div>";
          }
          $("#compare_imgs").html(list);
          $("#compare_img").show();
      }

      function CloseCompare() {
          $("#compare_img").hide();
      }

      function DeleteCompare(i) {
          compare_name.splice(i,1);
          compare_img.splice(i,1);
		  saveCompare();
          listCompare();
      }
	  
	  function showLikeButtons(nId) {
		$(".like_button").css('opacity', '0.1');
		if (getCookie("fonarik_vote_" + nId) != 1)
		{
			$(".like_button").css('opacity', '1');
		}
	  }
	  
	  function Like() {
		$.ajax({
				url: "ajax_vote.php",
				type: "POST",
				data: "type=like&id=" + curFlashlight,
				success: function(){
					setCookie("fonarik_vote_" + curFlashlight, 1, 3650);
			}
		});
		$(".like_button").css('opacity', '0.1');
	  }
	  
	  function NotLike () {
		$.ajax({
				url: "ajax_vote.php",
				type: "POST",
				data: "type=notlike&id=" + curFlashlight,
				success: function(response){
					setCookie("fonarik_vote_" + curFlashlight, 1, 3650);
			}
		});
		$(".like_button").css('opacity', '0.1');
	  }
	  
	function setCookie(c_name,value,exdays)
	 {
		var exdate=new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
	}
	
	function getCookie(c_name)
	{
		var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++)
		{
			x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
			{
				return unescape(y);
			}
		}
	}
	

