// JavaScript Document
var t;
function timetablescroll(straction){
	if(straction=='down'){
		if($('#timetable_inner').height() + parseInt($('#timetable_inner').css('margin-top')) > $('#timetable_window').height()){
			$('#timetable_inner').css('margin-top',parseInt($('#timetable_inner').css('margin-top'))-5+'px');
			t = setTimeout("timetablescroll('down');",60);
		}
	}
	else if(straction=='up'){
		if(parseInt($('#timetable_inner').css('margin-top')) < 0){
			$('#timetable_inner').css('margin-top',parseInt($('#timetable_inner').css('margin-top'))+5+'px');
			t = setTimeout("timetablescroll('up');",60);
		}
	}
	else if(straction=='stop'){
		clearTimeout(t);
	}
};

function selectCurrentProgram(){
	$("#timetable_inner .aftercurrent").removeClass('aftercurrent').prevAll("div.nowshowing-line:first").addClass('nowshowingcurrent');
	if($("#timetable_inner .nowshowingcurrent").find(".program a").length == 1)
		$("#timetable_inner .nowshowingcurrent").find(".program").append(' <a href="javascript:watchonline();" class="blue">watch now</a>');
	if($("#timetable_inner .nowshowingcurrent").length){
		try{
			var nowshowingcurrent = $("#timetable_inner .nowshowingcurrent").position().top;	
			$("#timetable_inner").css({marginTop:(nowshowingcurrent?0-nowshowingcurrent+50+'px':'0px')});
		}
		catch(err){}
	}
}

