jQuery(document).ready(function(){

	if(jQuery('body').hasClass('onadmin'))
	{}
	else
	{
	jQuery('#videobox-inner').click(function(){showVideo();
	});
}

jQuery('table.colored tbody').each(function(){var rows=jQuery(this).children('tr');var e=0;for(var i=0;i<rows.length;i++){if(e==1){jQuery(rows[i]).addClass('color');e=0;}
else{e=1;}}});if(jQuery.browser.msie){jQuery('#videobox-inner').hide();}
else{if(jQuery('body').hasClass('onadmin')){}
else{jQuery('#videobox object').hide();}}});function changeTab(object,tab){var parent1=jQuery(tab).parents().get(0);var parent2=jQuery(tab).parents().get(1);jQuery(parent2).children('li').removeClass('active');jQuery(parent1).addClass('active');jQuery('#topbox-blue-inner>div').hide();jQuery('#'+object).show();}


$(document).ready(function() {
    $('#login-link').click(function(event){
        event.preventDefault();
        $('#login-box').css('display','');
    });
    $('.login-box-top').click(function(event) {
        event.preventDefault();
        $('#regform-content').hide();
        $('#reset-content').hide();
        $('#login-content').show();
        $('#login-box').css('display','none');
    });
    
    $('.custom-select-marked').click(function(event) {
        var parent = $(this).parent('.custom-select');
        parent.children('.custom-select-menu').show();
        $("body").click(hideCustomMenus);
        return false;
    });
    $('.custom-select-menu a').click(function(event) {
        event.preventDefault();
        var parent = $(this).parents('.custom-select');
        parent.children('.custom-select-marked').html($(this).html());
    });


    $('.custom-select-kanal-marked').click(function(event) {
        var parent = $(this).parent('.custom-select-kanal');
        parent.children('.custom-select-kanal-menu').show();
        $("body").click(hideCustomMenus);
        return false;
    });
    $('.custom-select-kanal-menu a').click(function(event) {
        event.preventDefault();
        var parent = $(this).parents('.custom-select-kanal');
        parent.children('.custom-select-kanal-marked').html($(this).html());
    });


    $('.channel .show a').click(function(event) {
        event.stopPropagation();
    });
    
    $('.channel-dropdown-box').click(function() {
        
        var drop = $(this).children('.channel-dropdown');
        if (drop.css('display') == 'none') {
            drop.css('display','block');
            $('body').bind('click', hideDropDown);
             return false;
        }
        else {
            drop.css('display','none');
        }
    });
	
	$('.vert_ch_col .show table').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		opacity: 1,/*
		bodyHandler: function() {
			alert($(this).html());
		},*/
		bodyHandler: function() { 
			var vajalikhtml = $($(this).parents('.show').children('.floating-window')).html();
			if (vajalikhtml)
			{
				return vajalikhtml;
			}
			else {
				return "";
			}
		}
	});

	$('.vert_ch_col .show table').click(function(){
		var vajalikdiv = $(this).parents('.show').children('.show-info');
		if ($(vajalikdiv).css('display') == 'none')
		{
			$(vajalikdiv).css('display', 'block');
			$(this).parents('.show').addClass('show-active');
		}
		else {
			$(vajalikdiv).css('display', 'none');
			$(this).parents('.show').removeClass('show-active');
		}
	});
	
    setCurrentTime();
});


function showOverlay(){
var ie6=jQuery.browser.msie&&/MSIE 6.0/.test(navigator.userAgent);
var lyr1="";

if(jQuery.browser.msie)
	{
	lyr1=jQuery('<iframe class="overlay" style="filter: alpha(opacity=0);z-index:900;border:none;margin:0;padding:0;position:absolute;width:100%;height:100%;top:0;left:0" src="javascript:false;"></iframe>');
	}
else
	{
	lyr1=jQuery('<div class="overlay" style="display:none"></div>');
	}

var lyr2=jQuery('<div class="overlay" style="position: fixed; background: black; opacity: 0.6;-moz-opacity: 0.6;filter: alpha(opacity=60);z-index:900;border:none;margin:0;padding:0;width:100%;height:100%;top:0;left:0"></div>');jQuery(lyr1).appendTo('body');jQuery(lyr2).appendTo('body');var expr=jQuery.browser.msie&&(!jQuery.boxModel||jQuery('object,embed').length>0);if(ie6||expr){if(jQuery.boxModel)jQuery('html,body').css('height','100%');jQuery.each([lyr1,lyr2],function(i,o){var s=o[0].style;s.position='absolute';s.setExpression('height','document.body.scrollHeight > document.body.offsetHeight ? document.body.scrollHeight : document.body.offsetHeight + "px"');s.setExpression('width','jQuery.boxModel && document.documentElement.clientWidth || document.body.clientWidth + "px"');});
}

$(document,'html','body').scrollTop(0);
}

