//(c) 2005-2011 by AD
function opw(p,n,w,h){
	var w=window.open(p,n,'toolbar=no,scrollbars=yes,width='+w+',height='+h+',resizable=yes',false);
	w.focus();
}
function opwns(p,n,w,h){
	var w=window.open(p,n,'toolbar=no,scrollbars=no,width='+w+',height='+h+',resizable=yes',false);
	w.focus();
}
function printo(obj){
	if (typeof(console)!='undefined' && console.debug) console.debug(obj);
}
function trim(s){
	if (typeof(s)!='string') s='';
	return s.replace(/^\s+|\s+$/g,'');
}
function chkemail(fld,msg){
	var r=/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@[a-zA-Z0-9]([a-zA-Z0-9_-])*(\.[a-zA-Z0-9_-]+)+$/;
	var str=trim(fld.value);
	if (r.test(str)) return true;
	else{alert(msg);fld.focus();return false;}
}
function chktxtfld(fld,msg1,msg2,len){
	var str=trim(fld.value);
	if (str.length==0){alert(msg1);fld.focus();return false;}
	else if (str.length>len){alert(msg2.replace(/\{0\}/,len));fld.focus();return false;}
	else return true;
}
function delegate(fnc,cntx,args){
	if (args==(void 0)) args=[];
	return function(){fnc.apply(cntx,args);};
}

if (typeof(CE)=='undefined') CE={};

//rotazione bottom logos
CE.BL = (function(){
	var curri, num, items;
	var ttl=5*1000, fade=0.5*1000, easing='swing';;
	var hide={'opacity':0.0,'z-index':-100}, show={'opacity':1.0,'z-index':100};
	return {
		init : function(){
			items=$('#bb_container p');
			num=items.length;
			if (num>0){
				curri=num-1;
				items.css(hide).eq(curri).css(show);
				this.rotate2();
			}
		},
		rotate : function(){
			var newi=curri-1; if (newi<0) newi=num-1;
			var obj=items.eq(curri);
			obj.animate(hide,{duration:fade,easing:easing,complete:function(){obj.hide();}});
			items.eq(newi).show().animate(show,{duration:fade,easing:easing,complete:delegate(this.rotate2,this)});
			curri=newi;
		},
		rotate2 : function(){
			window.setTimeout(delegate(this.rotate,this),ttl);
		}		
	}
})();

//video
CE.Video = (function(){
	return {
		initEmbed : function(){
			var ec=function(ev){
				var el=$('#frmembed2');
				el.toggle();
				if (el.css('display')=='block') $('#txtembed').focus();
			};
			var ef=function(ev){this.select();}
			$('#btnembed').click(ec);
			$('#txtembed').focus(ef);
		},
		initBanner : function(secs){
			var vc=$('#videocontainer');var vb=$('#videobanner');var vs=$('#videoseconds');var me=this;
			function setsecs(){
				if(vs);vs.html(secs);
				if(secs<=0){
					vc.show();vb.hide();me.show();
				}else{
					secs--;window.setTimeout(setsecs,1000);
				}
			}
			if(vc && vb){vc.hide();vb.show();setsecs();}else me.show();
		},
		show : function(){
			if($.isFunction(__vd))__vd();
		},
		//flashLoaded : function(url){
		//	return function(e){
		//		jwplayer(e.ref).onPause(function(ev){var w=window.open(url,'cineuropa');w.focus();});
		//	};
		//}
		flashLoaded : function(url){
			return function(ev){var w=window.open(url,'cineuropa');w.focus();};
		}
	};
})();

//per i 'blocking scripts'
CE.addScript = function(url, c, cb) {
	var g = document.createElement('script'),
		s = document.getElementsByTagName('script')[0];
	g.type = 'text/javascript';
	g.async = true;
	g.src = url;
	if (c) try {g.innerHTML = c;}catch(ex){}
	if (cb) {
		g.onload = cb;
		// IE 6 & 7
		g.onreadystatechange = function() { if (this.readyState == 'complete') cb(); }
	}
	s.parentNode.insertBefore(g, s);
};