$(document).ready(function(){
	////////////CHECKBOX & RADIO BUTTON REPLACEMENTS///////
	$("input[type='checkbox']:not(.orange,.exclude)").checkbox({cls:'jquery-checkbox-blue'});
	$("input.orange[type='checkbox']:not(.exclude)").checkbox({cls:'jquery-checkbox-orange'});
	$("input[type='radio']:not(.orange,.exclude)").checkbox({cls:'jquery-radio-blue'});
	$("input.orange[type='radio']:not(.exclude)").checkbox({cls:'jquery-radio-orange'});
	
	/////////// SIDEBAR /////////////
	
	$(".plusminus").bind("click",function(){
		var $module = $("#"+$(this).attr("id").replace("_control","_module"));
		var strkey = $(this).attr("id").replace("_control","")
		if($module.is(":visible")){
			$module.slideUp(400);
			$(this).html('+');
			straction = 'removemodule';
		}else{
			$module.slideDown(400);
			$(this).html('&ndash;');
			straction = 'addmodule';
			if($(this).attr("id")=='nowshowing_control') selectCurrentProgram();
		}
		$.getJSON("processes/usersettings.php?straction="+straction+"&strkey="+strkey,function(data){});
	});
	
	//// PREFILLED INPUTS //////
	function prefilledField(id,j,strcolor){
		this.j = j;
		this.id = id;
		this.strdefaultval = this.j.val();
		this.strdefaultcolor = this.j.css("color");
		this.onclick = function(){
			if(this.j.val()==this.strdefaultval)
				this.j.val('').css('color',strcolor);
		}
		this.onblur = function(){
			if(this.j.val()=='')
				this.j.val(this.strdefaultval).css('color',this.strdefaultcolor);
		}
		this.onsubmit = function(){
			if(this.j.val()==this.strdefaultval)
				this.j.val('');
		}
		eval('this.j.bind("click",function(){arrprefilledfields['+this.id+'].onclick();});');
		eval('this.j.bind("blur",function(){arrprefilledfields['+this.id+'].onblur();});');
		eval('this.j.parents("form").bind("submit",function(){arrprefilledfields['+this.id+'].onsubmit();});');
	}
	var arrprefilledfields = [];
	$("input.prefilled").each(function(){
		var id = arrprefilledfields.length;
		arrprefilledfields[id] = new prefilledField(id,$(this),'#000');
	});
	$("input.prefilled_black").each(function(){
		var id = arrprefilledfields.length;
		arrprefilledfields[id] = new prefilledField(id,$(this),'#fff');
	});
	
	///////////NOW SHOWING////////////
	selectCurrentProgram();
	
	$("div.module-list > div:even").addClass('grey');
	
	$("#timetable_down").bind("mouseover",function(){
		timetablescroll('down');
	});
	$("#timetable_down").bind("mouseout",function(){
		timetablescroll('stop');
	});
	$("#timetable_up").bind("mouseover",function(){
		timetablescroll('up');
	});
	$("#timetable_up").bind("mouseout",function(){
		timetablescroll('stop');
	});
	$(".nowshowing-line:odd").addClass('grey');
	
	/////////// MY ACCTV /////////////
	$("ul.minitabs > li").bind("click",function(){
		parentul = $(this).parent();
		index = $(this).prevAll('li').length;
		$(this).addClass('selected').siblings('li').removeClass('selected');
		parentul.siblings("div.minitab").hide();
		parentul.siblings("div.minitab:eq("+index+")").show();
	});
	function submitform(formname){
		var strprocess = $("#login_strprocess").val();
		var strname = $("form[name='"+formname+"'] input[name='strname']").val();
		var stremail = $("form[name='"+formname+"'] input[name='stremail']").val();
		var strpassword = $("form[name='"+formname+"'] input[name='strpassword']").val();
		var intloginpersist = $("form[name='"+formname+"'] input[name='intloginpersist']").is(":checked")?1:0;
		$.getJSON("processes/login.php?strprocess="+strprocess+"&stremail="+stremail+"&strpassword="+strpassword,function(data){
			if(data.message){
				$("div.validation_"+formname).html('<ul>'+data.message+'</ul>').show();
			}else{
				if(intloginpersist){
					//set cookie
					var c_name = 'loginpersist';
					var value = data.memberkey;
					var expiredays = 10;
					var exdate=new Date();
					exdate.setDate(exdate.getDate()+expiredays);
					document.cookie=c_name+ "=" +escape(value)+
					((expiredays==null) ? "" : ";expires="+exdate.toUTCString())+
					";path=/";
				}
				window.location = windowLocation;
			}
		});
	}
	$("#btn_login").bind("click",function(){
		$("#login_strprocess").val('login');
		submitform('login');
		return false;
	});
	$("#btn_signup").bind("click",function(){
		$("#login_strprocess").val('signup');
		submitform('signup');
		return false;
	});
	$("a#logout").bind("click",function(){
		$.getJSON("processes/login.php?straction=logout",function(data){
			window.location = windowLocation;
		});
		return false;
	});
	
	///////////FLASH REPLACEMENT //////////////
	if(!hasFlash){
		$("object.flashobject").each(function(){
			$(this).hide();
			$(this).prev(".noflash").show();
		});
	}
	
	///////////OPEN FLV LINKS WITH FLOWPLAYER SHADOWBOX ////////////
	$("a[href$='.flv']").bind("click",function(){
		Shadowbox.open({
			width:640,
			height:380,
			content:'watch_video.php?video='+escape($(this).attr("href")),
			player:'iframe',
			title:$(this).attr("title")
		});
		return false;										   
	});
	
	/////////// TABLE FORMATTING /////////////
	$("table.viewdata tr:even td.data").addClass('grey');
	$("table.viewdata tr:even td.label").addClass('grey');
	
	////////////ROLLOVERS///////////////
	$("img.rollover,input.rollover").each(function(){
		var img = new Image();
		img.src = $(this).attr("src").replace(".","_o.");
		$(this).bind("mouseover",function(){
			$(this).attr("src",$(this).attr("src").replace(".","_o."));					  
		});
		$(this).bind("mouseout",function(){
			$(this).attr("src",$(this).attr("src").replace("_o.","."));					  
		});
	});	
	
	///////////SIDEBAR FEATURE PROGRAMS//////////////
	$("#sidebar .featureprogramlist:odd").addClass("featureprogramlistgrey");
	$("#sidebar .featureprogramlist").hoverIntent(function(){
		$(this).addClass("featureprogramselected").siblings().removeClass("featureprogramselected");
		$("#"+$(this).attr("id").replace("featureprogramlist_","featureprogramimg_")).show().siblings().hide();
	},function(){});
	
	//////////ROTATORS/////////////////
	$(window).load(function(){
		$(".rotator:not(#indexrotator)").each(function(){
			var options = {timeout:5000};
			switch($(this).attr("id")){
				case 'advert_banner':
					var options = {timeout:globalBannerSpeed};
					break;
			}
			$(this).cycle(options);
		});
	});
	
	
	/////////////COMPETITION SHADOWBOX////////////
	$("#comp_module a").bind("click",function(){
		Shadowbox.open({
			player:     'iframe',
			content:    $(this).attr("href"),
			width:540,
			height:650,
			options:	{
				modal:true
			}
		});
		return false;
	});
});