function showReminder(id, title, kuup){ 
	var myDate = new Date(kuup * 1000);
	document.getElementById("saateaeg").innerHTML=myDate.getDate()+'.'+(myDate.getMonth()+1)+'.'+myDate.getFullYear()+', kell '+myDate.getHours()+':'+myDate.getMinutes();

	target_nimi = document.getElementById("saatenimi");
	target_id = document.getElementById("saateid");
	target_kuup = document.getElementById("kuup2ev");

	jQuery('#popup').css({'top':'40px','position':'absolute'}); 

	target_nimi.innerHTML=title;
	target_id.value=id;
	target_kuup.value=kuup;

	jQuery('#popup').show();
	}

function submitReminder() {
	target_id = document.getElementById("saateid").value;
	target_email = document.getElementById("email").value;
	target_time = document.getElementById("time").value;
	kuup2ev = document.getElementById("kuup2ev").value;

	if ((target_email==null)||(target_email=="")){
		alert("Palun sisesta oma e-mail")
		document.getElementById("email").focus()
		return false
	}
	if (echeck(target_email)==false){
		target_email=""
		document.getElementById("email").focus()
		return false
	}

	var req='meeldetuletus=1&saade='+target_id+'&email='+target_email+'&time='+target_time+'&kuup2ev='+kuup2ev;
	ajaxpack.postAjaxRequest('index.php', req, processGetPost, 'eval');
	closeTopbox();
	//alert(target_id+', '+target_email+', '+target_time);
}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Vigane e-maili aadress")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Vigane e-maili aadress")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Vigane e-maili aadress")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Vigane e-maili aadress")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Vigane e-maili aadress")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Vigane e-maili aadress")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Vigane e-maili aadress")
		    return false
		 }

 		 return true					
	}

function showArchive(){ 
	jQuery('#arhiiv').css({'top':'40px','position':'absolute'}); 
	jQuery('#arhiiv').show();
	}

function submitArchive() {
	day = document.getElementById("day").value;
	month = document.getElementById("month").value;
	year = document.getElementById("year").value;
	kanal = document.getElementById("kanal").value;
	var k = year+'-'+month+'-'+day;

	//alert(k+', '+kanal);
	location.href = "kanal.php?kid="+kanal+"&dt="+k+"&ti=08:00";
}


function closeTopbox(){
	jQuery(document).unbind('scroll');
	jQuery('#popup').hide();
	jQuery('#arhiiv').hide();
	jQuery('.overlay').remove();
	}


function submitFavorite(id) {

	var req='lemmik=1&saade='+id;
	ajaxpack.postAjaxRequest('index.php', req, processGetPost, 'eval');
}


function submitRecording(id) {

	var req='salvestus=1&saade='+id;
	ajaxpack.postAjaxRequest('index.php', req, processGetPost, 'eval');
}

