var aPolls;
$(function () {
	var search = $("input[name=q]");
	search.focus(function(){
		$(this).addClass("out");
	});
	search.blur(function(){
		if ($(this).val() == "") $(this).removeClass("out");
	});	
	$("#videoribbon .jCarouselLite").jCarouselLite({
	  btnNext: "#videoribbon .next",
	  btnPrev: "#videoribbon .prev",
	  visible: 1,
	  speed: 500,
	  btnGo: ["#videoribbon .1", "#videoribbon .2", "#videoribbon .3", "#videoribbon .4", "#videoribbon .5", "#videoribbon .6", "#videoribbon .7", "#videoribbon .8", "#videoribbon .9", "#videoribbon .10"],
	  circular: false
	});
	$("#videoribbon .gallery_carousel .gCarousel").jCarouselLite({
	  btnNext: "#videoribbon .next",
	  btnPrev: "#videoribbon .prev",
	  visible: 5,
	  speed: 500,
	  circular: true
	});	
	$("#kinotavr .jCarouselLite").jCarouselLite({
	  btnNext: "#kinotavr .next",
	  btnPrev: "#kinotavr .prev",
	  visible: 1,
	  speed: 500,
	  btnGo: ["#kinotavr .1", "#kinotavr .2", "#kinotavr .3", "#kinotavr .4", "#kinotavr .5", "#kinotavr .6", "#kinotavr .7"],
	  circular: false
	});
	$("#kinotavr .gallery_carousel .gCarousel").jCarouselLite({
	  btnNext: "#kinotavr .next",
	  btnPrev: "#kinotavr .prev",
	  visible: 5,
	  speed: 500,
	  circular: true
	});
	$(".news").News({
	  visible: 6,
	  speed: 3000,
	  auto: 3000,
	  vertical: true
	});
	$(".size a").each(function () {
		$(this).click(function () {
			$(".size a").removeClass("curr");
			$(this).addClass("curr");
		});
	});
	$(".s1").click(function () {
		$(".font").css('font-size','13px');
		$(".font").css('line-height', '15px');
	});
	$(".s2").click(function () {
		$(".font").css('font-size','16px');
		$(".font").css('line-height', '19px');
	});
	$(".s3").click(function () {
		$(".font").css('font-size','18px');
		$(".font").css('line-height', '21px');
	});	
	$("#pickdate").hover(function () {
		$(".buttons").each(function () {
			$("a").removeClass("curr");
		});
		$(this).addClass("curr");
		$("#datepicker").datepicker({
			onSelect: function(date) {
				window.location = "?d="+date;
				}				
		});	
	});				
	$("#datepicker").hover(function () {
	},function () {
		$(".buttons").each(function () {
			$("a").removeClass("curr");
		});
		$('.all_').addClass("curr");				
		$("#datepicker").datepicker('destroy');
	});
	var zIndexNumber = 1000;
	$(".gallery").each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 10;
		var block = $('.pop', this);
		block.css({opacity: 0});
		$(this).mouseover(function () {
			block.css({display: 'block'});
			block.stop().animate({opacity: 1}, 400);
		}).mouseout(function () {
			block.stop().animate({opacity: 0}, 200, function () {
				block.css({display: 'none'});
			});
		});
	});
	$(".img_pos").each(function () {
		var height = $(this).height();
		var width = $(this).width();
		var margin_top = height/2;
		var margin_left = width/2;
		$(this).attr({height:height});
		$(this).css({"margin-left": "-"+margin_left+"px", "margin-top": "-"+margin_top+"px"});
	});
	
	


	
	$("#cross .select").click(function () {
		$("#cross input[name=n]").each(function () {
			
			var checked = $(this).attr('checked');
			
			
			
			if(checked==true){
				
				var value = $(this).val();
				
				if($.browser.msie && $.browser.version){
					$("#VID object").remove();
					$("#VID embed").remove();
					$("#VID").append('<EMBED src="'+value+'" type="application/x-mplayer2" autostart="1" stretchtofit="true" enablecontextmenu="0" showstatusbar="1" width="400" height="300"></EMBED>');
				
				}else{
					$("#crossVideo param[name=FileName]").val(value);
					$("#crossVideo embed").val(value);
					$("#crossVideo embed").attr("src", value);
					$("#crossVideo").appendTo('#VID');
				}
			}
		});	
	});

	if($('div.poll').length > 0) {
		$('div.poll').each(function() {
			aPolls = $(this).attr('id').match(/[\d\,]+/);
			aPolls = aPolls[0].split(',');
			
			for(var i in aPolls) {
				$(this).append('<div style="display:none;" id="poll_target_' + aPolls[i] + '"></div>');
			}
			
			aPolls.reverse();
			var pollid = aPolls.pop();
			if(document.cookie.match('zman_poll_' + pollid)) {
				$('#poll_target_' + pollid).load('/cgi-bin/polls.cgi?action=getPollResult&pollId=' + pollid, showVote(pollid));
				pollid = null;
				if(aPolls.length > 0) {				
					pollid = aPolls.pop();
					while(document.cookie.match('zman_poll_' + pollid)) {
						$('#poll_target_' + pollid).load('/cgi-bin/polls.cgi?action=getPollResult&pollId=' + pollid, showVote(pollid));
						pollid = null;
						if(aPolls.length == 0)
							break;
						pollid = aPolls.pop();
					}
				}
				if(pollid)
					$('#poll_target_' + pollid).load('/cgi-bin/polls.cgi?action=getPoll&pollId=' + pollid, showVote(pollid));
			}
			else {
				$('#poll_target_' + pollid).load('/cgi-bin/polls.cgi?action=getPoll&pollId=' + pollid, showVote(pollid));
			}
			
		});
	}
	
	if(url_hash = location.hash.substring(1)) {
	  if(requested_image_id = url_hash.match(/img(\d+)/)) {
	    $('.main_image').attr('src', '');
	    galleryId = $('.main_image').attr('id');
	    galleryId = galleryId.match(/\d+/);
	    load_requested_image(requested_image_id[1], galleryId[0], 1);
	  }
	}
	
});