/////////// LINE UP THE BOTTOM OF THE CONTENT AREA /////////////
function alignwithsidebar(){
	if($("div#contentborder").length > 0){
		if($("#searchbar").length > 0){
			var sbheight = $("#searchbar").height()+1;
		}else{
			var sbheight = 0
		}
		$("div#contentborder").css('height','auto');
		var leftheight = $("div#contentborder").height() + sbheight;
		var sizeto = $("div#sidebar-margin").height() - sbheight;
		
		if($("div#sidebar-margin").height() > leftheight){
			//alert('sidebar: '+$("div#sidebar-margin").height()+', content: '+$("div#contentborder").outerHeight());
			$("div#contentborder").height(sizeto);				
		}
		else{
			$("div#contentborder").css('height','auto');
			//alert("auto height");
		}
	}
}
function addcontentbuffer(){
	var buffer = 50;
	if($("div#contentborder").length > 0){
		$("div#contentborder").css('height','auto');
		function column(jid){
			this.j = $(jid);
			this.top = this.j.offset().top;
			this.height = this.j.height();
			this.bottom = this.top+this.height;
		}
		var left = new column("div#contentborder");
		var right = new column("div#sidebar-margin");
		
		if(right.bottom > left.bottom + 50){
			left.j.height(left.height+50);
		}else if(right.bottom + 50 > left.bottom){
			left.j.height(right.bottom-left.top);	
		}
	}
}
/*
$(window).load(function(){
	alignwithsidebar();
	addcontentbuffer();
});
*/

/////////// FORM BUTTONS /////////////////
$(window).load(function(){
	//$("#topform form button,#topform form input[type='submit']").removeClass().addClass('button').addClass('wider');
	//alert($("#topform form button").length);
});

////////// LIST COLORING ////////////////
$(document).ready(function(){
	$("div.listitem:even").addClass('listitem-grey');
});

/////////SELECTED MENU ITEM ////////////
$(function(){
	if($("#subnav ul a.navcurrent").length){
		var pos = $("#subnav ul a.navcurrent").position().left;
		$("#subnav ul")
		.css("background-image","url(css/images/subnav_bg_selected.jpg)")
		.css("background-position",pos+"px 0px")
		.css("background-repeat","no-repeat");
	}
});

////////LOGIN & REGISTER Buttons //////////
$(function(){
	$("#tab_login").bind("click",function(){
		if($("#div_register").is(":visible")){
			$("#div_register").hide();
			$("#div_login").show();
		}else if($("#div_login").is(":visible")){
			$("#div_login").slideUp();
		}else $("#div_login").slideDown();
	});
	$("#tab_register").bind("click",function(){
		if($("#div_login").is(":visible")){
			$("#div_register").show();
			$("#div_login").hide();
		}else if($("#div_register").is(":visible")){
			$("#div_register").slideUp();
		}else $("#div_register").slideDown();
	});
});

////////WATCH ONLINE //////
function watchonline(){
	var w = 480;
	var h = 629;
	var l = (screen.availWidth-10 - w) / 2;
	var t = (screen.availHeight-20 - h) / 2;
	newWindow = window.open('watch.php', 'watchonline', 'fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=no,resizable=no,directories=no,location=no,width=' + w + ',height=' + h + ',left=' + l + ',top=' + t);
}
function changelocation(url){
	window.location = url;
}	

