	
	function calcDate( element, millis ) {
		if ( millis.charAt(0)=='|') {
			millis=millis.substring(1);
			var timezone = 0*60*60*1000;
			var d = new Date();
			d.setTime( parseInt(millis) + parseInt(timezone) );
			element.innerHTML = parseInt( (d.getYear()<1000 ? d.getYear()+1900 : d.getYear())  )+'-'+(d.getMonth()<9 ? '0':'') + parseInt(d.getMonth()+1)+'-'+(d.getDate()<10 ? '0' : '') + d.getDate();
		}
	}
	
	function calcDateTime( element, millis ) {
		if ( millis.charAt(0)=='|') {
			millis=millis.substring(1);
			var timezone = 0*60*60*1000;
			var d = new Date();
			d.setTime( parseInt(millis) + parseInt(timezone) );
			element.innerHTML = parseInt((d.getYear()<1000 ? d.getYear()+1900 : d.getYear()) )+'-'+(d.getMonth()<9 ? '0':'') + parseInt(d.getMonth()+1)+'-'+(d.getDate()<10 ? '0' : '') + d.getDate()+' '+(d.getHours()<10 ? '0' : '')+d.getHours()+':'+(d.getMinutes()<10 ? '0' : '') +d.getMinutes();
		}
	
	}

	function calcTime( element, millis ) {
		if ( millis.charAt(0)=='|') {
			millis=millis.substring(1);
			var timezone = 0*60*60*1000;
			var d = new Date();
			d.setTime( parseInt(millis)+parseInt(timezone));
			element.innerHTML = (d.getHours()<10 ? '0' : '')+d.getHours()+':'+(d.getMinutes()<10 ? '0' : '') +d.getMinutes();
		}
	}
	
	function calcTimeDuration( element, millis ) {
		if ( millis.charAt(0)=='|') {
			millis=millis.substring(1);
			var d = new Date();
			currentMillis = d.getTime();
			d.setTime( parseInt(millis) );
			age = currentMillis - d.getTime();// +( d.getTimezoneOffset()*60000)  ; // offset in minutes
			if ( age < 86400000 ) { //24 hours
			
				hours = Math.floor(age / 3600000); // 1 hour
				minutes = Math.floor( (age-hours*3600000 ) / 60000 ) ; // 1 minute
				durationString = hours+'h'+minutes+'m ago';
			
				element.store = durationString;
				element.innerHTML = durationString;
				element.onmouseout= function() { this.innerHTML=this.store; }

				element.dateString = parseInt((d.getYear()<1000 ? d.getYear()+1900 : d.getYear()) )+'-'+(d.getMonth()<9 ? '0':'') + parseInt(d.getMonth()+1)+'-'+(d.getDate()<10 ? '0' : '') + d.getDate()+' '+(d.getHours()<10 ? '0' : '')+d.getHours()+':'+(d.getMinutes()<10 ? '0' : '') +d.getMinutes();
				element.onmouseover= function() { this.innerHTML=this.dateString; }
			} else {
				element.innerHTML = parseInt( (d.getYear()<1000 ? d.getYear()+1900 : d.getYear())  )+'-'+(d.getMonth()<9 ? '0':'') + parseInt(d.getMonth()+1)+'-'+(d.getDate()<10 ? '0' : '') + d.getDate();
			}
		}
	}
	
	function doTimes() {
		jQuery('.date').each( function() {
			calcDate( this,this.innerHTML );
		});
		jQuery('.datetime').each( function() {
			calcDateTime( this, this.innerHTML );
		});
		jQuery('.time').each( function() {
			calcTime( this, this.innerHTML );
		});
		jQuery('.timeduration').each( function() {
			calcTimeDuration( this,this.innerHTML );
		});
	}

	function setHeight() {
		alert( document.body.clientHeight );
	}
	
	function loadAjaxElement( element, url , param) {
		var myAjax = new Ajax.Updater( element, host+url, { method: 'get' , parameters: param });
	}

	function loadAjaxField( field, url , param) {
		new Ajax.Request(host+url, {  method: 'get',  parameters: param, onSuccess: function(transport) {
    			field.value = transport.responseText;
  			}
		});
	}
	
	function enableButtonsExchange() {
		alert( $('src').value.length );
		if ( $('src').value.length>0 && $('dest').value.length>0 ) {
			$('calc_btn').disabled=false;
		}
	}
	function toggleChat() {
		var chatDiv = $('chat');
		if ( chatDiv.style.zIndex > 0 ) {
			chatDiv.style.zIndex = -1;
			chatDiv.style.top=0;
			$('body').style.paddingTop = 155;
			new Ajax.Request(host+'/chat_toggle.p?chat=false', { method:'get' });
		} else {
			chatDiv.style.zIndex = 5;
			chatDiv.style.top=153;
			$('body').style.paddingTop = 282;
			$('chatText').scrollTop = $('chatText').scrollHeight;
			new Ajax.Request(host+'/chat_toggle.p?chat=true', { method:'get' });
		}
	}

	function initTorrentList() {
		var f = $$('.torrent_row');
		for ( var i=0; i<f.length; i++) {
			//Element.absolutize(f[i]);
			img = $('img_'+f[i].identify() );
			img.style.top = Element.cumulativeOffset(f[i])[1]-84;
			img.style.left = Element.cumulativeOffset(f[i])[0]-203;
		}
	}

	var bannerLoadCount=0;
	var bannerMaxCount=0;
	var bannerCategory='';
	var bannerTimeOut=0;

	function rotateBanner(category,max,time) {
		bannerMaxCount=max;
		bannerLoadCount=0;
		bannerCategory=category;
		bannerTimeOut = time*1000;
		loadNextBanner();
	}

	function loadNextBanner() {
		bannerLoadCount++;
		banner = $('tvtbanner');
		bl = (bannerLoadCount < 10 ? '0'+bannerLoadCount : bannerLoadCount);
		if ( banner ) {
			banner.src="http://static.tvtorrents.com/images/banners/tvt-banner-cat"+bannerCategory+"-"+bl+".png";
			if ( bannerLoadCount==bannerMaxCount)
				bannerLoadCount=0;
		}
		setTimeout("loadNextBanner()",bannerTimeOut);
	}

	/** jQuery **/
	jQuery(document).ready( function() {
		
		/** Calc local time **/
		doTimes();
		
		/** Show search **/
		jQuery(".showsearch").submit( function() {
			jQuery('#body_span').load( host+'/ajax/show_search.p',"search="+jQuery(this).children("input:text").val() );
			return false;
		});
		jQuery(".showsearch input:button").click( function() {
			jQuery('#body_span').load( host+'/ajax/show_search.p',"search="+jQuery(this).siblings("input:text").val() );
			return false;
		});
		jQuery(".showsearch input:text").keyup( function() {
			if ( jQuery(this).val().match(/ $/)  )
				jQuery('#body_span').load( host+'/ajax/show_search.p',"search="+jQuery(this).val() );
			//new Ajax.Updater('body_span', host+'/ajax/show_search.p?search='+field.value.replace(/^\s*/, "").replace(/\s*$/, "").replace(/\,/,"").split(' '), { method:'get'}  );		
		}).focus( function() {
			if ( jQuery(this).val()=='Search show') {
				jQuery(this).val('').css( "color","#000");				
			}
		}).blur( function() {
			if ( jQuery(this).val().length==0 ) {
				jQuery(this).css( "color","#aaa").val('Search show');
			}
		}).trigger('blur');
		
		
		/** Torrent search **/
		jQuery(".torrentsearch input:text").focus( function() {
			if ( jQuery(this).val()=='Search torrent') {
				jQuery(this).val('').css( "color","#000");				
			}
		}).blur( function() {
			if ( jQuery(this).val().length==0 ) {
				jQuery(this).css( "color","#aaa").val('Search torrent');
			}
		}).trigger('blur');
		
		jQuery(".natcheck-light img").live('click', function() {
			jQuery(this).parent("span").load( host+"/NatCheckServlet?query="+q+"&r="+Math.random()+"&reset=true"  );
		});

		trafficLights();


	});
	
	function trafficLights() {
		// update yellow
		jQuery(".natcheck-light img[src$='docheck.gif']").each( function() {
			q = jQuery(this).parent("span").attr("data-query");
			//q = /query=(.*)&/.exec(this.src)[1];
			jQuery(this).parent("span").load( host+"/NatCheckServlet?query="+q+"&r="+Math.random()  );
		});
		setTimeout( 'trafficLights()',15000);
	}
	
