/**
 * Returns current timestamp
 * @return Int
 */
function now() {
	return +new Date;
}
/**
 * JSONP Request
 * @param url
 * @param data 
 * @param callback
 */
var _jsonc = now();
function jsonp(url, data, callback) {
	var head   = document.getElementsByTagName("head")[0];
	var script = document.createElement("script");
	var callbackName = "jsonp" + _jsonc++;	
	window[ callbackName ] = function(tmp){
		if(callback) callback(tmp);
		window[ callbackName ] = undefined;
		try{ delete window[ callbackName ]; } catch(e){}
		if ( head ) head.removeChild( script );
	};	
	var query  = [];
	for(var i in data) {
		query.push(i + "=" + encodeURIComponent(data[i]));
	}
	script.src = url + (url.indexOf("?") != -1 ? "&" : "?") +
				 query.join("&") + (query.length ? "&" : "") +
				 "json-callback=" + callbackName;
	script.charset = "utf-8";
	head.appendChild(script);
}

$(document).ready(function(){
if($.fn.fancybox){
	$('#various1').fancybox({
		'padding' : 0,
		'titleShow' : false,
		'overlayColor' : '#000'
	});
	
	$("a[rel=letopis_group]").fancybox({
		'width'		: '96%',
		'height'		: '96%',
		'autoScale'		: true,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'titlePosition' 	: 'over',
		'type'			: 'iframe',
		'showCloseButton'	: true,
		'centerOnScroll'	: true
		
	});

	$("a[rel=images_group]").fancybox({
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'titlePosition' 	: 'over',
		'showCloseButton'	: true,
		'padding'			: 1
/*		'centerOnScroll'	: true*/
	});


	$("a[rel=pdf_group]").fancybox({
		'width'		: '96%',
		'height'		: '96%',
		'autoScale'		: false,
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'titlePosition' 	: 'over',
		'type'			: 'iframe',
		'showCloseButton'	: true,
		'centerOnScroll'	: true
	});
}
	/* menu */
	$('#navbar a').click(function(){
		if(($(this).parent().find('.subNav').length)&&(!$(this).parent().find('.subNav').is(':visible')))
		{
			hideAllSubMenu();
			$(this).parent().addClass('activeSub').addClass('active');
			return false;
		}
		else if($(this).parent().find('.subNav').is(':visible'))
		{
			$(this).parent().removeClass('activeSub').removeClass('active');
			return false;
		}
	})
	
	/* toolbar */
		$("#toolbar .btnHide").click( function() {
			$("#toolbar .content").animate({height: 0}, 400 );

			$("#toolbar").animate({height: "26px"}, 500 );

			$("#toolbar .btnShow").show(150);
		});

		$("#toolbar .btnShow").click( function() {
			$("#toolbar .content").animate({height: "44px"}, 500 );

			$("#toolbar").animate({height: "44px"}, 400 );

			$("#toolbar .btnShow").hide(150);
		});
	

	/* ===================== popup ===================== */
		$('#testResult').click(function(){
			$('.test .popup').fadeIn(150);
			return false;
		});

		$('.test .popup .btnClose').click(function(){
			$('.test .popup').fadeOut(150);
		});
	/* ================================================= */


	/* social */
		$('#wrapper').removeClass('nojs');
		$(".social a").hover(function(){ $(this).children('img').animate({opacity:1},200) }, function(){ $(this).children('img').animate({opacity:0.5},200) }) 
		
	/* matches */
		$("#matches .slides").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
			visible: 1,
			mouseWheel: true,
			auto: 8000,
			speed: 250
		});
	
	/* control panel */
		$("#controlPanel .slides").jCarouselLite({
			btnNext: ".next2",
			btnPrev: ".prev2",
			visible: 5,
			mouseWheel: true,
			circular: false,
			speed: 250
		});
		
		$('.btn').click(function(){
			var t = $(this).attr('class');
			t = t.replace('btn ','');
			t = t.replace(' hidden','');
			var h = $.cookie('hiddenBlocks');
			if(h)h = h.split(',');
			$(this).toggleClass('hidden');
			
			if($(this).hasClass('hidden'))
				{
					$('#'+t).slideUp();
					$(setSelector).sortable( "refresh" );
					//$.cookie(setCookieName, $(setSelector).sortable("toArray"), { expires: setCookieExpiry, path: "/" });
					if(!h){
						h = t;
					}else{
						if(h.indexOf(t) != -1 ){
							//do nothin
						}else{
							h.push(t);
						}
					}
					$.cookie('hiddenBlocks', h, { expires: setCookieExpiry, path: "/" });
					jsonp("/udata/users/saveBlocksSettings.json?hidden_blocks="+h, {},	function(data){
						//do nothing
					});
					
				}
			else
				{ 
					$('#'+t).slideDown();
					$(setSelector).sortable( "refresh" );
					if(h.indexOf(t) != -1){
						h.splice(h.indexOf(t), 1);
					}
					$.cookie('hiddenBlocks', h, { expires: setCookieExpiry, path: "/" });
					jsonp("/udata/users/saveBlocksSettings.json?hidden_blocks="+h, {},	function(data){
						//do nothing
					});
				}
		});
		
		/* scoreboard */
		var $paneTarget = $('#scoreboard .w');	
		var scoreFirst=0;
		$paneTarget.stop().scrollTo( 'tr:eq('+scoreFirst+')', 200, {axis:'y'} );
		
		$('#scoreboard .btnUp').click(function(){
			if(scoreFirst>0)
			{
				scoreFirst--;
				$('#scoreboard .btnDown').fadeIn();
				$paneTarget.stop().scrollTo( '-=95px', 200, {axis:'y'} );
				//$paneTarget.stop().scrollTo( 'tr:eq('+scoreFirst+')', 200, {axis:'y'} );
			}
			if(scoreFirst==0)
				$('#scoreboard .btnUp').fadeOut();
		});
		$('#scoreboard .btnDown').click(function(){
			if((scoreFirst+6)<($('#scoreboard .w table tr').length-6))
			{ 
				scoreFirst++;
				$('#scoreboard .btnUp').fadeIn();
				$paneTarget.stop().scrollTo( '+=95px', 200, {axis:'y'} );
				//$paneTarget.stop().scrollTo( '+='+scoreFirst+')', 200, {axis:'y'} );
			}
			if((scoreFirst+12)==($('#scoreboard .w table tr').length-6))
					$('#scoreboard .btnDown').fadeOut();
		});
		
		$('#scoreboard .w').mousewheel(function(event, delta) {
			if(delta > 0)
			{
				if(scoreFirst>0)
				{
					scoreFirst--;
					$('#scoreboard .btnDown').fadeIn();
					$paneTarget.stop().scrollTo( '-=95px', 200, {axis:'y'} );
					//$paneTarget.stop().scrollTo( 'tr:eq('+scoreFirst+')', 200, {axis:'y'} );
					//$('#dragndrop').text(scoreFirst);
				}
				if(scoreFirst==0)
					$('#scoreboard .btnUp').fadeOut();
			}
			else
			{
				if((scoreFirst+6)<($('#scoreboard .w table tr').length-6))
				{ 
					scoreFirst++;
					$('#scoreboard .btnUp').fadeIn();
					$paneTarget.stop().scrollTo( '+=95px', 200, {axis:'y'} );
					//$paneTarget.stop().scrollTo( '+='+scoreFirst+')', 200, {axis:'y'} );
//					$('#dragndrop').text(scoreFirst);
				}
				if((scoreFirst+12)==($('#scoreboard .w table tr').length-6))
					$('#scoreboard .btnDown').fadeOut();
					
			}
			return false;
		});
		
		//var $paneTarget = $('.scoreboard .w');	
		var scoreFirst=0;
		$paneTarget.stop().scrollTo( 'tr:eq('+scoreFirst+')', 200, {axis:'y'} );
		
		$('.scoreboard .btnUp').click(function(){
			if(scoreFirst>0)
			{
				scoreFirst--;
				$(this).parent('.scoreboard').find('.btnDown').fadeIn();
				$(this).parent('.scoreboard').find('.w').stop().scrollTo( '-=95px', 200, {axis:'y'} );
			}
			if(scoreFirst==0) $(this).parent('.scoreboard').find('.btnUp').fadeOut();
		});
		
		$('.scoreboard .btnDown').click(function(){
			if((scoreFirst+6)<=($(this).parent('.scoreboard').find('.w table tr').length))
			{
				scoreFirst++;
				$(this).parent('.scoreboard').find('.btnUp').fadeIn();
				$(this).parent('.scoreboard').find('.w').stop().scrollTo( '+=95px', 200, {axis:'y'} );
			}
			if((scoreFirst*6)>=($(this).parent('.scoreboard').find('.w table tr').length)) $(this).parent('.scoreboard').find('.btnDown').fadeOut();
		});
		
		$('.scoreboard .w').mousewheel(function(event, delta) {
			if(delta > 0)
			{
				if(scoreFirst>0)
				{
					scoreFirst--;
					$(this).parent().find('.btnDown').fadeIn();
					$(this).parent('.scoreboard').find('.w').stop().scrollTo( '-=95px', 200, {axis:'y'} );
				}
				if(scoreFirst==0) $(this).parent().find('.btnUp').fadeOut();
			}
			else
			{
				if((scoreFirst+6)<=($(this).parent().find('.w table tr').length))
				{ 
					scoreFirst++;
					$(this).parent().find('.btnUp').fadeIn();
					$(this).parent('.scoreboard').find('.w').stop().scrollTo( '+=95px', 200, {axis:'y'} );
				}
				if((scoreFirst*6)>=($(this).parent().find('.w table tr').length)) $(this).parent().find('.btnDown').fadeOut();
			}
			return false;
		});
		
		
		
		
		
		
		
	/*	controlPanel */
		$('#controlPanel .btnOpen').click(function(){
			$('#controlPanel').animate({ height: '98px', width: '810px' }, 500);

			$('#controlPanel .container').animate({ height: '86px', opacity: '1' }, 500);
			
			$('#controlPanel .btnOpen').fadeOut(100);
			$('#controlPanel .hint').fadeOut(100);
			$('#controlPanel .btnClose').fadeIn();
		})

		$('#controlPanel .btnClose').click(function(){
			$('#controlPanel .container').animate({ height: '0', opacity: '0' }, 500);
			
			$('#controlPanel .btnClose').fadeOut()
			$('#controlPanel .hint').fadeIn(500)
			$('#controlPanel .btnOpen').fadeIn(500)

			$('#controlPanel').animate({ height: '36px', width: '258px' }, 500)
		})
	
	/* blue content shadow */
	$(".boxWrapp .box").live('mouseover',function(){ $(this).find('.overlay').fadeOut(200)});
	$(".boxWrapp .box").live('mouseleave',function(){ $(this).find('.overlay').fadeIn(400) }) 

	
	/*$("#newsTeaser").tabs({fx:{opacity: "toggle"},event:'mouseover'}).tabs("rotate", 4000, true);*/

	/* news */
	var s=$('#newsTeaser .display').length;
	var n=0;
	var stopNews=false;
	
	setInterval(function() { 
		
		if(!stopNews)
		{
		
			$('#newsTeaser .display.selected').fadeOut(600);
			$('#newsTeaser .tabs li.selected').removeClass('selected');
			$('#newsTeaser .display.selected').removeClass('selected');
		
			$('#newsTeaser .display:eq('+n+')').fadeIn(600);
			$('#newsTeaser .tabs li:eq('+n+')').addClass('selected');
			$('#newsTeaser .display:eq('+n+')').addClass('selected');
		
			if(n==(s-1))
				n=0;
			else
				n++
		}			

		}, 3000)
			
		$('#newsTeaser .tabs li').hover(function(){
			if(!$(this).hasClass('selected'))
			{
				stopNews=true;
				
				var t=$(this).index();
				
				$('#newsTeaser .display.selected').fadeOut(600);
				$('#newsTeaser .tabs li.selected').removeClass('selected');
				$('#newsTeaser .display.selected').removeClass('selected');
				
				$('#newsTeaser .display:eq('+t+')').fadeIn(600);
				$('#newsTeaser .tabs li:eq('+t+')').addClass('selected');
				$('#newsTeaser .display:eq('+t+')').addClass('selected');

				if(t==(s-1)) n=0;
				else n=t+1;
				
			}
		},
		function(){
			stopNews=false;
			
			
		})
		
	/* drag'n'drop */	
		$(setSelector).sortable({
			cancel: ".ui-state-disabled",
			axis: "xy",
			cursor: "move",
			tolerance: "pointer",
			opacity: 0.6,
			placeholder: 'placeholder',
			forcePlaceholderSize: true,
			dropOnEmpty: true,
			update: function() { getOrder(); }
		});

		//$( "#sortable1 li, #sortable2 li" ).disableSelection();
		
		
		
		// here, we reload the saved order
		restoreOrder();
		
		jsonp("/udata/users/getBlocksSettings.json", {}, function(data){
			hideBlocks(data);
			
		});
		
		function hideBlocks(data){
			//alert(data.is_auth);
			if(data.is_auth == 'false'){
				this.hiddenBlocks = $.cookie('hiddenBlocks');
			}else{
				
				this.hiddenBlocks = data.hidden_blocks;
				$.cookie('hiddenBlocks', this.hiddenBlocks, { expires: setCookieExpiry, path: "/" });
			}
			
			if(this.hiddenBlocks){
				this.hiddenBlocks = this.hiddenBlocks.split(",");
				for (var i = 0, n = this.hiddenBlocks.length; i < n; i++) {
					if(this.hiddenBlocks[i] != ''){
						//alert(this.hiddenBlocks[i]);
						$('#'+this.hiddenBlocks[i]).hide();
						$('.preview .'+this.hiddenBlocks[i]).addClass('hidden');
					}
				}
			}
		}
});