//search
CE.initSearch = function(msg1,msg2){
	var chks=function(ev){
		var frm=$(this)[0]; 
		var val=trim(frm.textsearch.value);
		if (val!=frm.textsearch.value) frm.textsearch.value=val;
		if(val==msg1||val.length<1){alert(msg2);frm.textsearch.focus();return false;}
		return true;
	};
	var clr1=function(ev){var el=$(this)[0]; if (el.value==msg1) el.value=''; $(this).css('font-style','normal');};
	var clr2=function(ev){var el=$(this)[0]; if (trim(el.value).length==0||el.value==msg1){ el.value=msg1; $(this).css('font-style','italic');} else  $(this).css('font-style','normal'); };
	$('.head-box .search-menu form').submit(chks).find('input[name="textsearch"]').focus(clr1).blur(clr2).map(clr2);
	$('#cc form.search-form').submit(chks).find('input[name="textsearch"]').focus(clr1).blur(clr2).map(clr2);
};
CE.initSearchPage = function(msg){
	var seldesall=function(ev){
		var frm=$(this)[0].form;
		for(var i=0,found=false,v=false;i<frm.elements.length;i++){
			var el=frm.elements[i];
			if (el.type=='checkbox'){
				if (!found) { found=true; v=!el.checked; }
				el.checked=v;
			}
		}
	};
	var chksel=function(ev){
		var frm=$(this)[0], found=false, el1;
		for(var i=0;i<frm.elements.length;i++){
			var el=frm.elements[i];
			if (el.type=='checkbox') {
				if (!el1) el1=el;
				if (el.checked) { found=true; break; }
			}
		}
		if (!found) { alert(msg); if (el1) el1.focus(); }
		return found;
	};
	$('#cc form.search-form').submit(chksel).find('#seldesall').click(seldesall);
};

//filmsubmit
CE.initFilmSubmit = function(vreq,msg1,msg2,msg3,len1,len2){
	var vfs=function(ev){
		var frm=$(this)[0];
		for(var i in vreq){
			var s=vreq[i];var fld=frm['filmsubmit'+s];
			if((s=='email'&&!chkemail(fld,msg1))||(fld.type=='text'&&!chktxtfld(fld,msg2,msg3,len1))||(fld.type=='textarea'&&!chktxtfld(fld,msg2,msg3,len2))) return false;
		}
		return true;
	};
	$('#filmsubmit').submit(vfs);
};

//links
CE.initLinks = function(msg1,msg2,msg3){
	var chks=function(ev){
		var frm=$(this)[0];
		var txt=trim(frm.free.value);
		if(frm.cat.selectedIndex==0&&frm.ctr.selectedIndex==0&&(txt.length==0||txt==msg1)) { alert(msg2); frm.cat.focus(); return false; }
		else if(txt.length>0&&txt.length<3) { alert(msg3); frm.free.focus(); return false; }
		else { frm.free.value=txt; return true; }
	};
	var clr0=function(i,el){if (el.value==msg1) $(el).css('font-style','italic');else $(el).css('font-style','normal');};
	var clr1=function(ev){var el=$(this)[0]; if (el.value==msg1) el.value=''; $(this).css('font-style','normal');};
	var clr2=function(ev){var el=$(this)[0]; if (trim(el.value).length==0){ el.value=msg1; $(this).css('font-style','italic');} };
	var chg=function(ev){var frm$=$($(this)[0].form);/*if(chks.call(frm$,ev))*/frm$[0].submit();};
	$('#cc .search-news form.search-links').submit(chks).find('select').change(chg).end().find('input[name="free"]').focus(clr1).blur(clr2).map(clr0);
};

//twitter
CE.TW = (function(){
	var ttl=7*1000, ttlm=0.3, fade=0.5*1000, maxr=3, curri=0, objs, num, width;
	var hide={'opacity':0.0,'z-index':-100}, show={'opacity':1.0,'z-index':100};
	var itvl1=0, itvl2=0, easing='swing';
	return {
		init:function(){
			objs=$('#ir .title .tweetc .tweet');
			objs.css(hide).eq(curri).css(show);
			num=objs.length;
			width=objs.parent().width();
			this.nextc();
		},
		next:function(){
			if (maxr>0) {
				var obj=objs.eq(curri);
				obj.animate(hide,{duration:fade,easing:easing,complete:function(){obj.hide();}});
				curri++; if (curri>=num) { curri%=num; maxr--; }
			}
			objs.eq(curri).css({left:0,top:0}).show().animate(show,{duration:fade,easing:easing,complete:delegate(this.nextc,this)});
		},
		nextc:function(){
			itvl1=window.setTimeout(delegate(this.next,this),ttl);
			var diff=objs.eq(curri).width()-width;
			itvl2=(diff>0?window.setTimeout(delegate(this.slide,this,[diff]),ttlm*ttl):0);
		},
		slide:function(diff){
			objs.eq(curri).animate({left:-diff-5},{duration:(1-2*ttlm)*ttl,easing:'linear'});
		}
	};
})();

