function setCookie(name, value, expiredays, path, domain, secure) {
   if (expiredays) {
      var exdate=new Date();
      exdate.setDate(exdate.getDate()+expiredays);
      var expires = exdate.toGMTString();
   }
   
   document.cookie = name + "=" + escape(value) +
   ((expiredays) ? "; expires=" + expires : "") +
   ((path) ? "; path=" + path : "") +
   ((domain) ? "; domain=" + domain : "") +
   ((secure) ? "; secure" : "");
   
}

function getCookie(name) {
   var cookie = " " + document.cookie;
   var search = " " + name + "=";
   var setStr = null;
   var offset = 0;
   var end = 0;
   if (cookie.length > 0) {
      offset = cookie.indexOf(search);
      if (offset != -1) {
         offset += search.length;
         end = cookie.indexOf(";", offset)
         if (end == -1) {
            end = cookie.length;
         }
         setStr = unescape(cookie.substring(offset, end));
      }
   }
   return setStr;
}

function initVoter(){  
  $(".vote-plus,.vote-minus,.vote-p,.vote-m").remove();
    
  var rel=$(".highslide-image").attr('src');
  var rel2=rel.match("path=(.*)\.\.\.\.(.*)&w")[1];
  if(getCookie(rel2)=="plus"){
    //$(".highslide-wrapper").append('<span class="vote-p">Спасибо</span>');
  }else if(getCookie(rel2)=="minus"){
    //$(".highslide-wrapper").append('<span class="vote-m">-1</span>');
  }else{
    $(".highslide-wrapper").append('<a href="#" class="vote-plus">Понравилось</a>');
  }
  
  $(".vote-plus").click(function(){
    $.post("/ajax/vote.php",{"path":rel,"plus":true},function(){
      var rel=$(".highslide-image").attr('src');
      var rel2=rel.match("path=(.*)\.\.\.\.(.*)&w")[1];
      
      $(".vote-plus,.vote-minus,.vote-p,.vote-m").remove();
      $(".highslide-wrapper").append('<span class="vote-p">Спасибо!</span>');
      setCookie(rel2, "plus", 1, "/");
    });
    return false;
  });
  
};

$(document).ready(function(){
	var day=0;
	$(".changeEvents").each(function(){
    if($(this).hasClass('on')){
      var d=$(this).attr('day');
      day=d;
    }
	});
	
  $(".sb a").click(function(){
    var form=$(this).parent().parent().parent();
    form.submit();
  });
	
	$(".p_am a").click(function(){
        var rel=$(this).attr("rel");
        var classe=$(this).attr("class");
        
        if(classe=="act") {
            $(this).removeClass("act");
            $(".wrap_map"+rel).animate({ height: "0px",  }, 800 );      return false;
        }
        else {    
            $(this).addClass("act");
            $(".wrap_map"+rel).animate({ height: "300px",  }, 800 );      return false;
        }
        
        return false;


     });
	
	function updateFunction(){
    $(".c_left,.c_right").click(function(){
      var date=$(this).attr('rel');
      $.post("/ajax/calendar.php",{act:'month',day:day,date:date},function(data){
        $("#changeEventAjax").html(data);
        updateFunction();
      });
      return false;
  	});
  	$(".changeEvents").click(function(){
      var date=$(this).attr('rel');
      $(".changeEvents").removeClass('on');
      $(this).addClass('on');
      $(".changeEventAjax").remove();
      
      var d=$(this).attr('day');
      day=d;
      
      $.post("/ajax/calendar.php",{date:date},function(data){
        $("#changeEventAjax").append(data);
      })
      return false;
  	});
	}
	
  $(".c_left,.c_right").click(function(){
    var date=$(this).attr('rel');
    $.post("/ajax/calendar.php",{act:'month',day:day,date:date},function(data){
      $("#changeEventAjax").html(data);
      updateFunction();
    });
    return false;
	});
	
	$(".changeEvents").click(function(){
    var date=$(this).attr('rel');
    $(".changeEvents").removeClass('on');
    $(this).addClass('on');
    $(".changeEventAjax").remove();
    
    var d=$(this).attr('day');
    day=d;
    
    $.post("/ajax/calendar.php",{date:date},function(data){
      $("#changeEventAjax").append(data);
    })
    return false;
	});
	
	$(".events_acc").accordion({event:"mouseover"});
	$(".exhibitions_acc").accordion({event:"mouseover"});
	$("a[rel=calendar]").click(function(){
	  $("#popup_calendar").show();
	  return false;
	});
	$("#pc_close").click(function(){
	  $("#popup_calendar").hide();
	  return false;
	});
	
	if($("div.move_element").width !== null) {
		var gal_num = $("div.move_element").length;
		var visible_width = $("div.move_element").width();
		var gal_height_array = new Array();
		var gal_id_array = new Array();
		var current_id = "";
		var current_step = 0;
		var go_int;
		var step = 20;
		
		$("a.arr_big").click(function(event){
			event.preventDefault();
		});

		$(window).load(function() {
			for(var i = 0; i < gal_num; i++) {
				gal_height_array[i] = $("div.move_element table").height();
				gal_id_array[i] = $("div.move_element:eq(" + i + ")").attr("id");
				$("#" + gal_id_array[i]).height(gal_height_array[i]);
				$("#" + gal_id_array[i] + " a.arr_big").css("top", Math.round(gal_height_array[i] / 2) - 64);
			}
			
			$("a.arr_left").hover(
				function() {
					current_id = $(this).parent().attr("id");
					current_step = 1;
					go_int = setInterval(move_pics, 50);
				},
				function() {
					current_step = 0;
					clearInterval(go_int);
				}
			);
			$("a.arr_right").hover(
				function() {
					current_id = $(this).parent().attr("id");
					current_step = -1;
					go_int = setInterval(move_pics, 50);
				},
				function() {
					current_step = 0;
					clearInterval(go_int);
				}
			);
			
			function move_pics() {
				var local_left = parseInt($("#" + current_id + " > div.move_scroll").css("left"));
				var local_width = $("#" + current_id + " table").width();
				local_left = local_left + (current_step * step);
				if(local_left > 0) {local_left = 0};
				if(local_left < visible_width - local_width) {local_left = visible_width - local_width};
				$("#" + current_id + " > div.move_scroll").css("left", local_left);
			}
		});
		
	}


     



});
