$(document).ready(function(){

	// TABS
	$("#NewsBlock > .BlockContent > .BoxTop").click(function(){
		if( !$(this).hasClass("OpenBox") ){
			if( $("#NewsBlock > .BlockContent > .OpenBox") ){
				$("#NewsBlock > .BlockContent > .OpenBox").removeAttr("id");
			}
			$("#NewsBlock > .BlockContent > .BoxContent").slideUp("slow");
		  	$(this).next("div").slideDown("slow");
			$("#NewsBlock > .BlockContent > .OpenBox").removeClass("OpenBox");
			$(this).addClass("OpenBox");
			if($(this).hasClass("FirstBox")){
				$(this).attr("id", "FirstBox");
			}
		}else{
			$(this).next("div").slideUp("slow");
			$(this).removeClass("OpenBox");
		}
	$(".SuperSearcher > .Tabs > .services > a").focus();
	});

	// Reopen tab if is anchor
	if(document.location.hash.length>0){
		$("#NewsBlock > .BlockContent > .BoxTop").ready(function(){

			// Get anchor data (need to rewrite)
			var href = window.location.href.split('#');
			var currentAnchor = href[1];
			//var currentAnchor = window.location.hash;
			var openTab = currentAnchor.split('openTab=')[1] - 1;

			if( !$("#NewsBlock > .BlockContent > .BoxContent").eq(openTab).prev("div").hasClass("OpenBox") ){
				$("#NewsBlock > .BlockContent > .BoxContent").slideUp("slow");
			  	$("#NewsBlock > .BlockContent > .BoxContent").eq(openTab).slideDown("slow");
				$("#NewsBlock > .BlockContent > .OpenBox").removeClass("OpenBox");
				$("#NewsBlock > .BlockContent > .BoxContent").eq(openTab).prev("div").addClass("OpenBox");
			}
		});
	}

	// TABS horoscope
	// if( $('#horoscope_select').lenght ){
		$('#horoscope_select').change(function(){
			window.location = '/izklaide/horoskopi?sign_id='+ this.value;
		});
	// }

	$('.movie_box .rating').css('cursor', 'pointer');
	$('.movie_box .rating').click(function(){
		location.href = $('h2 > a', $(this).parent().parent() ).attr('href');
	});

	$("#TopsBlock li").click(function(){
		if($(this).hasClass('selected') != true){
			if($(this).attr('id') == 'topTabDvd'){
				$('#topTabKino').removeClass('selected');
				$('#topKino').hide();
				$('#topTabDvd').addClass('selected');
				$('#topDvd').show();

			}else if($(this).attr('id') == 'topTabKino'){
				$('#topTabDvd').removeClass('selected');
				$('#topDvd').hide();
				$('#topTabKino').addClass('selected');
				$('#topKino').show();
			}
		}
	});

	$("ul.SportResults .Prev, ul.SportResults .Next").click(calendarResults);

	function calendarResults(){
		var type = $(this).attr('class');
		if(typeof(calendarPage) == 'undefined'){
			calendarPage = 1;
		}
		if(type == 'Next'){
			calendarPage++;
		}else{
			calendarPage--;
		}
		$.ajax({
			type: 'POST',
			url: '/?calendar&type='+ type,
			data: 'page='+ calendarPage,
			beforeSend: function(){
				//$('ul.SportResults').html('<img src="/img/indicator2.gif" alt="" style="vertical-align:middle" />');
				$('ul.SportResults .More').html('<div style="text-align:center"><img src="/img/indicator2.gif" alt="" /></div>');
			},
			success: function(xmlData){
				$('ul.SportResults').html( xmlData );
				$("ul.SportResults .Prev, ul.SportResults .Next").click(calendarResults);
			}
		});
	}

});

function openTab(tid){
	window.location = '#?openTab='+ tid;
	return false;
}



//flip banner

var fi_flip = new Object();


/* Function called from Flash */
function sizeup987(){
    document.getElementById('fi_flip_cornerBig').style.visibility = 'visible';
    document.getElementById('fi_flip_cornerSmall').style.visibility = 'hidden';
		fi_flip.check = setTimeout("fi_getFrame(true)", 500);
}
function sizedown987(){
    document.getElementById("fi_flip_cornerSmall").style.visibility = "visible";
    document.getElementById("fi_flip_cornerBig").style.visibility = "hidden";
}


/* Flash connection functions */
function fi_thisMovie(movieName) {
  if (navigator.appName.indexOf ("Microsoft") !=-1) {
    return window[movieName]
  } else {
    return document[movieName]
  }
}
function fi_getFrame(first_time){
	movieName = 'fi_flip_cornerBigObject';
	if(document.getElementById('fi_flip_cornerBig').style.visibility == 'visible' || first_time == true){

		var fi_movie = fi_thisMovie(movieName);
		var cfrm = fi_movie.TCurrentFrame("_level0/");

		//console.info('checking... current frame='+cfrm);
		fi_flip.check = setTimeout("fi_getFrame(false)", 500);
		if((cfrm == 29) && !fi_movie.IsPlaying()){
			sizedown987();
			clearTimeout(fi_flip.check);
			//console.info('manual size down');
		}
		if((cfrm == 43) && !fi_movie.IsPlaying()){
			if(fi_movie.TCurrentFrame("/mask") == 0){
				//console.info('manual mask player - '+fi_movie.TCurrentFrame("/mask"));
				fi_movie.TGotoFrame("/mask","openCorner");
				fi_movie.TPlay("/mask");
				fi_movie.TGotoFrame("/mask2","openCorner");
				fi_movie.TPlay("/mask2");
				//console.info(': '+fi_movie.TCurrentFrame("/mask"));
				clearTimeout(fi_flip.check);
			}
		}
	}
}



