var channelName = 'UK_Babes_1';
daysOfTheWeek = new Array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');

$(document).ready(function(){

	/*$.ajax({
		type: "GET",
		url: "/script/channelChanger.php",
		success: function(html){
			$('.channelChanger .middle').jScrollPane();
			$('.channelChanger .scroll').html(html);
			checkChannelSchedule();
			channelHighlight();
			timer = setInterval('checkChannelSchedule();', 10000);
		}
	});*/

});

function getFlashMovieObject(movieName){
	
	if(window.document[movieName]){
		return window.document[movieName];
	}
	
	if(navigator.appName.indexOf("Microsoft Internet")==-1){
		if(document.embeds && document.embeds[movieName]){
			return document.embeds[movieName]; 
		}
	} else { // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
		return document.getElementById(movieName);
	}
}

function SendCommandToFlashMovie(swf, data){
	var flashMovie = getFlashMovieObject(swf);
	flashMovie.SetVariable("/:javascript_command", data);
}

function SendVariableToFlashMovie(swf, data){
	var flashMovie = getFlashMovieObject(swf);
    flashMovie.SetVariable("/:javascript_var", data);
}

function channelChange(channelName){
	window['channelName'] = channelName.replace(/-/g, "_");
	channelHighlight();
	SendCommandToFlashMovie('player', 'set_channel::'+channelName);
}

function setVariable(variable, data){
	window[variable] = data;
}

function checkChannelSchedule(){
			
	day = daysOfTheWeek[new Date().getDay()];
	decimalTime = new Date().getHours() + (new Date().getMinutes() / 60) ;
	/*
	//if(typeof(decimalTime) == 'undefined')decimalTime = 3;
	if(typeof(decimalTime) == 'undefined')decimalTime = new Date().getHours() + (new Date().getMinutes() / 60) ;
	decimalTime++
	decimalTime++
	if(decimalTime >= 24)decimalTime = 0;
	*/
	logging = decimalTime+'<br>';
	count = 0;
	for(var item in channels){
				
		logging += item + ' ';
		
		isLive = false;
		if(channels[item].showtimes[day].length != 0){
			
			for(i=0; i<channels[item].showtimes[day].length; i++){
			
				startTime = channels[item].showtimes[day][i].start;
				finishTime = channels[item].showtimes[day][i].finish
				
				logging += '[' + startTime + ' : ' + finishTime + '] ';

				if(decimalTime >= startTime && decimalTime < finishTime){
					isLive = true;
				} else if(startTime > finishTime){
					if(decimalTime >= startTime){
						isLive = true;
					}
				}
			}
			
			dayNumber = new Date().getDay() - 1;
			if(dayNumber == -1)dayNumber = 6;
			dayName = daysOfTheWeek[ dayNumber ];
			dayBeforeLastShow = channels[item].showtimes[ dayName ][ channels[item].showtimes[ dayName ].length - 1];
			
			if(finishTime < startTime){
				if(decimalTime < finishTime){
					isLive = true;
				}
			}
			
		}
				
		logging += isLive + '<br>';

		if(isLive == true){
			count++
		}
		
		
	}
	
	resetWidth(count);
	
	for(var item in channels){
				
		isLive = false;
		if(channels[item].showtimes[day].length != 0){
			
			for(i=0; i<channels[item].showtimes[day].length; i++){
			
				startTime = channels[item].showtimes[day][i].start;
				finishTime = channels[item].showtimes[day][i].finish

				if(decimalTime >= startTime && decimalTime < finishTime){
					isLive = true;
				} else if(startTime > finishTime){
					if(decimalTime >= startTime){
						isLive = true;
					}
				}
				
				dayNumber = new Date().getDay() - 1;
				if(dayNumber == -1)dayNumber = 6;
				dayName = daysOfTheWeek[ dayNumber ];
				dayBeforeLastShow = channels[item].showtimes[ dayName ][ channels[item].showtimes[ dayName ].length - 1];
			
				if(finishTime < startTime){
					if(decimalTime < finishTime){
						isLive = true;
					}
				}
			}
			
		}
		
		if($('.channel.'+item+' img').is(":visible") == true && isLive == false){
			
			
			$('.channel.'+item+' img').fadeOut(500, function() {
			$(this).parent().animate({
				width: 0
			}, 500, function() {
				$(this).hide();
				$(this).css('width', 60);
			});
			});
			
			
		} else if($('.channel.'+item+' img').is(":visible") == false && isLive == true){
			
			$('.channel.'+item).css('width', 0);
			$('.channel.'+item).show();
			$('.channel.'+item+' img').hide();
			
			$('.channel.'+item).animate({
				width: 60
			}, 500, function() {
				$(this).children('img').fadeIn(500);
			});
			
		}
		
		if(item == channelName){
			$('.channel.'+item).css('opacity', 1);
		} else {
			$('.channel.'+item).css('opacity', 0.5);
		}
		
	}
	
	/*
	if($('#log-window').length == 0){
		$('body').append('<div id="log-window" style="position: absolute;top: 5px;left: 5px;display: block;width: 260px;height: 95%;overflow: hidden;padding: 5px;background: #2d2d2d;border: 1px solid #999999;color: #999999;font-family: Arial;font-size: 10px;"></div>');
	}
	$('#log-window').prepend(logging+'<hr>');
	*/

}