/* nav */
$(document).keyup(function(event){ if (event.keyCode == 27) { hideAllSubMenu(); } });
function hideAllSubMenu()
{
	$('#navbar li.activeSub').removeClass('activeSub').removeClass('active');	
}


/* drag'n'drop settings */

	// set the list selector
	var setSelector = "#dragndrop";
	// set the cookie name
	var setCookieName = "listOrder";
	// set the cookie expiry time (days):
	var setCookieExpiry = 7;


	// function that writes the list order to a cookie
	function getOrder() {
		// save custom order to cookie
		$.cookie(setCookieName, $(setSelector).sortable("toArray"), { expires: setCookieExpiry, path: "/" });
	}

	// function that restores the list order from a cookie
	//var hiddenBlocks;
	
	function restoreOrder() {
		var list = $(setSelector);
		if (list == null) return
		
		// fetch the cookie value (saved order)
		var cookie = $.cookie(setCookieName);
		if (!cookie) return;
		
		// make array from saved order
		var IDs = cookie.split(",");
		
		// fetch current order
		var items = list.sortable("toArray");
		
		// make array from current order
		var rebuild = new Array();
		for ( var v=0, len=items.length; v<len; v++ ){
			rebuild[items[v]] = items[v];
		}
		
		for (var i = 0, n = IDs.length; i < n; i++) {
			
			// item id from saved order
			var itemID = IDs[i];
			
			//alert(itemID);
			
			if (itemID in rebuild) {
			
				// select item id from current order
				var item = rebuild[itemID];
				
				// select the item according to current order
				var child = $("ul#dragndrop").children("#" + item);
				
				// select the item according to the saved order
				var savedOrd = $("ul#dragndrop").children("#" + itemID);
				
				// remove all the items
				child.remove();
				
				// add the items in turn according to saved order
				// we need to filter here since the "ui-sortable"
				// class is applied to all ul elements and we
				// only want the very first!  You can modify this
				// to support multiple lists - not tested!
				$("ul#dragndrop").filter(":first").append(savedOrd);
			}
		}
		
		/*jsonp("/udata/users/getBlocksSettings.json", {}, function(data){
			hideBlocks(data);
			
		});
		
		function hideBlocks(data){
			//alert(data.is_auth);
			if(data.is_auth == 'false'){
				this.hiddenBlocks = $.cookie('hiddenBlocks');
			}else{
				
				this.hiddenBlocks = data.hidden_blocks;
				$.cookie('hiddenBlocks', this.hiddenBlocks, { expires: setCookieExpiry, path: "/" });
			}
			
			if(this.hiddenBlocks){
				this.hiddenBlocks = this.hiddenBlocks.split(",");
				for (var i = 0, n = this.hiddenBlocks.length; i < n; i++) {
					if(this.hiddenBlocks[i] != ''){
						//alert(this.hiddenBlocks[i]);
						$('#'+this.hiddenBlocks[i]).hide();
						$('.preview .'+this.hiddenBlocks[i]).addClass('hidden');
					}
				}
			}
		}*/
		
	}

/*$(document).ready( function() {
$('.login').click(function() { window.location = '/users/login_do'; });
});*/