//menu
CE.MN = (function(){
	var cobj=0, drt1=200, drt2=150, easing='swing';
	return {
		init:function(){
			$('.menu-hor .menu-arrow').mouseenter(this.anim(true)).mouseleave(this.anim(false));
		},
		anim:function(flag){
			return function(){
				if(flag && this!=cobj){
					if(cobj) $(cobj).find('.smenu').slideUp(drt2,easing);
					cobj=this;
					$(this).find('.smenu').stop(true,true).slideDown(drt1,easing);
				}else if (!flag && this==cobj){
					$(this).find('.smenu').slideUp(drt2,easing);
					cobj=0;
				}
			};
		}
	};
})();

//top focus
CE.FRT = (function(){
	var vdatas, curri, iwidth, anim, zidx, opct;
	var drt=600;
	return {
		init:function(v,ci,w){
			vdatas=v; curri=ci; iwidth=w; anim=false;
			$('#ir .container .arrow-left').click(this.rotate(true));
			$('#ir .container .arrow-right').click(this.rotate(false));
			var lbl=$('#ir .bottom .focus');
			zidx=parseInt(lbl.css('z-index'), 10); if (isNaN(zidx)) zidx=100;
			opct=parseFloat(lbl.css('opacity')); if (isNaN(opct)) opct=1.0;
			//$('#ir .image-box .image').css('visibility','visible');
		},
		rotate:function(prev){
			return function(){
				if (anim) return;
				var first, last;
				last=(first=$('#ir .image-box .image').first()).end().last();
				anim=true;
				if (prev) {
					curri--;if(curri<0)curri=vdatas.length-1;
				} else {
					curri++;if(curri>vdatas.length-1)curri=0;
				}
				var lbl1=$('#ir .bottom .focus'), lbl2=lbl1.clone().css({'z-index':0,opacity:0.0}).appendTo('#ir .bottom');
				var v=vdatas[curri];
				lbl2.find('.tit').html(v.t); lbl2.find('.dir').html(v.d); lbl2.find('a').attr('href',v.l);
				if(prev){
					last.css('left', parseInt(first.css('left'), 10) - iwidth).prependTo('#ir .image-box');
					$('#ir .image-box .image').animate({'left':'+='+iwidth},{duration:drt,easing:'easeOutExpo',complete:function(){anim=false;}});
				}else{
					first.css('left', parseInt(last.css('left'), 10) + iwidth).appendTo('#ir .image-box');
					$('#ir .image-box .image').animate({'left':'-='+iwidth},{duration:drt,easing:'easeOutExpo',complete: function(){anim=false;}});
				}
				lbl1.animate({/*'z-index':0,*/'opacity':0.0},{duration:drt,easing:'easeOutExpo',complete:function(){lbl1.remove()}});
				lbl2.animate({/*'z-index':zidx,*/'opacity':opct},{duration:drt,easing:'easeOutExpo',complete:function(){}});				
			};
		}
	};
})();

//links
CE.LNK = (function(){
	return{
		init:function(){
			var h=function(me,enter){ return function(ev){me.handler.call(me,enter,this,ev);}; };
			var me=this;
			$('#cc a[href]:not(a[href^="#"]):not(a[href^="javascript:"])').hover(h(me,true),h(me,false));
			$('#cc area[href]:not(area[href^="#"]):not(area[href^="javascript:"])').hover(h(me,true),h(me,false));
		},
		handler:function(enter,el,ev){
			var lnk=$(el).attr('href');
			var jq=$('#cc a[href="'+lnk+'"]');
			if (enter) jq.addClass('rollover'); else jq.removeClass('rollover');
		}
	};
})();