load_requested_image = function(imageId, galleryId, ajax) {
	if(ajax == 1) {
	  $.get('/cgi-bin/gallery.cgi?action=getLocation&imageId=' + imageId + '&galleryId=' + galleryId, function(data) {
		    data = data.split('|');
		    $('.main_image').attr('src', data[0]);
		    $('.main_image').attr('alt', data[1]);
		    $('.main_image').attr('title', data[1]);
		    $('#current_image_description').html(data[1]);
		    location.hash = '#img' + imageId;
	  });
	}
	else {
	  window.location.href = window.location.href.replace(/#img\d+/, '#img' + imageId);
	  window.location.reload();
	}
}

hideVote = function(id) {
	 $('#poll_target_' + id).slideDown('slow');
}

showVote = function(id) {
	 $('#poll_target_' + id).slideDown('slow');
}

voteSubmit = function(pollid) {
	if($('input.selected').length == 0)
		return false;
	var AnswerId = '';
	if($('#polltype').val() == 'single') {
		AnswerId = $('input.selected').attr('id').match(/\d+/);
	}
	else {
		$('input.selected').each(function() {
			AnswerId += $(this).attr('id').match(/\d+/) + '_';
		});
		AnswerId = AnswerId.slice(0, -1);
	}
	hideVote(pollid);
	var sid = $("#poll_" + pollid + " [name=sid]").val();
	var captchaCode = $("#poll_" + pollid + " [name=captchaCode]").val();
	$.get('/cgi-bin/polls.cgi?action=vote&AnswerId=' + AnswerId + '&pollId=' + pollid + '&sid=' + sid + '&captchaCode=' + captchaCode, function(data) {
		
		$("#poll_" + pollid + " .error").hide();
		if(data.split("|")[0]=="0") {
			$("#poll_" + pollid + " .error").html(data.split("|")[1]);
			$("#poll_" + pollid + " .error").fadeIn('fast');
			return;
		}		
		
		$('#poll_target_' + pollid).load('/cgi-bin/polls.cgi?action=getPollResult&pollId=' + pollid, showVote(pollid));
		if(aPolls.length > 0) {
			pollid = aPolls.pop();
			$('#poll_target_' + pollid).load('/cgi-bin/polls.cgi?action=getPoll&pollId=' + pollid, showVote(pollid));
		}
	});
	return false;
}

addToFavorites = function(url, title) {
  try {
    if (jQuery.browser.mozilla) {
      window.sidebar.addPanel (title,url, "");
    } else if (jQuery.browser.msie) {
      window.external.AddFavorite(url, title);
    } else {
      alert('Нажмите комбинацию клавиш Ctrl+D для добавления страницы ва закладки');
    }
    return false;
  }
  catch(e) { 
    return false;
  }
}
setHomePage = function(url) {
  try { 
    if (jQuery.browser.msie) {
      document.body.style.behavior='url(#default#homepage)'; 
      document.body.setHomePage(url);
    }
    return false; 
  }
  catch(e) { 
    return false;
  }
}