function channelHighlight(){
	
	for(var item in channels){
				
		if(item == channelName){
			$('.channel.'+item).css('opacity', 1);
		} else {
			$('.channel.'+item).css('opacity', 0.5);
		}
		
		$('.channel.'+item).hover(
			function(){
				$(this).css('opacity', 1);
			}, function(){
				if($(this).attr('class').indexOf(channelName) == -1){
					$(this).css('opacity', 0.5);
				}
			}
		);
		
	}
	
}

function resetWidth(count){
	
	width = 65 * count;
	
	// 585 multiple of 65px. img width + margin
	if(width < 585){
		$('.channelChanger .middle').css({width: width});
		$('.jspHorizontalBar').hide();
		$('.jspPane').css('left', 0);
	} else {
		$('.channelChanger .middle').css({width: 585});
		$('.jspHorizontalBar').show();
	}
	
}


function launch_pin_reg(){
	
	$('#livestream').attr("src", $('#livestream').attr("src") + "&vc=1");
	
	if($('.flashplayer').offset().top < $("body").scrollTop()){
		$('body').animate({scrollTop : $('.flashplayer').offset().top}, 'slow');
	}
	
}

function launch_chat(viewer, replacement_session_id){
	//$('.chat').slideDown('slow');
	$('.chat').show();
	$('#chat-frame').attr("src", "http://www.fone-me.com/api/iptv/chat/cellcast24/?viewer=" + viewer + "&replacement_session_id=" + replacement_session_id);
}

function iframe_height(normal, with_chat){
	if($('#livestream').attr("height") == normal){
		$('#livestream').attr("height", normal + with_chat);
	} else {
		$('#livestream').attr("height", normal);
	}
}

function close_chat(){
	/*$('.chat').slideUp('slow', function() {
		$('#chat-frame').attr("src", "about:blank");
	});*/
	$('.chat').hide();
	$('#chat-frame').attr("src", "about:blank");
	
}


function CreateBookmarkLink() { 
	
	title = "Rampant.tv"; 
	url = "http://www.rampant.tv/";
	
	if(window.sidebar){
		window.sidebar.addPanel(title, url, "");
	} else if(document.all){
		window.external.AddFavorite(url, title);
	} else if(window.opera && window.print){
		alert('Browser Not Supported\nPress Ctrl+D to bookmark (Command+D for macs) after you click OK');
	} else if(window.chrome){
		alert('Browser Not Supported\nPress Ctrl+D to bookmark (Command+D for macs) after you click OK');
	}
	
}

 

function strip_url(){
	
	form_element = document.right_email.message.value;
	site_url = "http://www.itelebill.com";
	
	
	url_open = form_element.indexOf("http");
	url_close = form_element.indexOf("	", form_element.indexOf(site_url));
	
	before = form_element.slice(0, url_open);
	
	after = form_element.slice(url_close, form_element.length);
	my_string = before + "{URL}" + after;
	
	return my_string.replace(/ /g, "%20");
}
	
/**************************************************
	SEND FORM
**************************************************/

var x;
var send_url_required = new Array("name", "email");

var error_message_text = new Array();

//var right_contact_required = new Array("name", "email", "enquiry");

