var regex_mail = /^[a-z0-9_\-]+(\.[_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 rozwinietyNewsletter=false;
var timeOutChecker;
var regex_mail = /^[a-z0-9_\-]+(\.[_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)$/;

Cufon.replace('.cf, h1, h2');
	
	


function myMouseXY(e) {
	if (IE) {
		try { 
			pX = event.clientX + document.body.scrollLeft;
			pY = event.clientY + document.body.scrollTop;
		} catch(err){
		}
	} else {
		pX = e.pageX;
		pY = e.pageY;
	}
	if (pX < 0) pX = 0;
	if (pY < 0) pY = 0;
	return true;
}

var IE = document.all ? true : false;
if (!IE) document.captureEvents(Event.MOUSEMOVE);
document.onmousemove = myMouseXY;
var pX = 0;
var pY = 0;
$(document).ready(function(){
	//alert(___getPageSize());
	$('.social a').click(function(){
		$(this).attr('target','_blank');
	});
	$("#d1").hover(
		function(){
			$(".dymek_1").show();
		}, function(){
			$(".dymek_1").hide();
		}
	);
	
	$("#d2").hover(
		function(){
			$(".dymek_2").show();
		}, function(){
			$(".dymek_2").hide();
		}
	);

	$("#d3").hover(
		function(){
			$(".dymek_3").show();
		}, function(){
			$(".dymek_3").hide();
		}
	);
	
	
	$(".newsletter_close").click(function(){
		zwinNewsletter();
	});

	$(".r_close").click(function(){
		closeRecommend();
	});
	
	$("#bf_submit").click(function(){
		$("#bf_search").submit();
	});

	$("#pf_submit").click(function(){
		$("#pf_search").submit();
	});
	
	$("#notices1").click(function(){
	
		$(".og_content").show();
		$(".zg_content").hide();
		$(".za_content").hide();
		$("#notices1").removeClass("zgloszenia");
		$("#notices1").addClass("ogloszenia");
		$("#notices2").removeClass("ogloszenia");
		$("#notices2").addClass("zgloszenia");
		$("#notices3").removeClass("zapowiedzi2");
		$("#notices3").addClass("zapowiedzi");
		Cufon.replace('.cf, h1, h2');
	});
	$("#notices2").click(function(){
	
		$(".zg_content").show();
		$(".og_content").hide();
		$(".za_content").hide();
		$("#notices1").removeClass("ogloszenia");
		$("#notices1").addClass("zgloszenia");
		$("#notices3").removeClass("zapowiedzi2");
		$("#notices3").addClass("zapowiedzi");
		$("#notices2").removeClass("zgloszenia");
		$("#notices2").addClass("ogloszenia");	
		Cufon.replace('.cf, h1, h2');		
	});
	$("#notices3").click(function(){
	
		$(".za_content").show();
		$(".og_content").hide();
		$(".zg_content").hide();
		$("#notices1").removeClass("ogloszenia");
		$("#notices1").addClass("zgloszenia");
		$("#notices2").removeClass("ogloszenia");
		$("#notices2").addClass("zgloszenia");
		$("#notices3").removeClass("zapowiedzi");
		$("#notices3").addClass("zapowiedzi2");		
		Cufon.replace('.cf, h1, h2');		
	});
	
	$(".nletter").mouseout(function(){
		//timeOutChecker = setTimeout("zwinNewsletter()",4000);
	});
	$(".nletter").mouseover(function(){
		//clearTimeout(timeOutChecker);
	});
	
	$("body").click(function(){

	});

	doScroll(0)
	if ($.browser.msie){
		//	DD_belatedPNG.fix('.pngfix, .menu_dot');
	}
	
	$(".nletter_params").click(function(){
	//alert("s");
	});
	
	$(".nletter_submit").click(function(){
		//alert("a"); 
		
		if ($(".nletter_params").css("display") != "block"){
		$(".nletter_submit").fadeOut();
		$(".nletter").css("background","#000000");
		$(".nletter").animate( { height:"130px" }, { queue:false, duration:500 } )
         .animate( { marginTop:"-100px" }, 500 ,"", function(){
		 //alert("ok");
		 rozwinietyNewsletter=true;
		 $(".nletter_params").slideDown("normal", function(){
			$(".newsletter_close").fadeIn();
			$(".nletter_submit").fadeIn();
		 });
		 $(".nletter_submit").css("margin-top","105px");
		 $(".nletter_load").css("margin-top","105px");
		 });
	
//         .animate( { borderRightWidth:"15px" }, 1500);
	} else {
	
	 // ajax request
		if (regex_mail.test($('input[name=adres]').val()) == false){
			if (prefix=="pl"){
				alert("Niepoprawny adres email");
			} else {
				alert("Wrong email address");
			}
			$(".nletter_submit").show();
			$(".nletter_load").hide();			
			return;
		}
		
		var ch1 = ($('input[name=ch1]:checked').val()==1)?1:0;
		var ch2 = ($('input[name=ch2]:checked').val()==1)?1:0;
		var  ch3 = ($('input[name=ch3]:checked').val()==1)?1:0;
		
		var url="/ajax/subscribeNewsletter"+ajaxSeparator+$('input[name=adres]').val()+ajaxSeparator+$('input[name=sub]:checked').val()+ajaxSeparator+ch1+ajaxSeparator+ch2+ajaxSeparator+ch3+ajaxSeparator+prefix;
		$.get(url,"",function(result){
			$(".nletter_checks").html(result);
			$(".nletter_submit").show();
			$(".nletter_load").hide();
		});
		
		$(".nletter_submit").hide();
		$(".nletter_load").show();
	}
	
	
	});
	
	
	$('.nletter_radio input').click(function() {
		if ($(this).val() == '1') {
			$('.nletter_unsub').hide();
			$('.nletter_sub').show();
		}
		else {
			$('.nletter_sub').hide();
			$('.nletter_unsub').show();
		}
	});
	
	$('#f_search').focus(function(){
	    	var $input = $(this);
	    	if ($input.val() == $input.next().val()) {
	    		$input.val('');
	    	}
	    }).blur(function(){
	    	var $input = $(this);
	    	if ($input.val() == '') {
	    		$input.val($input.next().val());
	    	}
	    });
	
});

 

function zwinNewsletter(){
	$(".newsletter_close").fadeOut();
	$(".nletter_params").slideUp();
	$(".nletter").animate( {marginTop:"0px" }, { queue:false, duration:500 } )
        .animate( { height:"30px"  }, 500 ,"", function(){
	 
	 rozwinietyNewsletter=false;
	 $(".nletter_submit").css("margin-top","5px");
	 $(".nletter_load").css("margin-top","5px");
	 $(".nletter").css("background","none").css('background-color','#000000');
	 });
	 
	if (prefix=="pl"){	
		$(".nletter_checks").html('<input type="checkbox" value="1" id="ch1" checked="checked" name="ch1"/> PISFletter <br/>					<input type="checkbox" value="1" id="ch2" checked="checked" name="ch2"/> Pierwsze ujęcie<br/>					<input type="checkbox" value="1" id="ch3" checked="checked" name="ch3"/> Nadchodzące wydarzenia<br/>');
	 } else {
		$(".nletter_checks").html('<div style="display:none;"><input type="checkbox" value="1" id="ch1" checked="checked" name="ch1"/> PISFletter <br/></div><div>'+newsletterLead+'</div>');	 
	 }
	 
	 $("input[name=adres]").val("");	
	 
	 
}

function doScroll(pos) {
	if (typeof(message)=="string"){
	if (message.charAt(pos) != '|') {
		txt = txt + message.charAt(pos);
		$('input[name=scroll]').val(txt);
		pauza = 30;
    } else {
		pauza = 3200;
		txt   = ""
		if (pos == message.length-1) pos = -1
    }
    pos++;
    setTimeout("doScroll('"+pos+"')",pauza);
	}
}

function changeCalendar(m,y){
	//location.href="calendar.php?month="+m+"&year="+y;
	$('#write_calendar').hide();
	$('#calendar_load').show();
	$.get("/ajax/calendar"+ajaxSeparator+m+ajaxSeparator+y,"",function(result){
		$('#write_calendar').html(result);
		$('#calendar_load').hide();
		$('#write_calendar').show();
	});
}

function doEvent(id){
	//alert(id);
	
	//location.href="/pl/kinematografia/news";
	//alert(eventLink[id]);
	//return;
	location.href=eventLink[id];
}


function doReload(a){
	$("#trailers").fadeOut("normal",function(){
		$(".trailer_load").show();
		$("#trailers").load("/ajax/mainMovie"+ajaxSeparator+a+ajaxSeparator+prefix,"",function(){
			Cufon.replace('.cf, h1, h2');
			$(".trailer_load").hide();
			$("#trailers").fadeIn();
		});
			
	});
}

function doHref(add){
	location.href=add;
}

function js_fonts(nr){
	$(".letter").css("font-weight", "normal");
	$(".letter").css("color", "rgb(200,200,200)");
	
	$(".lett"+nr).css("font-weight", "bold");
	$(".lett"+nr).css("color", "#ffffff");
  switch (nr){
	case 1:$(".p_center").css("font-size","11px"); return false; break;
	case 2:$(".p_center").css("font-size","12px"); return false; break;
	case 3:$(".p_center").css("font-size","13px"); return false; break;
  }
  return false;
}
 
function CreateBookmarkLink() {
 title = "Polski Instytut Sztuki Filmowej"; 
 url = "http://www.pisf.pl";
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
 }

function print_(url){
	okno = window.open(url, "", "width=600,height=600,location=no,menubar=no,scrolling=yes,scrollbars=yes");
} 
 
 
 function reccomend(){
	$('embed, object, select').css({ 'visibility' : 'hidden' });
	var sizes=___getPageSize();
	$("body").append("<div class='rec' style='z-index:1000;display:none;filter: alpha(opacity=50); -moz-opacity: 0.5; opacity: 0.5;position:absolute;top:0;left:0;width:"+sizes[0]+"px;height:"+sizes[1]+"px;background:#000000;'></div> ");
	//DD_belatedPNG.fix('.recommend_wrap');
	$(".rec").fadeIn("", function(){
		$("body").append("<div class='powiadom'></div>");
			$(".powiadom").slideDown("", function(){
				$(".powiadom").load("/recommend");
				
			});
	});
	
	
 }
 
 
 function closeRecommend(){
 
	$(".powiadom, .rec").fadeOut("fast",function(){
		$(".powiadom, .rec").remove();
		$('embed, object, select').css({ 'visibility' : 'visible' });
	});
	
	
 }
 
 
function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};

function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};


		
		
		
		
		
		
		
		
function test(){
	if ($.browser.msie && $.browser.version==6){
	//alert($.browser.version);
		$(".p_left").css("margin-left","10px");
		$(".p_right").css("margin-left","0px");
	}
}		
		
		
		
	

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("/ajax/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","");
				}); 
				
			});
		});
	}
}	
