//alert(ajaxURL);
var regex_mail = /^[A-Za-z0-9_\-]+(\.[A-Z_a-z0-9\-]+)*@([_a-z0-9\-]+\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$/;
var ajaxSeparator=",,,,,";
var cyrylica=0;
var ajaxURL="/ajax/";


$(document).ready(function(){
	
	if ($('.lipdubStar').length) { // sprawdza, czy jestesmy na podstronie z opisem punktu, by mozna bylo glosowac
		if ($('#userId').val().length && !$('#voted').val().length)
		{
			$('div.lipdubStar span.vote').hover(
				function() {
					var rate = $(this).attr('class');
					rate = rate.replace("vote point", "");
					$(this).parent().css('background-position', '0 '+((-300+(rate*30)))+'px');
				},
				function () {
					var org_rate = $(this).parent().attr('id');
					org_rate = org_rate.replace("org", "");
					$(this).parent().css('background-position', '0 '+org_rate+'px');
				}
			);
		}
	}

	$('.lipdubStar span.vote').click(function() {
		var rate = $(this).attr('class');
		rate = rate.replace("vote point", "");
		movieId = $("#movieId").val();
		$('#loaderVote').show();
		$.getJSON(ajaxURL+"vote_lipdub"+ajaxSeparator+rate+ajaxSeparator+movieId, function(result){
			$("#libdubRating").html(result[1]);
			$(".lipdubStar").css('background-position', '0 '+result[3]+'px');
			$(".lipdubStar").attr('id', 'org'+result[3]);
			$('#thankYouVoteComment').text(result[2]).show();
			$('#loaderVote').hide();
		});
		return false;
	});

	
	
	$(".reload_captcha").click(function(){
		
		reloadCaptcha();
	});
	
	$("span.left").click(function(){
		$(this).parent().find("span.right").click();
	});
	
	$("span.left").next().click(function(){
		$(this).parent().find("span.right").click();
	});	
	

 
 
	
	// sifr end
	
	
	$("#searchSubmit").click(function(){
		location.href=$("#linkSearch").val()+"/key:"+$("#search").val();
		return false;
	});
	
	$("#searchForm").submit(function(){
		$("#searchSubmit").click();
		return false;
	});
	
	$("#c_reset").click(function(){
		$("#comment").val("");
		$("#email").val("");
		$("#autor").val("");
		$("input[name=agreement]").removeAttr("checked");
	});
	

	if (!$('.lipdubStar').length) {
		$('.point1').mouseover(function(){ 	$(this).parent().css("background-position","0px -25px");	});
		$('.point2').mouseover(function(){ 	$(this).parent().css("background-position","0px -50px");	});
		$('.point3').mouseover(function(){ 	$(this).parent().css("background-position","0px -75px");	});
		$('.point4').mouseover(function(){ 	$(this).parent().css("background-position","0px -100px");	});
		$('.point5').mouseover(function(){ 	$(this).parent().css("background-position","0px -125px");	});
		$('.point6').mouseover(function(){ 	$(this).parent().css("background-position","0px -150px");	});
		$('.point7').mouseover(function(){ 	$(this).parent().css("background-position","0px -175px");	});
		$('.point8').mouseover(function(){ 	$(this).parent().css("background-position","0px -200px");	});
		$('.point9').mouseover(function(){ 	$(this).parent().css("background-position","0px -225px");	});
		$('.point10').mouseover(function(){	$(this).parent().css("background-position","0px -250px");	});
		$('.point1').click(function(){ doVote(1) });
		$('.point2').click(function(){ doVote(2) });
		$('.point3').click(function(){ doVote(3) });
		$('.point4').click(function(){ doVote(4) });
		$('.point5').click(function(){ doVote(5) });
		$('.point6').click(function(){ doVote(6) });
		$('.point7').click(function(){ doVote(7) });
		$('.point8').click(function(){ doVote(8) });
		$('.point9').click(function(){ doVote(9) });
		$('.point10').click(function(){ doVote(10) });
	}
	


	
	
	
	
	
});

function doVote(score){
	var id = $("#photoid").val();
	//alert(id);
	$(".voting_stars").fadeOut("",function(){
		$(".v_load").show();
			//alert(ajaxSeparator+score+ajaxSeparator+id);
			
			$.getJSON(ajaxURL+"/vote"+ajaxSeparator+score+ajaxSeparator+id+ajaxSeparator+$("#thispage").val(),"", function(result){
			
				$(".v_load").hide() 
				$(".stars").html(result[1]);
				$("#wynik").html(result[2]);
					
					Cufon.replace('.cf', { fontFamily: 'Gotham Book' });
					Cufon.replace('.cfb', { fontFamily: 'Gotham Medium' });
				$(".stars").show();
			});
		 
	})
}

function getComments(item, lang, oid, timestamp, doRepeat){
	$("#c_load").show();
	var timestamp = $("#timestamp").val();
	$.getJSON(ajaxURL+"/getcomments"+ajaxSeparator+item+ajaxSeparator+lang+ajaxSeparator+oid+ajaxSeparator+timestamp, "", function(result){
		if (result[0]>0){
			$("#comments_zone").append(result[1]);
			$("#timestamp").val(result[2]);
			$(".comment").slideDown();	
		}
		$("#c_load").hide();
	});
	
	if (doRepeat==1){
		setTimeout(function(){
			getComments(item, lang, oid, timestamp,1);
		},5000);
	}
}

var lockMe=0;
function getLDComments(item, lang, oid, timestamp, doRepeat){
	//$("#c_load").show();
	lockMe=1;
	var timestamp = $("#timestamp").val();
	$.getJSON(ajaxURL+"/ldgetcomments"+ajaxSeparator+item+ajaxSeparator+lang+ajaxSeparator+oid+ajaxSeparator+timestamp, "", function(result){
		if (result[0]>0){
			$("#comments_zone").append(result[1]);
			
			$(".comment").show();	
			
		}
		$("#timestamp").val(result[2]);
		lockMe=0;
		//$("#c_load").hide();
	});
	
	if (doRepeat==1){
		setTimeout(function(){
			if (lockMe==0){
				getLDComments(item, lang, oid, timestamp,1);	
			}
			
		},10000);
	}
}


function loveithateit(item, lang, oid, vote){
	$("#load"+item).show();
	$.getJSON(ajaxURL+"/loveithateit"+ajaxSeparator+item+ajaxSeparator+lang+ajaxSeparator+oid+ajaxSeparator+vote, "", function(result){
		$("#loveit"+item).html(result.loveit);
		$("#hateit"+item).html(result.hateit);
		$("#load"+item).hide();
		if (result.valid!=1){
			//alert("xx");
			
			$("#hateit_"+item).fadeOut("fast", function(){
				$("#loveit_"+item).fadeOut("fast", function(){
					$("#error"+item).fadeIn("fast", function(){
						setTimeout(function(){
							$("#error"+item).fadeOut("fast", function(){
								$("#loveit_"+item).fadeIn("fast", function(){
									$("#hateit_"+item).fadeIn("fast", function(){
									});
								});
							
							});
							
						},1000);
					});
				});
			});
			
		}
	});
}



function submitPool(id){
	
	//return;
	z=$("input[name=poll_"+id+"]:checked").val();
	if (z!=undefined){
		$("#main_pool_submit").fadeOut("", function() {
			$("#main_pool_data"+id).slideUp("slow", function(){
				var poolId = id;
				var answerid = z;
				var lang = $("#lang").val(); 
				
				$.getJSON(ajaxURL+"/pollvote"+ajaxSeparator+poolId+ajaxSeparator+answerid+ajaxSeparator+lang, "", function(result){
					
					if (result[1]==1){
					// alert("ok");
					} else {
					//alert("głosowałeś wcześniej!");
					}
					
					for (i in result[2]){
						$("#td"+i).html(result[2][i]+"%");
					}
					$("#main_pool_data"+id).slideDown("slow","");
				}); 
				
			});
		});
	}
}


function getMyHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
  return myHeight;
}


function logout(){
	$.get(ajaxURL+"/logout","",function(result){
		location.href = result;
	});
}


 
	 
	
function reloadCaptcha(){
	//$("span.reload_captcha").slideUp("normal", function(){
		$("img.dest_captcha").fadeOut(function(){
			$("img.dest_captcha").attr("src","/tpl/captcha/securimage_show.php?rel="+(Math.random()*100000));
			$("img.dest_captcha").fadeIn(function(){
				$("span.reload_captcha").slideDown();
			});
		});
		
	//});
	
	
}