//right bar
CE.HRT = function(s,ci,w){
	this.section=s; this.curri=ci, this.iwidth=w;
	this.anim=false;
	this.ctrl=$('#cr .box-right.'+this.section+' .control-slide');
	this.wrpr=$('#cr .box-right.'+this.section+' .wrapper');
	this.imgs=$('#cr .box-right.'+this.section+' .wrapper img');
	this.count=$('a',this.ctrl).length;
	this.lastclick=0;
	var img=this.imgs.eq(this.curri);
	//occhio! .load non funziona su alcuni browser per immagini cached!!!!
	if (img[0].complete) this.initHeight();
	else img.load(delegate(this.initHeight,this));
	var me=this;
	$('a',this.ctrl[0]).click(function(){var el=this;me.rotate.call(me,el);});
	this.narotate=2*this.count-1;
	if (this.count>1) this.itvl=window.setTimeout(delegate(this.arotate,this),this.arotateint);
};
CE.HRT.prototype.initHeight=function(){
	var newh=$('div.box-right-pager',this.wrpr).eq(this.curri).outerHeight(true)+this.addh;
	this.wrpr.css('height',newh);
};
CE.HRT.prototype.arotate=function(){
	var newi=this.curri+1;
	if (newi>=this.count) newi=0;
	var noanim=((new Date()).getTime()-this.lastclick<this.arotateint);
	if (noanim||--this.narotate>0||newi!=0) this.itvl=window.setTimeout(delegate(this.arotate,this),this.arotateint);
	if (noanim) return;
	this.rotate2(newi);
};
CE.HRT.prototype.rotate=function(el){
	if (this.anim) return;
	var newi=parseInt($(el).html(),10)-1; if (isNaN(newi)) newi=0;
	this.lastclick=(new Date()).getTime();
	this.rotate2(newi);
};
CE.HRT.prototype.rotate2=function(newi){
	if (newi!=this.curri) {
		this.anim=true;
		var diff=(newi-this.curri)*this.iwidth;
		var newh=(this.imgs.eq(newi)[0].complete?$('div.box-right-pager',this.wrpr).eq(newi).outerHeight(true)+this.addh:0);
		$('span',this.ctrl).eq(this.curri).css('display','none'); //hide()
		$('a',this.ctrl).eq(this.curri).css('display','inline'); //show()
		this.curri=newi;
		var me=this;
		if (newh>0) {
			this.wrpr.animate({'height':newh},{duration:this.drt,easing:this.easing,complete:function(){}});
		} else {
			this.imgs.unbind('load');
			this.imgs.eq(this.curri).load(function(){me.initHeight.call(me);});
		}
		$('.box-right-pager',this.wrpr).animate({'left':'-='+diff},{duration:this.drt,easing:this.easing,complete:function(){me.rotateComplete.call(me);}});
	}
};
CE.HRT.prototype.rotateComplete=function(){
	$('span',this.ctrl).eq(this.curri).css('display','inline'); //show()
	$('a',this.ctrl).eq(this.curri).css('display','none'); //hide()
	this.anim=false;
}
CE.HRT.prototype.easing='swing'; //'easeOutExpo';
CE.HRT.prototype.arotateint=7000;
CE.HRT.prototype.drt=600;
CE.HRT.prototype.addh=0;

//news right bar
CE.NRT = function(ci,w){
	this.curri=ci, this.iwidth=w;
	this.next=(this.prev=$('#cr .box-right.related .control-slide a').first()).end().last();
	this.nextph=(this.prevph=$('#cr .box-right.related .control-slide .placeholder').first()).end().last();
	this.wrpr=$('#cr .box-right.related .wrapper');
	this.count=$('div.box-right-pager',this.wrpr).length;
	if (this.curri>=this.count) this.curri=this.count-1;
	this.lastclick=0;
	this.rotateComplete();
	var me=this;
	var newh=$('div.box-right-pager',this.wrpr).eq(this.curri).outerHeight(true)+this.addh;
	this.wrpr.css('height',newh);
	this.prev.click(function(){me.rotate.call(me,-1);});
	this.next.click(function(){me.rotate.call(me,+1);});
	//this.itvl=window.setInterval(function(){me.arotate.call(me);},this.arotateint);
};
CE.NRT.prototype.arotate=function(){
	if ((new Date()).getTime()-this.lastclick<this.arotateint) return;
	var newi=this.curri+1;
	if (newi>=this.count) newi=0;
	this.rotate2(newi);
};
CE.NRT.prototype.rotate=function(inc){
	if (this.anim || inc==0) return;
	var newi=(this.curri+inc)%this.count;
	this.lastclick=(new Date()).getTime();
	this.rotate2(newi);
};
CE.NRT.prototype.rotate2=function(newi){
	if (newi!=this.curri) {
		this.anim=true;
		var diff=(newi-this.curri)*this.iwidth;
		var newh=$('div.box-right-pager',this.wrpr).eq(newi).outerHeight(true)+this.addh;
		this.curri=newi;
		var me=this;
		this.wrpr.animate({'height':newh},{duration:this.drt,easing:this.easing,complete:function(){}});
		$('.box-right-pager',this.wrpr).animate({'left':'-='+diff},{duration:this.drt,easing:this.easing,complete:function(){me.rotateComplete.call(me);}});
	}
};
CE.NRT.prototype.rotateComplete=function(){
	if (this.curri>0) { this.prev.css('display','inline'); this.prevph.hide(); } else { this.prev.hide(); this.prevph.css('display','inline'); }
	if (this.curri<this.count-1) { this.next.css('display','inline'); this.nextph.hide(); } else { this.next.hide(); this.nextph.css('display','inline'); }
	//this.prev.css('visibility',(this.curri>0?"visible":"hidden"));
	//this.next.css('visibility',(this.curri<this.count-1?"visible":"hidden"));
	this.anim=false;
}
CE.NRT.prototype.easing='swing'; //'easeOutExpo';
CE.NRT.prototype.arotateint=7000;
CE.NRT.prototype.drt=600;
CE.NRT.prototype.addh=0;

