/**
 * Tabination JavaScript File
 */

/* TABINATION */

var _activTab = 1;
var _activHover = 0;
var _timer = 0;
var _duration = 5000;
var _slow_tabin = 500;
var _video_shown = 0;
var _tab_hover = 0;

$(document).ready(function(){	
	
	$('#hot1').css("opacity", 0);
	$('#hot2').css("opacity", 0);
	$('#hot3').css("opacity", 0);
	$('#hot4').css("opacity", 0);
	
	$('#hot'+_activTab).css("opacity", 1);
	$('#hot'+_activTab).css('display', 'block');
	$('#tab'+_activTab).addClass("activ");
	
	$('.tab a').mouseover(function(e) {
		if(_activHover != $(this).parent().attr("id"))
		{
			_activHover = $(this).parent().attr("id");
			var clickedId = Number(_activHover.substring(3));
			reTab(clickedId);
		}
		
		e.stopPropagation();
		clearTimeout(_timer);
		_tab_hover = _activHover;
		
	});
	
	$('.tab a').mouseout(function(e) {
		clearTimeout(_timer);
		_timer = setTimeout(function(){
			clearTimeout(_timer);
			nextTab();
		},_duration);
		_tab_hover = 0;
	});
	
	$('.hot_banner').mouseover(function(e) {
		e.stopPropagation();
		clearTimeout(_timer);
	});
	
	$('.hot_banner').mouseout(function() {
		clearTimeout(_timer);
		_timer = setTimeout(function(){
			clearTimeout(_timer);
			nextTab();
		},_duration);
		
	});
	
	_timer = setTimeout(function(){
		clearTimeout(_timer);
		nextTab();
	},_duration);
	
});

function reTab(tabId){
	if(_video_shown == 0){
		
		//alert('next')
		if(tabId != _activTab || $('#hot'+_activTab).css("opacity") != 1)
		{
			clearTimeout(_timer);
			$('#tab'+_activTab).removeClass("activ")
			
			$('#hot'+_activTab).stop().animate({opacity: 0}, _slow_tabin * Number($('#hot'+_activTab).css("opacity")), function(){
				$('#hot'+_activTab).css('display', 'none');
				
				$('#hot'+tabId).css('display', 'block');
				$('#hot'+tabId).animate({opacity: 1}, _slow_tabin);
				$('#tab'+tabId).addClass("activ");
				_activTab = tabId;
			});
			if(_tab_hover == 0){
				_timer = setTimeout(function(){
					clearTimeout(_timer);
					nextTab();
				},_duration);
			}
			
		}
	
	}
}

function show_video(id, newsId, article) {
	//alert('otwieram');
	//alert($("#hot"+id).children().children().children('#zobacz_video').attr('id'));
	if($("#hot"+id).children().children().children('.zobacz_video').attr('class') == 'zobacz_video' && newsId != -1)
	{
		$('#hot_video').css('display', 'block');
		$('#hot_video').css('opacity', '0');
		
		//alert(newsId + ', ' +id);
		$("#hot_video").children('#close').attr('href', 'javascript:show_video('+id+','+newsId+')');
		$("#hot_video").children('#comments').attr('href', 'http://tvkanon.pl/video,'+newsId+'.html#komentarze');
		$("#hot_video").children('#read_more').attr('href', 'http://tvkanon.pl/video,'+newsId+'.html');
		if(article){
			$("#hot_video").children('#read_more').show();
		} else {
			$("#hot_video").children('#read_more').hide();
		}
		
		$("#flashplayer").empty();
		$("#flashplayer").append(getPlayerCode(newsId));
		
		//alert('dupa');
		$('#hot_video').animate({opacity: 1}, 'slow', function(){
			//
		});
		
		$("#hot"+id).children('div').children().children('.zobacz_video').attr('class', 'zamknij_video');
		
		reTab(id);
		_video_shown = 1;
	}
	else {
		$('#hot_video').animate({opacity: 0}, 'slow', function(){
			$('#hot_video').css('display', 'none');
			$("#hot"+id).children().children().children('.zamknij_video').attr('class', 'zobacz_video');
			_video_shown = 0;
		});
		
	}
	
}

function getPlayerCode(id){
	var playercode = '<div id="flashplayer" style="width:630px; height:430px;"><object width="100%" height="100%"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="wmode" value="transparent" /><param name="movie" value="files/videoplayer/VideoPlayer.swf?5_hot'+id+'_1" /><embed src="files/videoplayer/VideoPlayer.swf?5_hot'+id+'_1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="100%" height="100%" wmode="transparent"></embed></object></div>';
	return playercode;
}

function nextTab(){
	//alert("_activTab + 1 = " + _activTab + 1 + " is " + _activTab + 1 > 4);
	var next = (_activTab + 1 > 4) ? 1 : _activTab + 1;
	/*alert(_activTab + 1);
	alert(_activTab + 1 > 4);
	alert(next + " next");*/
	reTab(next);
}