fi_flip.applyConfig = function(conf) {

    // Small image dimensions (width, height)
    conf.image_small_dimensions = new Array(100, 100);
    // Large image dimensions (width, height)
    conf.image_large_dimensions = new Array(600, 600);

    fi_flip.component_url = conf.components_prefix;
    fi_flip.ad_url = escape(conf.redirect_url);
    fi_flip.small_path = conf.components_prefix + 'small.swf';
    fi_flip.small_image = escape(conf.image_small_url);
    fi_flip.small_width = conf.image_small_dimensions[0];
    fi_flip.small_height = conf.image_small_dimensions[0];
    fi_flip.small_params = 'ico=' + fi_flip.small_image;

    fi_flip.big_path = conf.components_prefix + 'large.swf';
    fi_flip.big_image = escape(conf.image_large_url);
    fi_flip.big_width = conf.image_large_dimensions[0];
    fi_flip.big_height = conf.image_large_dimensions[1];
    fi_flip.big_params = 'big=' + fi_flip.big_image + '&ad_url=' + fi_flip.ad_url;

}



fi_flip.init = function (conf) {

    fi_flip.applyConfig(conf);
    //document.write('<link type="text/css" href="'+fi_flip.component_url+'fi_flip.css" rel="stylesheet" />');
    // <fi_flip_cornerSmall>
    document.write('<div id="fi_flip_cornerSmall" style="margin-left:-100px;margin-top:2px;position:absolute;float:right;width:'+ fi_flip.small_width +'px;height:'+ fi_flip.small_height +'px;z-index:9999;">');
    // object
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
    document.write(' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"');
    document.write(' id="fi_flip_cornerSmallObject" width="'+fi_flip.small_width+'" height="'+fi_flip.small_height+'">');
    // object params
    document.write(' <param name="allowScriptAccess" value="always"/> ');
    document.write(' <param name="movie" value="'+ fi_flip.small_path +'?'+ fi_flip.small_params +'"/>');
    document.write(' <param name="wmode" value="transparent" />');
    document.write(' <param name="quality" value="high" /> ');
    document.write(' <param name="FlashVars" value="'+fi_flip.small_params+'"/>');
    // embed
    document.write('<embed src="'+ fi_flip.small_path + '?' + fi_flip.small_params +'" name="fi_flip_cornerSmallObject" wmode="transparent" quality="high" width="'+ fi_flip.small_width +'" height="'+ fi_flip.small_height +'" flashvars="'+ fi_flip.small_params +'" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
    document.write('</object></div>');
    document.write('</script>');
    // </fi_flip_cornerSmall>
    // <fi_flip_cornerBig>

    document.write('<div id="fi_flip_cornerBig" style="margin-top:2px;margin-left:-600px;float:left;position:absolute;width:'+ fi_flip.big_width +'px;height:'+ fi_flip.big_height +'px;z-index:9999;">');
    // object
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
    document.write(' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"');
    document.write(' id="fi_flip_cornerBigObject" width="'+ fi_flip.big_width +'" height="'+ fi_flip.big_height +'">');
    // object params
    document.write(' <param name="allowScriptAccess" value="always"/> ');
    document.write(' <param name="movie" value="'+ fi_flip.big_path +'?'+ fi_flip.big_params +'"/>');
    document.write(' <param name="wmode" value="transparent"/>');
    document.write(' <param name="quality" value="high" /> ');
    document.write(' <param name="FlashVars" value="'+ fi_flip.big_params +'"/>');
    // embed
    document.write('<embed src="'+ fi_flip.big_path + '?' + fi_flip.big_params +'" id="fi_flip_cornerBigEmbed" name="fi_flip_cornerBigObject" wmode="transparent" quality="high" width="'+ fi_flip.big_width +'" height="'+ fi_flip.big_height +'" flashvars="'+ fi_flip.big_params +'" swliveconnect="true" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
    document.write('</object></div>');
    document.write('<div class="fi_flip_clear_div"></div>');
    // </fi_flip_cornerBig>
    setTimeout('sizedown987();',500);

		/* Specially for IE */
		if (navigator.appName == "Microsoft Internet Explorer" &&  parseFloat(navigator.appVersion)<5) {
			function fi_addEvent(elm, evType, fn, useCapture) {
				if (elm.addEventListener) {
					elm.addEventListener(evType, fn, useCapture);
					return true;
				}	else if (elm.attachEvent) {
					var r = elm.attachEvent('on' + evType, fn);
					return r;
				}	else {
					elm['on' + evType] = fn;
				}
			}
			fi_addEvent(window, 'resize', function(){
    		document.getElementById('fi_flip_cornerSmall').style.visibility = 'hidden';
				setTimeout(sizedown987,100)
			},true);
		}
}