 $(document).ready(function() {
  $.ajaxSetup({ 
	scriptCharset: "utf-8" , 
	contentType: "application/json; charset=utf-8"
  });
  //*************** Vars ********************//
  // set howtivideo not act & howtopic act
  $.howtoVideo = 0; 
  $.howtoImg = 1; 
  $.nextact = 1;
  // cas slider
  $.cas_act = false;
  //*************** !Vars ******************// 
  // disables the language drop down
  $('#lang').attr('disabled', 'disabled');
 // hides the ingredients-rightbox as soon as the DOM is ready (a little sooner that page load)
  $('#ingredients-rightbox').hide();
  // hides map in lang & country choise
  $('.submap').hide();
  $('#map').hide();
  $('#map').fadeIn('slow');
  // hides image & video howto 
//  $('#howto-pic').hide();
  $('#video_wrapper').hide();
  $('.ingDescr').hide();
  $('.ddbrands').hide();
  //  hide menu on the right column
  $('#menu1').hide();
  $('#menu2').hide();
  $('#menu3').hide();
  $('#rightmenu1').click(function() {
	$('#menu1').toggle(400);
	return false;
  });
  //menu right slider functions
  $('#rightmenu2').click(function() {
	$('#menu2').toggle(400);
	return false;
  });
  $('#rightmenu3').click(function() {
	$('#menu3').toggle(400);
	return false;
  });
  // help shows image and video instuctions
  $('#help').click(function() {
    return false;
  });
  $('#help_batch').click(function() {
	goToAnchor('myVideoAnchor');
    return false;
  });
  // show image help
  $('#howto-img-head').click(function() {
    $('#howto-pic').toggle(400);
	if($.howtoImg == 0){
		$(this).removeClass().addClass("acc_act");
		$.howtoImg = 1;
	}else{
		$(this).removeClass().addClass("acc_not_act");
		$.howtoImg = 0;
	}
    return false;
  });
  // imagehowto h5 hover & click functions
  $('#howto-img-head').mouseover(function() {
	if($.howtoImg == 0){
		$(this).removeClass().addClass("acc_not_act_hover");
	}else{
		$(this).removeClass().addClass("acc_act_hover");
	}
  });
  $('#howto-img-head').mouseout(function() {
	if($.howtoImg == 0){
		$(this).removeClass().addClass("acc_not_act");
	}else{
		$(this).removeClass().addClass("acc_act");
	}
  });
  // show video help
  $('#howto-video-head').click(function() {
    toggleVideo();
	if($.howtoVideo == 0){
		$(this).removeClass().addClass("acc_act");
		$.howtoVideo = 1;
	}else{
		$(this).removeClass().addClass("acc_not_act");
		$.howtoVideo = 0;
	}
    return false;
  });
  // video h5 hover & click functions
  $('#howto-video-head').mouseover(function() {
	if($.howtoVideo == 0){
		$(this).removeClass().addClass("acc_not_act_hover");
	}else{
		$(this).removeClass().addClass("acc_act_hover");
	}
  });
  $('#howto-video-head').mouseout(function() {
	if($.howtoVideo == 0){
		$(this).removeClass().addClass("acc_not_act");
	}else{
		$(this).removeClass().addClass("acc_act");
	}
  });
  // show next video help & show transcript
  $('#nextvideo').click(function() {
	if(typeof(nextact) === 'undefined' || nextact == 1){ 
    	initHowto("../../video/","dreft.flv");
    	
    	
    	if($(this).hasClass("tur")) {
    		$("#howto-video-right").html('<p>Şişenin arkasında, net ağırlık bilgisinin civarında bir kod bulunmaktadır.</p><p class="top6px">Bu 8 haneli kodu, arama ekranındaki ilk kutuya yazın.</p>');
    	}
    	
    	else if($(this).hasClass("ser")) {
    		$("#howto-video-right").html('<p>Pogledаjte nа poleđini oznаke zа broj koji je odštаmpаn u blizini neto sаdržаja na deklаrаciji nа bočici. </p><p class="top6px">Uzmite 8 znаkovа u redu zаjedno i stаvite ih u prvo polje za unos aa ekrаnu zа pretrаgu.</p>');
    	}
    	
    	else {
    		$("#howto-video-right").html('<p>Look at the back label for a code  which is printed near the net contents declaration on the bottle.</p><p class="top6px">Take the 8 characters in a row together and put them into the first input field of the search screen.</p>');
    	}
		nextact	= 0;
    	return false;
	}
	if(nextact == 0){
		initHowto("../../video/","ariel.flv");
		
		if($(this).hasClass("tur")) {
			$("#howto-video-right").html('<p>Pakette net ağırlık bilgisinin civarında bir kod bulunmaktadır.</p><p class="top6px">Bu 8 haneli kodu, arama ekranındaki ilk kutuya yazın.</p>');
		}
		
		else if($(this).hasClass("ser")) {
			$("#howto-video-right").html('<p>Potraži kod koji je odštаmpаn u blizini neto sаdržаja kod deklаrаcije nа pаkovаnju.</p><p class="top6px">Uzmite 8 znаkovа u redu zаjedno i stаvite ih u prvo polje za unos na ekrаnu zа pretrаgu.</p>');
		} 
		
		else {
			$("#howto-video-right").html('<p>Search for a code which is printed near the net contents declaration on the package.</p><p class="top6px">Take the 8 characters in a row together and put them into the first input field of the search screen.</p>');
		}
		nextact	= 1;
		 return false;
	}
  }); 
// Map Country - Language
  $('#Country').change(function() {
	var country = $("#Country").val();  
	$.ajax({
		type: "GET",
		url: "countrylang.xml",
		dataType: "xml",
		success: function(xml){
		  $(xml).find("result").each(function()
		  {
			if($(this).attr("id") == country) {
				var lang = $(this).find('language').text();
				var lng = $(this).find('lng').text();
				var img = $(this).find('img').text();
				$("#lang").html('<option value="'+lng+'">'+lang+'</option>');
				$('#lang').removeAttr('disabled');
				$('.submap').fadeIn('slow');
				changeImage(img);
			}
		  });        
    	}
	});
  });
  // show ingredient rightbox and throw ajax request to highlight ingredients with informations
  $('#slick-toggle').click(function() {
	if(!$.cas_act){
		$.cas_act = true;
	}else {	
		$.cas_act = true;
	}
	$('.ingDescr').show();
	$( ".scrollArea li" ).each(
		function(index) {
			var listItem = $(this);
    		var cas_id = $(this).attr("title");
			$.ajax({
				type: "GET",
				url: "../xml/ing_statements.xml",
				dataType: "xml",
				success: function(xml){
				  $(xml).find("result").each(function()
				  {
					if($(this).attr("id") == cas_id) {
						listItem.css("color" , "#0000C9");
						listItem.css("cursor","pointer");
					}
				  });        
				}
			});
  		}
	);

    return false;
  });
  // cas ajax request to show on right box
  $('.cas').click(function() {
	var cas_id = $(this).attr("title");
	if(!$.cas_act){
		$('#ingredients-rightbox').toggle(400);
		$.cas_act = true;
	}else{
		$.cas_act = false;
	}
	$.ajax({
		type: "GET",
		url: "../xml/ing_statements.xml",
		dataType: "xml",
		success: function(xml){
		  $(xml).find("result").each(function()
		  {
			if($(this).attr("id") == cas_id) {
				var cas_name = $(this).find('name').text();
				var cas_functionality = $(this).find('functionality').text();
				var cas_statement = $(this).find('statement').text();
				var cas_link1 = $(this).find('link1').text();
				var cas_link2 = $(this).find('link2').text();
				$("#ingredients-rightbox").html('<span class="bold-printingredient">'+cas_name+'</span>'+
												'<div class="scroll-cas">'+
													'<span class="bold-left12px ">Functionality</span>'+
													'<p>'+cas_functionality+'</p>'+
													'<p class="bold">Eco</p>'+
													'<p>'+cas_statement+'</p>'+
													'<p><a href="'+cas_link1+'">'+cas_link1+'</a>'+
													'<p><a href="'+cas_link2+'">'+cas_link2+'</a>'+
												'</div>'
												);
				$(".scroll-cas").css("overflow", "auto");
		  		$(".scroll-cas").css("width", "246px");
		  		$(".scroll-cas").css("height", "240px");
				$(".scroll-cas").css("margin-top", "16px");
			}
		  });        
    	}
	});
  }); 
  // redirection for map
  $("#ProductSearchForm").submit(function () { 
	var myLang = $("#lang").val(); 
	var country = $("#Country").val();
	var uri = window.location + myLang + '/search/' +country;
	location.href = uri;
	return false;
  });

  $.extend({
    getUrlVars: function(){
      var vars = [], hash;
      var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
      for(var i = 0; i < hashes.length; i++)
      {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
      }
      return vars;
    },
    getUrlVar: function(name){
    return $.getUrlVars()[name];
    }
  });
  // dosing hack to show directly
  $('#tmp_cnt_ddchange').click(function() {
	$('.ddbrands').toggle(400);
    return false;
  });
  // IE6 Png transparency fix
	$(document).pngFix();
});
// Map image swap 
function changeImage(img)
{
  $('#map').fadeOut('slow', function() {
    $('#map').html('<img src="img/general/map/'+img+'" width="544" height="530" />');
	$('#map').fadeIn();
  });
}
// Batch help functions
function iniBatch(str)
{
	$('#errorBatchCode').html(str);
	$(".batchfield").css("background" , "#FFFFFF");
	$("#help_batchcode").css("visibility", "visible");
	$(".plus").css("visibility", "visible");
}
function toggleVideo()
{
	$('#video_wrapper').toggle(400);
}
function batch_required(mystr){
	$('#errorBatchCode').html(mystr);
	$(".batchfield").css("border", "1px solid red");
	$(".batchfield").css("background" , "#FFFFFF");
	$("#help_batchcode").css("visibility", "visible");
	$("#help_batch_result").css("border-bottom" , "1px solid #ff9900");
	$(".plus").css("visibility", "visible");
	
}
function batch_error(){
	$(".batchfield").css("border", "1px solid red");
	$(".batchfield").css("background" , "#FFFFFF");
	$("#help_batchcode").css("visibility", "visible");
	$("#help_batch_result").css("border-bottom" , "1px solid #ff9900");
	$(".plus").css("visibility", "visible");
	
}
function init_batch(){
	$(".batchfield").css("background" , "#FFFFFF");
	$("#help_batchcode").css("visibility", "visible");
	$(".plus").css("visibility", "visible");
	
}
// HOWTO Video
function initHowto(path, videosource)
{
 var flashvars = {};
 var params = {};
 var attributes = {};
 flashvars.mySource = videosource;    
 flashvars.wmode = "transparent";
 flashvars.allowFullscreen = "true";
 flashvars.mySkinn = path+"SkinUnderPlaySeekFullscreen.swf";
 swfobject.embedSWF(path+"player.swf", "flashcontent-howto", "320", "280", "9.0.115", false, flashvars, params, attributes); 
}
// DD functions (flash is only shown for a certain p reange)
function initDosing(swf,linkUri){
 var flashvars = {};
 var params = {};
 var attributes = {};
 flashvars.url = linkUri;    
 flashvars.wmode = "transparent";
 params.wmode = "transparent";
 swfobject.embedSWF(swf, "flashcontent-dosing", "168", "144", "9.0.115", false, flashvars, params, attributes); 
}
// 
function gotoVodeo(evt)
{
	
	
    toggleVideo();
    goToAnchor('#flashcontent-howto');
	if($.howtoVideo == 0){
		$('#howto-video-head').removeClass().addClass("acc_act");
		$.howtoVideo = 1;
	}else{
		$('#howto-video-head').removeClass().addClass("acc_not_act");
		$.howtoVideo = 0;
	}
	
	evt.preventDefault();
}
// anchor functions
function goToAnchor(myAnchor){
 window.location.hash=myAnchor;
}