function decision(message, url){
if(confirm(message)) location.href = url;
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

function resetVideo(target, divId, action) 
{
  target=document.getElementById(target); 

  if (typeof(divId)=="string")
	{
	divId=document.getElementById(divId); 
	}

  if (action=="play") 
	{
	org=divId.innerHTML;
	target.innerHTML=org;
	}

  if (action=="stop") 
	{
	target.innerHTML='';
	}

  return false;
}

function hideDropDown() {
     $('.channel-dropdown').hide();
     $('body').unbind('click', hideDropDown);
}
function toggleRow(id) {
    if ($('#' + id).css('display') == 'none') {
        $('#' + id).css('display','');
    }
    else {
        $('#' + id).css('display','none');
    }
}

function hideCustomMenus() {
     $('.custom-select-menu').hide();
     $('.custom-select-kanal-menu').hide();
     $('body').unbind('click', hideCustomMenus);
}

function toggleInformation(id,element) {

    var parent = $(element).parents('.channel');
    
    if($('#'+id).css('display') == 'none') {
        parent.children('.channel-info').children('.channel-current-time').children('.show').removeClass('active-show');
        $(element).addClass('active-show');
        parent.children('.show-info').not('#'+id).hide();
        $('#'+id).show();
        setCurrentTime();
    }
    else {
        parent.children('.channel-info').children('.channel-current-time').children('.show').removeClass('active-show');
        parent.children('.show-info').hide();
        setCurrentTime();
    }

}

function hideInformation(element) {
    var parent = $(element).parent().parent().parent();;
    parent.children('.channel-info').children('.channel-current-time').children('.show').removeClass('active-show');
    parent.children('.show-info').hide();
    setCurrentTime();
}

function markType(element) {
    var parent = element.parentNode;
    var id=element.id;
    if(element.checked) {
		setCookie(id,'on',-1);
		setCookie(id,'on',7);
        $('.'+id).addClass(parent.className);
    }
    else {
		setCookie(id,'off',-1);
		setCookie(id,'off',7);
        $('.'+id).removeClass(parent.className);
    }
}

function setCurrentTime() {    
    $('#current-time').css('height',($('#listing').height()-15)+'px');
}

function hidecommform() {
}

function getObj(name) {
	if (document.getElementById) { return document.getElementById(name); }
	else if (document.all) { return document.all[name]; }
	else if (document.layers) { return document.layers[name]; } 
}

function initAd(title,txt,width,top) {
	if(typeof(top)=='undefined') {
		top=50;
	}
	if (parseInt(navigator.appVersion) >= 4 && navigator.javaEnabled()) {
		document.write('<div id="popbg" style="position: absolute; left: 0; top: 0; width:100%; height: 100%; z-index: 98; background: #888; opacity: 0.5; moz-opacity: 0.5; filter: alpha(opacity=50); "></div>');
		document.write('<div id="pop" style="position: absolute; left: 0; top: 200px; z-index: 1299;">');
		document.write('<table cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" border="0" width="'+width+'" height="'+top+'">');
		document.write('<tr><td align="right" style="padding: 5px;" valign="top" height="15"><a href="javascript:hideAd(\'pop\');hideAd(\'popbg\');">Sulge aken</a></td></tr>');
		document.write('<tr><td align="left" style="padding: 10px;" valign="top" height="20"><b>'+title+'</b></td></tr>');
		document.write('<tr><td align="left" style="padding: 10px;" valign="top">'+txt+'</td></tr>');
		document.write('</table>');
		document.write('</div>');
		positionAd(width);
	}
}

function positionAd(width) {
	if (document.layers) {
		document.layers.popbg.width = window.innerWidth; document.layers.popbg.height = window.innerHeight;
		document.layers.pop.left = ((window.innerWidth / 2) - (width / 2));
	}
	else if (document.all) {
		document.all.popbg.style.width = document.body.offsetWidth;
		document.all.popbg.style.height = document.body.offsetHeight;
		document.all.pop.style.left = ((document.body.offsetWidth / 2) - (width / 2));
	}
	else if (document.getElementById) {
		document.getElementById("popbg").style.width = window.innerWidth;
		document.getElementById("popbg").style.height = window.innerHeight;
		document.getElementById("pop").style.left = ((window.innerWidth / 2) - (width / 2))+'px';
	}
	setTimeout("positionAd('"+width+"');",1000);
}

function showAd(divId) 
{
	if (document.layers) document.layers[divId].visibility = 'show';
	else if (document.all) document.all[divId].style.visibility = 'visible';
	else if (document.getElementById) document.getElementById(divId).style.visibility = 'visible';
	setCookie('ad','1',7);
}

function hideAd(divId) 
{
	if (document.layers) document.layers[divId].visibility = 'hide';
	else if (document.all) document.all[divId].style.visibility = 'hidden';
	else if (document.getElementById) document.getElementById(divId).style.visibility = 'hidden';
	setCookie('ad','1',7);
}

function adDown(divId) 
{
	var state=typeof topPos;
	if(state=='undefined') {
		topPos=-260;
	}
	if(topPos < 75) {
		topPos+=3;
		if (document.layers) document.layers[divId].top = topPos;
		else if (document.all) document.all[divId].style.top = topPos;
		else if (document.getElementById) document.getElementById(divId).style.top = topPos;	
		setTimeout("adDown('pop');",25);
	}
}

// -------------------------------------------------------------------
// Image Thumbnail Viewer Script- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Last updated: July 7th, 2008- Fixed enlarged image not showing in IE sometimes
// -------------------------------------------------------------------

var thumbnailviewer={
enableTitle: true, //Should "title" attribute of link be used as description?
enableAnimation: true, //Enable fading animation?
definefooter: '<div class="footerbar">Sulge aken</div>', //Define HTML for footer interface
defineLoading: '<img src="pix/loading.gif" /> Laen pilti ...', //Define HTML for "loading" div

/////////////No need to edit beyond here/////////////////////////

scrollbarwidth: 16,
opacitystring: 'filter:progid:DXImageTransform.Microsoft.alpha(opacity=10); -moz-opacity: 0.1; opacity: 0.1',
targetlinks:[], //Array to hold links with rel="thumbnail"

createthumbBox:function(){
//write out HTML for Image Thumbnail Viewer plus loading div
document.write('<div id="thumbBox" onClick="thumbnailviewer.closeit()"><div id="thumbImage"></div>'+this.definefooter+'</div>')
document.write('<div id="thumbLoading">'+this.defineLoading+'</div>')
this.thumbBox=document.getElementById("thumbBox")
this.thumbImage=document.getElementById("thumbImage") //Reference div that holds the shown image
this.thumbLoading=document.getElementById("thumbLoading") //Reference "loading" div that will be shown while image is fetched
this.standardbody=(document.compatMode=="CSS1Compat")? document.documentElement : document.body //create reference to common "body" across doctypes
},


centerDiv:function(divobj){ //Centers a div element on the page
var ie=document.all && !window.opera
var dom=document.getElementById
var scroll_top=(ie)? this.standardbody.scrollTop : window.pageYOffset
var scroll_left=(ie)? this.standardbody.scrollLeft : window.pageXOffset
var docwidth=(ie)? this.standardbody.clientWidth : window.innerWidth-this.scrollbarwidth
var docheight=(ie)? this.standardbody.clientHeight: window.innerHeight
var docheightcomplete=(this.standardbody.offsetHeight>this.standardbody.scrollHeight)? this.standardbody.offsetHeight : this.standardbody.scrollHeight //Full scroll height of document
var objwidth=divobj.offsetWidth //width of div element
var objheight=divobj.offsetHeight //height of div element
var topposition=(docheight>objheight)? scroll_top+docheight/2-objheight/2+"px" : scroll_top+10+"px" //Vertical position of div element: Either centered, or if element height larger than viewpoint height, 10px from top of viewpoint
divobj.style.left=docwidth/2-objwidth/2+"px" //Center div element horizontally
divobj.style.top=Math.floor(parseInt(topposition))+"px"
divobj.style.visibility="visible"
},

showthumbBox:function(){ //Show ThumbBox div
thumbnailviewer.thumbLoading.style.visibility="hidden" //Hide "loading" div
this.centerDiv(this.thumbBox)
if (this.enableAnimation){ //If fading animation enabled
this.currentopacity=0.1 //Starting opacity value
this.opacitytimer=setInterval("thumbnailviewer.opacityanimation()", 20)
}
},


loadimage:function(link){ //Load image function that gets attached to each link on the page with rel="thumbnail"
if (this.thumbBox.style.visibility=="visible") //if thumbox is visible on the page already
this.closeit() //Hide it first (not doing so causes triggers some positioning bug in Firefox
var imageHTML='<img src="'+link.getAttribute("href")+'" style="'+this.opacitystring+'" />' //Construct HTML for shown image
if (this.enableTitle && link.getAttribute("title")) //Use title attr of the link as description?
imageHTML+='<br />'+link.getAttribute("title")
this.centerDiv(this.thumbLoading) //Center and display "loading" div while we set up the image to be shown
this.thumbImage.innerHTML=imageHTML //Populate thumbImage div with shown image's HTML (while still hidden)
this.featureImage=this.thumbImage.getElementsByTagName("img")[0] //Reference shown image itself
if (this.featureImage.complete)
thumbnailviewer.showthumbBox()
else{
this.featureImage.onload=function(){ //When target image has completely loaded
thumbnailviewer.showthumbBox() //Display "thumbbox" div to the world!
}
}
if (document.all && !window.createPopup) //Target IE5.0 browsers only. Address IE image cache not firing onload bug: panoramio.com/blog/onload-event/
this.featureImage.src=link.getAttribute("href")
this.featureImage.onerror=function(){ //If an error has occurred while loading the image to show
thumbnailviewer.thumbLoading.style.visibility="hidden" //Hide "loading" div, game over
}
},

setimgopacity:function(value){ //Sets the opacity of "thumbimage" div per the passed in value setting (0 to 1 and in between)
var targetobject=this.featureImage
if (targetobject.filters && targetobject.filters[0]){ //IE syntax
if (typeof targetobject.filters[0].opacity=="number") //IE6
targetobject.filters[0].opacity=value*100
else //IE 5.5
targetobject.style.filter="alpha(opacity="+value*100+")"
}
else if (typeof targetobject.style.MozOpacity!="undefined") //Old Mozilla syntax
targetobject.style.MozOpacity=value
else if (typeof targetobject.style.opacity!="undefined") //Standard opacity syntax
targetobject.style.opacity=value
else //Non of the above, stop opacity animation
this.stopanimation()
},

opacityanimation:function(){ //Gradually increase opacity function
this.setimgopacity(this.currentopacity)
this.currentopacity+=0.1
if (this.currentopacity>1)
this.stopanimation()
},

stopanimation:function(){
if (typeof this.opacitytimer!="undefined")
clearInterval(this.opacitytimer)
},


closeit:function(){ //Close "thumbbox" div function
this.stopanimation()
this.thumbBox.style.visibility="hidden"
this.thumbImage.innerHTML=""
this.thumbBox.style.left="-2000px"
this.thumbBox.style.top="-2000px"
},

cleanup:function(){ //Clean up routine on page unload
this.thumbLoading=null
if (this.featureImage) this.featureImage.onload=null
this.featureImage=null
this.thumbImage=null
for (var i=0; i<this.targetlinks.length; i++)
this.targetlinks[i].onclick=null
this.thumbBox=null
},

dotask:function(target, functionref, tasktype){ //assign a function to execute to an event handler (ie: onunload)
var tasktype=(window.addEventListener)? tasktype : "on"+tasktype
if (target.addEventListener)
target.addEventListener(tasktype, functionref, false)
else if (target.attachEvent)
target.attachEvent(tasktype, functionref)
},

init:function(){ //Initialize thumbnail viewer script by scanning page and attaching appropriate function to links with rel="thumbnail"
if (!this.enableAnimation)
this.opacitystring=""
var pagelinks=document.getElementsByTagName("a")
for (var i=0; i<pagelinks.length; i++){ //BEGIN FOR LOOP
if (pagelinks[i].getAttribute("rel") && pagelinks[i].getAttribute("rel")=="thumbnail"){ //Begin if statement
pagelinks[i].onclick=function(){
thumbnailviewer.stopanimation() //Stop any currently running fade animation on "thumbbox" div before proceeding
thumbnailviewer.loadimage(this) //Load image
return false
}
this.targetlinks[this.targetlinks.length]=pagelinks[i] //store reference to target link
} //end if statement
} //END FOR LOOP
//Reposition "thumbbox" div when page is resized
this.dotask(window, function(){if (thumbnailviewer.thumbBox.style.visibility=="visible") thumbnailviewer.centerDiv(thumbnailviewer.thumbBox)}, "resize")


} //END init() function

}

thumbnailviewer.createthumbBox() //Output HTML for the image thumbnail viewer
thumbnailviewer.dotask(window, function(){thumbnailviewer.init()}, "load") //Initialize script on page load
thumbnailviewer.dotask(window, function(){thumbnailviewer.cleanup()}, "unload")