var flashTimer = null;
var flashState = flashCount = 0;
function submitEms( q ){

	var e = $ES( 'input' );
	var c = 0;

	for( i=0; i<e.length; i++ )
	{
		if ( e[i].name == 'where' ) 
		{
			if( e[i].checked == true )
			{
				if( e[i].value == 'local' ){
					
					var cbox_open = ( $('just_open').checked ) ? 'igen' : 'nem';
					var cbox_title = ( $('just_title').checked ) ? 'igen' : 'nem';
					
					window.location = 'kereso.php?q=' + q + '&title=' + cbox_title + '&open=' + cbox_open;
					return false;
				}
				else if( e[i].value == 'abr' ){
					
					if( $('portals').value == 'nope' ){
						flash( 'portals' );	
						//alert( 'Kérlek válassz egy portált a legördülő menüből!' );

						return false;
					}
					
					window.location = 'browse.php?q=' + q + '&tp=' + $('portals').value;
					return false;
				}
				c++;
			}
		}
	}
	if( c == 0 ) 
	{
		alert('Kérjük válassz, hogy a Torrentek.hu oldalon, vagy egy nemzetközi oldalon keresnél.')
		return false;
	}
}

/*
	Ez csekkolja le, hogy a nemzetközi portáloknál be van-e ütve valami értelmes és submitolhat-e.
	E?
*/
function interCanSubmit()
{
	var formula = $('k');
	var inputTxt = $('nk_inp').value;

	if( inputTxt != 'Kulcsszó' && inputTxt.length > 2 )
	{
		formula.submit();
	}
}
function subCanSubmit()
{
	var formula = $('eff');
	var inputTxt = $('q_sub').value;

	if( inputTxt != 'Kulcsszó' && inputTxt.length > 2 )
	{
		formula.submit();
	}
}

function flash(el)
{
	if(flashState == 1)
	{
		$(el).style.backgroundColor = "#333";
		flashState = 0;
	}
	else
	{
		$(el).style.backgroundColor = "#090";
		flashState = 1;
	}
	
	if(flashCount == 7)
	{
		$(el).focus();
		clearTimeout(flashTimer);
		flashCount = flashState = 0;
	}
	else
	{
		flashTimer = setTimeout("flash('"+el+"')",50);
		flashCount++;
	}
	
}

function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	}
	else {
		el.style.display = '';
	}
}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}
	
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function setTarget(e){

	// Nemzetközi form csekkolása.
	if( $( 'automata' ).checked == false )
	{
		if( e.id == 'k' && $('portals').value == 'nope' ) {
			flash( 'portals' );	
			return false;
		}
		if( e.id == 'k' && $('nk_inp').value == 'Kulcsszó' ) {
			alert('Mit szeretnél keresni???.')
			return false;
		}
	} else {
	
		return true;	
	}

	// Felirat kereső form csekkolása.
	if( e.id == 'eff' && $('s_portal').value == 'nope' ) {
		flash( 's_portal' );	
		return false;
	}
	if( e.id == 'eff' && $('q_sub').value == 'Kulcsszó' ) {
		alert('Mit szeretnél keresni???.')
		return false;
	}

	var myForm1 = $('k');
	var myForm2 = $('l');
	var myForm3 = $('f');
	//
	var box = $('c');
	//
	myForm1.target = ( box.checked ) ? '_blank' : '_self' ;
	myForm2.target = ( box.checked ) ? '_blank' : '_self' ;
	myForm3.target = ( box.checked ) ? '_blank' : '_self' ;
}

function NewWindow(mypage,myname,w,h,scroll){
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable=false'
	win = window.open(mypage,myname,settings)
}

window.onload = function(){

		if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
			window.attachEvent("onload", fnLoadPngs);
		}

		var cli_info = getCookie( 'clients' );

		if( cli_info == 'nyitva' || !cli_info ){
			$('download').style.height = '300px';
		}

		function fnLoadPngs() {
			
			var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
			var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
		
			for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {

				if (itsAllGood && img.src.match(/\.png$/i) != null) {
					var src = img.src;
					
					var div = document.createElement("DIV");
					div.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizing='scale')"
					div.style.width = img.width + "px";
					div.style.height = img.height + "px";
					if( img.style.position ) div.style.position = img.style.position;
					if( img.style.right ) div.style.right = img.style.right;
					if( img.style.top ) div.style.top = img.style.top;
					if( img.style.left ) div.style.left = img.style.left;
					if( img.style.bottom ) div.style.bottom = img.style.bottom;
					if( img.style.cursor ) div.style.cursor = img.style.cursor;
					if( img.onclick ) div.onclick = img.onclick;
					//if( img.id ) div.id = img.id;
					
					img.replaceNode(div);
				}
			}
		}
}

function hide_info()
{
	var hide = new Fx.Style( 'info1', 'opacity', { duration:850, 
													transition:Fx.Transitions.bounceOut });
	hide.start(1, 0.2);
	
	var hide2 = new Fx.Style( 'info2', 'opacity', { duration:850, 
													transition:Fx.Transitions.elasticOut, 
													onComplete: hide });
	hide2.start(1, 0.2);
	setTimeout( "ezt('hide')", 500 );
	setCookie( 'info', false, 30 );
}

function toggle_clients()
{
	// INIT
	var state = getCookie( 'clients' );
	if( !state ){ setCookie( 'clients', 'nyitva', 30 ) }
	//
	var hheight = $('download').offsetHeight;

	var hide = new Fx.Style( 'download', 'height', { duration:500,
													 transition:Fx.Transitions.elasticOut });
	// NA TÖRTÉNJEN IS VALAMI
	switch( state )
	{	
		case 'nyitva':
			hide.start( hheight, 25 );
			//$('cbutton').src = 'images/icons/apply.png';
			setCookie( 'clients', 'zarva', 30 )
		break;
		
		case 'zarva':
			hide.start( 25, 300 );
			//$('cbutton').src = 'images/icons/delete.png';
			setCookie( 'clients', 'nyitva', 30 )
		break;
	}
}

function ezt( has )
{
	switch( has ){
		case 'show':
			setCookie( 'info', true, 30 );
			$('info1').style.display = 'block';
			$('info2').style.display = 'block';
			$('info1').style.visibility = 'hidden';
			$('info2').style.visibility = 'hidden';
			var show = new Fx.Style( 'info1', 'opacity', { duration:850, 
															transition:Fx.Transitions.bounceOut });
			show.start(0, 1);
			var show2 = new Fx.Style( 'info2', 'opacity', { duration:850, 
															transition:Fx.Transitions.bounceOut });
			show2.start(0, 1);
		break;

		case 'hide':
			$('info1').style.display = 'none';
			$('info2').style.display = 'none';
		break;
	}
}