///// Articles /////////
$(function(){
	$("#dark_feature *[minheight]").each(function(){
		minheight = parseInt($(this).attr('minheight'));
		if($(this).height() < minheight){
			$(this).height(minheight);
		}
	});
	$("#dark_feature *[alignheight]").each(function(){
		alignto = $('#'+$(this).attr('alignheight'));
		if(alignto.length){
			target = {
				vpadding:parseInt(alignto.css('padding-top')) + parseInt(alignto.css('padding-bottom')),
				offset:alignto.offset().top,
				cssheight:alignto.height(),
				height:function(){
					return this.cssheight + this.vpadding;
				}
			};
			align = $(this);
			thisItem = {
				vpadding:parseInt(align.css('padding-top')) + parseInt(align.css('padding-bottom')),
				offset:align.offset().top,
				cssheight:align.height()
			};
			offsetDiff = target.offset - align.offset().top;
			align.height(target.height() + offsetDiff - thisItem.vpadding);
		}
	});
});

var arrslideshow = [];
		
function start_slideshow(start_frame, end_frame, delay, divname, fadespeed) {
		if(fadespeed==null){
			fadespeed=800;
		}else if(fadespeed==0){
			fadespeed=1;
		}
		var nextindex = arrslideshow.length;
		var visibleframe = 1;
		arrslideshow[nextindex] = {'divname':divname,'visibleframe':visibleframe,'slideshowon':true,'delay':delay,'start_frame':start_frame,'end_frame':end_frame,'fadespeed':fadespeed};
		setTimeout(switch_slides(start_frame,start_frame,end_frame, delay, divname, fadespeed), delay);
}

function makevisible(divname,frame,fadespeed){
	for(i=0;i<arrslideshow.length;i++){
		if(arrslideshow[i].divname == divname){
			arrslideshow[i].visibleframe = frame;
		}
	}
	setTimeout("$('#" + divname + frame + "').fadeIn("+fadespeed+")",fadespeed+20);
}
											
function switch_slides(frame, start_frame, end_frame, delay, divname, fadespeed) {
		var slideshowon;
		return (function() {
			for(i=0;i<arrslideshow.length;i++){
				if(arrslideshow[i].divname == divname){
					slideshowon = arrslideshow[i].slideshowon;
				}
			}
			if(slideshowon){
				$('#'+divname + frame).fadeOut(fadespeed);
				if(document.getElementById(divname+"_page"+frame))
					setTimeout("document.getElementById('"+divname+"_page'+"+frame+").className = \"slideshow_page\"",1000);
				if (frame == end_frame) { frame = start_frame; } else { frame = frame + 1; }
				makevisible(divname,frame, fadespeed);
				setTimeout(switch_slides(frame, start_frame, end_frame, delay, divname, fadespeed), delay + fadespeed + 50);
				if(document.getElementById(divname+"_page"+frame))
					setTimeout("document.getElementById('"+divname+"_page'+"+frame+").className = \"slideshow_page_selected\"",1000);
			}
		})
}

function stopslideshow(frame, divname){
	var start_frame;
	var end_frame;
	var delay;
	var slideindex;
	for(i=0;i<arrslideshow.length;i++){
		if(arrslideshow[i].divname == divname){
			visibleframe = arrslideshow[i].visibleframe;
			start_frame = arrslideshow[i].start_frame;
			end_frame = arrslideshow[i].end_frame;
			delay = arrslideshow[i].delay;
			fadespeed = arrslideshow[i].fadespeed;
			arrslideshow[i].slideshowon = false;
			slideindex = i;
		}
	}
	if(frame!=visibleframe){
		$('#'+divname + visibleframe).fadeOut(fadespeed);
		if(document.getElementById(divname+"_page"+visibleframe))
			setTimeout("document.getElementById('"+divname+"_page'+"+visibleframe+").className = \"slideshow_page\"",fadespeed*1.25);
		if(document.getElementById(divname+"_page"+frame))
			setTimeout("document.getElementById('"+divname+"_page'+"+frame+").className = \"slideshow_page_selected\"",fadespeed*1.25);
		setTimeout("arrslideshow["+slideindex+"].slideshowon = true;",delay*2);	
		setTimeout(switch_slides(frame,start_frame,end_frame, delay, divname, fadespeed),delay*2);
		makevisible(divname,frame, fadespeed);	
	}
}

/*Homepage rotator*/
$(function(){
	$("#indexrotator").each(function(){
		var opt = {};
		opt.timeout = 8000;
		opt.speed = 1000;
		opt.fx = 'fade';
		opt.pager = "#indexrotator_pager";
		opt.pause = 1;
		opt.pauseOnPagerHover = 1;
		$(this).cycle(opt);
	});
});