function check_form(url, form_name){
	
	//alert(document[form_name].contact_type.length);
	//alert(document[form_name].elements);
	
	document.getElementById(form_name + "_loader").innerHTML = "<img src=\"/images/loader.gif\">";
	
	//document[form_name].send.disabled = "true"
	//document[form_name].send.setAttribute("class", "button button_disabled");
	
	error_message = "";
	url_string = "";
	
	 for(i=0; i < document[form_name].elements.length; i++){
		
		
		///*
		if(document[form_name].elements[i].name.toLowerCase() != "submit" && document[form_name].elements[i].name.toLowerCase() != "send" && document[form_name].elements[i].name.toLowerCase() != "reset"){
			
			//alert(document[form_name].elements[i].name.toLowerCase());
			
			if(document[form_name].elements[i].name.substr(0,3) != "cb_" && document[form_name].elements[i].name.substr(0,2) != "r_"){
				
				if(form_name == "right_email" && document[form_name].elements[i].name == "message"){
					url_string += "message=" + strip_url() + "&";
				} else {
					url_string += document[form_name].elements[i].name + "=" + document[form_name].elements[i].value.replace(/ /g, "%20") + "&";
				}
				
				///*
				for (x in this[form_name + "_required"]){
					
					///*
					if (document[form_name].elements[i].name.toLowerCase() == this[form_name + "_required"][x]){
						
						//alert(document[form_name].elements[i].name.toLowerCase() + " || " + this[form_name + "_required"][x] + " |=| " + document[form_name].elements[i].value);

												
						if(document[form_name].elements[i].value == "" || document[form_name].elements[i].value.toLowerCase() == ("select " + document[form_name].elements[i].name)){
							
							switch(document[form_name].elements[i].name){
								
								case "to":
									error_message += "<p>Please enter your Friends Email Address</p>\n";
									break;
									
								case "name":
									error_message += "<p>Please enter your Name</p>\n";
									break;
									
								case "issue":
									error_message += "<p>Please enter your Issue or Question</p>\n";
									break;
									
								case "mobile_number":
									error_message += "<p>Please enter your Mobile Phone Number to help us track our records</p>\n";
									break;
									
								case "detail":
									error_message += "<p>Please enter the nature of your problem</p>\n";
									break;
									
								case "phone":
									error_message += "<p>Please enter your Phone Number</p>\n";
									break;
									
								case "email":
									if(form_name == "right_email"){
										error_message += "<p>Please enter the Email Address you wish to send to</p>\n";
									} else {
										error_message += "<p>Please enter your Email Address</p>\n";
									}
									break;
									
								case "enquiry":
									error_message += "<p>Please enter the nature of your Enquiry</p>\n";
									break;

								default:
									error_message += "<p>Please enter your " + document[form_name].elements[i].name + "</p>\n";
								
							}
							
						}
						
					}
					//*/
				}
			//*/
			} else if(document[form_name].elements[i].name.substr(0,3) == "cb_"){
				
				url_string += document[form_name].elements[i].name + "=" + document[form_name].elements[i].checked + "&";
				
			} else if(document[form_name].elements[i].name.substr(0,2) == "r_"){
				
				for(r = 0; r < document[form_name][document[form_name].elements[i].name].length; r++){
					if(document[form_name][document[form_name].elements[i].name][r].checked == true){
						val = document[form_name][document[form_name].elements[i].name][r].value;
					}
				}
				
				url_string += document[form_name].elements[i].name.substr(2, document[form_name].elements[i].name.length) + "=" + val + "&";
				//alert(document[form_name].elements[i].name.substr(2, document[form_name].elements[i].name.length) + ": " + val);
				
			}
			
		}
		//*/
	 }
	
	if(error_message != ""){
		document.getElementById(form_name + "_info").innerHTML = "<div class=\"error_notice\"><h5>Oops! Please Correct The Following Errors</h5>" + error_message + "</div>";
		document.getElementById(form_name + "_loader").innerHTML = "";
	
		//document[form_name].send.disabled = "false"
		//document[form_name].send.setAttribute("class", "button");
	
	} else {
		
		send_form(url, url_string, form_name);
		//alert(url_string);
		
	}
	
	return false;
	
}

function send_form(url, url_string, div){

	var params = url_string;
	//document.getElementById("testing").innerHTML = url_string;
	
	if (document.getElementById){
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	}

	if (x){
		x.onreadystatechange = function(){
			if (x.readyState == 4 && x.status == 200) {
				
				document.getElementById(div + "_loader").innerHTML = "";
	
				//document[div].send.disabled = "false"
				//document[div].send.setAttribute("class", "button");
				
				document.getElementById(div + "_info").innerHTML = "<div class=\"success_notice\">" + x.responseText + "</div>";
				
			}
		}
		
		x.open("POST", url, true);
		
		x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		x.setRequestHeader("Content-length", params.length);
		x.setRequestHeader("Connection", "close");
	
		x.send(params);
		
	}
}

function capitilise_words(input_string){

	var tmpStr, tmpChar, preString, postString, strlen;
	tmpStr = input_string.toLowerCase();
	stringLen = tmpStr.length;

	if (stringLen > 0){
		
		for (i = 0; i < stringLen; i++){
			if (i == 0){
				tmpChar = tmpStr.substring(0, 1).toUpperCase();
				postString = tmpStr.substring(1, stringLen);
				tmpStr = tmpChar + postString;
			} else {
				tmpChar = tmpStr.substring(i, i+1);
				if (tmpChar == " " && i < (stringLen-1)){
					tmpChar = tmpStr.substring(i+1, i+2).toUpperCase();
					preString = tmpStr.substring(0, i+1);
					postString = tmpStr.substring(i+2, stringLen);
					tmpStr = preString + tmpChar + postString;
				}
			}
		}
	}

	return tmpStr;
	
}

/****************************************************************************************
Ticker info display
****************************************************************************************/
$(document).ready(function() {
	
	$(".ticker_block").click(function(event){
		//$('.minutes_info').css({"left": $('#container').offset().left - 20});
		$('.minutes_info').css({margin: '0 205px' , /*display: 'block',*/ top: '100px'});
		$('.minutes_info').fadeIn(200);
		
		
	});
	
	$(".popup_close").click(function(event){
		$('.minutes_info').fadeOut(200);
	});
	
	/*$(".minutes_ticker").click(function(event){
		event.preventDefault();
	});*/

});


