//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=",,,,,";



$(document).ready(function(){
	
	$(".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();
	});	
	

	

	$("div.lead p").removeAttr("style");
	$("div.lead div").removeAttr("style");
	$("div.lead span").removeAttr("style");
	
	$("div.lead img").css("border","10px solid #ffffff");
	$("div.rec_image img").css("border","5px solid #ffffff");
	$("div.lead p").css("margin","0px");
	$("div.lead p").css("padding","0px");
	
	if ($.browser.safari){
		$("div.c_center").css("margin-left","1px");
	}
	
	$('a.lightbox').lightBox();
	if (cyrylica==1){
		Cufon.replace('.cf', { fontFamily: 'Gotham Book' });
		Cufon.replace('.cfb', { fontFamily: 'Gotham Medium' });
		
	} else {
		Cufon.replace('.cf', { fontFamily: 'Gotham Book' });
		Cufon.replace('.cfb', { fontFamily: 'Gotham Medium' });

	}
	// sifr start
 
	
	// 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");
	});
	

	
	$('.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) });

	


	
	if (hp==1){
		
		wysokosc=(getMyHeight()-90);
		if(wysokosc>560){
			wysokosc=560;
		}
		$("#content_header_hp").css("height",wysokosc);
		
		
		//var fo = new FlashObject("/tpl/swf/avonFashionMain"+(cyrylica==1?"_cyr":"")+".swf?contentXmlPath=../../cache/"+PREFIX+"_homepage.xml&amp;menuXmlPath=../../cache/"+PREFIX+"_menu.xml", "swf_top_flash", "100%", wysokosc , "8", "");
		var fo = new FlashObject("/tpl/swf/preloader.swf?mainPath=/tpl/swf/avonFashionMain"+(cyrylica==1?"_cyr":"")+".swf&contentXmlPath=../../cache/"+PREFIX+"_homepage.xml&amp;menuXmlPath=../../cache/"+PREFIX+"_menu.xml", "swf_top_flash", "100%", wysokosc , "8", "");		
		
		fo.addParam("allowScriptAccess", "always");
		fo.addParam("quality", "high");
		fo.addParam("scale","exactfit");
		fo.write("content_header_hp");


		if (window.addEventListener){
        /** DOMMouseScroll is for mozill. */
			window.addEventListener('DOMMouseScroll', function(){
				//alert("aa");
				return false;
			}, false);
		}
/** IE/Opera. */
		window.onmousewheel = document.onmousewheel = function(){
			return false;
		};


		
		
	} else {
		toolAutoScroll();
	}
	
	
	
});

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);
	}
}


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;
	});
}


/*
var height = $("body").height();
var cloudHeight = $("#cloud").height();
	
function toolAutoScroll(){

	var pageScrl = $(document).scrollTop();
	var toolScrl = $('#cloud').offset({ scroll: false });
	if (pageScrl < 90) {
		$('#cloud').animate({top: 0},750, 'swing', function(){
			window.setTimeout('toolAutoScroll()', 50)
		});
	} else {
	
		var newTop = pageScrl-90;
		
		if (newTop > height-cloudHeight){
			newTop = height-cloudHeight-300;
		}
		
		$('#cloud').animate({top: newTop},750, 'swing', function(){
			window.setTimeout('toolAutoScroll()', 50)
		});
	}


}
*/
	
	
	
function toolAutoScroll(){
	var height = $("body").height();
	var cloudHeight = $("#cloud").height();
	var pageScrl = $(document).scrollTop();
	var toolScrl = $('#cloud').offset({ scroll: false });
	
	if (pageScrl < 90) {
		$('#cloud').animate({top: 0},750, 'swing', function(){
			window.setTimeout('toolAutoScroll()', 50)
		});
	} else {
	
		if (pageScrl > height-cloudHeight-80){
			pageScrl = height-cloudHeight-80;
		}
		var newTop = pageScrl-90;
//		$("#xxx").html("NewTop " + newTop + "<br> pageScr " + pageScrl + "<br> h "  + height+ "<br> ch " + cloudHeight);
		
		$('#cloud').animate({top: newTop},750, 'swing', function(){
			window.setTimeout('toolAutoScroll()', 50)
		});
	}


}


	
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();
			});
		});
		
	});
	
	
}
