jQuery().ready(function(){
	
	//$('#content .big-blocks .radioshows').loadFlash({filename: '/web/files/inspenjoy.swf', width: 327, height: 300});
	$('#content .big-blocks .radioshows').loadFlash({filename: 'http://openx.njoybg.com/www/images/e2d63e0b04db692afba8cb47a5f7f260.swf', width: 327, height: 300});
	
	$('.blankable').focus(function() {
		if ($(this).val() == $(this).attr('title')) {
			$(this).val('');
		}
	}).blur(function() {
		if ($(this).val() == '') {
			$(this).val($(this).attr('title'));
		}
	});
	
	// Tooltip
	var tooltip = 0;
	$('#header #topbox #playing span').mouseover(function () {
		$('#tooltip').show(300);
	});
	
	$('#header #topbox #playing span, #tooltip').mouseenter(function () {
		clearTimeout(tooltip);
	}).mouseleave(function () {
		tooltip = setTimeout(function() {
			$('#tooltip').hide(300);
		}, 500)
	});
	
	 // Main carousel
	 $(function() {
		setInterval( "slideSwitch()", 5000 );
	});
	$('.subheader .carousel ol li:first').addClass('selected');
	$('.subheader .carousel ul li:first').addClass('active');
	
	$('.subheader .carousel ol li').click( function() {
		slideSwitchGo($(this).index());
	});
	/* commented out old home page carousel
	$('.subheader .carousel ol li').live('click', function () {
		$('.subheader .carousel ol li').removeClass('selected');
		$(this).addClass('selected');
		$('.subheader .carousel ul li').eq(current).addClass('active');
	});

	setTimeout(function () { rotate() }, 5000);
	*/
	


	
	
	
	
	// Sidebar gallery
	$("#sidebar .gallery").tabs({
		selected: 0, 
		fx: {opacity: 'toggle'},
		show: function(event, ui) {
			width = 0;
			elements = 0;
			$("#sidebar .gallery ul.header li:not(:last)").each(function() {
				width += $(this).width();
				elements += 1;
			});
			$("#sidebar .gallery ul.header li.last").css('width', ($("#sidebar .gallery ul.header").width() - width - elements) + 'px');
		},
		select: function(event, ui) {
			$('li', $(ui.tab).parents('ul:first')).removeClass('header-gray');
			$(ui.tab).parent().addClass('header-gray');
			$(ui.tab).parent().css('background-position', '-' + $(ui.tab).parent()[0].offsetLeft + 'px 0px');
		}
	});
	
	$("#sidebar .gallery ul li:first").addClass('header-gray');
	
	$('#sidebar .gallery ul.carousel').jcarousel();
	
	$('#sidebar .minisites ul').jcarousel({
		scroll: 1,
		auto: 2,
		wrap: 'last',
		initCallback: function(carousel) {
			carousel.clip.hover(function() {
		        carousel.stopAuto();
		    }, function() {
		        carousel.startAuto();
		    });
			$('#sidebar .minisites em.next').click(function() {
				carousel.startAuto(0);
				carousel.next();
				return false;
			}); 
			$('#sidebar .minisites em.prev').click(function() {
				carousel.startAuto(0);
				carousel.prev();
				return false;
			}); 
		},
		itemFirstInCallback: {
			onBeforeAnimation: function(carousel, li, index, state) {
				$('var', $(li).parents('.minisites')).html(index);
			},
			onAfterAnimation: false
		},
		buttonNextHTML: null,
		buttonPrevHTML: null
	});
	
	// Articles
	$('#articles .filter #category_id').change(function () {
		category_id = $('.filter #category_id').val();
		if (parseInt(category_id)) {
			window.location = _root + 'news/category/' + $('.filter #category_id').val();
		} else {
			window.location = _root + 'news/';
		}
	});
	
	$('#article .gallery li a').click(function () {
		$('#article .gallery img:first').attr('src', $('img', $(this)).attr('src').replace('79x61', '346x277'));
	});
	
	// Main content box tabs
	$(".content-box.tabs .header").tabs({
		selected: ((window.location.toString().indexOf('#content-tab-') >= 0) ?  (window.location.toString().substr(window.location.toString().indexOf('#content-tab-') + 13) * 1 - 1) : 0), 
		fx: {opacity: 'toggle'},
		show: function(event, ui) {
			width = 0;
			elements = 0;
			$(".content-box.tabs .header ul li:not(:last)").each(function() {
				width += $(this).width();
				elements += 1;
			});
			$(".content-box.tabs .header ul li.last").css('width', ($(".content-box.tabs .header ul").width() - width - elements) + 'px');
		},
		select: function(event, ui) {
			$('li', $(ui.tab).parents('ul:first')).removeClass('header-gray');
			$(ui.tab).parent().addClass('header-gray');
			$(ui.tab).parent().css('background-position', '-' + $(ui.tab).parent()[0].offsetLeft + 'px 0px');
		}		
	});
	
	if (window.location.toString().indexOf('#content-tab-') >= 0) {
		$(".content-box.tabs .header ul li:eq(" + (window.location.toString().substr(window.location.toString().indexOf('#content-tab-') + 13) * 1 - 1) + ")").addClass('header-gray');
	} else {
		$(".content-box.tabs .header ul li:first").addClass('header-gray');	
	}
	
	// Media fancybox
	$('#media .videos a, .gallery-content .videos a').click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'centerOnScroll': true,
				'transitionIn'	: 'fade',
				'transitionOut'	: 'fade',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				 					'wmode'				: 'transparent',
									'allowfullscreen'	: 'true'
									}
		});
		return false;
	});
	
	$('#media a.gallery').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'overlayShow'	:	true,
		'centerOnScroll':	true
	});
	
	if (location.toString().indexOf('media/images') >= 0 && location.hash.toString().length) {
		$('#media a#image-' + location.hash.toString().replace('#', '')).click();
	}
	
	// Top 40's player big
	$('#top40 .vote strong').click(function() {
		// Clear all flash players
		clear_players();
		
		$(this).css('display', 'block'); // Ostava imeto na pesenta
		$(this).addClass('nobgr');
		$(this).parents('div:first').prepend("<div></div>");
		$('div', $(this).parents('div:first')).loadFlash({filename: _root + 'web/flash/player-big.swf', width: 48, height: 48, params: {flashvars: 'autoPlay=1&fileURL='+$(this).attr('title')}});
		
		// Stop the rock, don`t stop the rock
		return false;
	});
	
	// Top 40's player sidebar
	$('#sidebar .top40 .song span').click(function() {
		// Clear all flash players
		clear_players();
		
		// Append data
		$(this).css('display', 'none');
		$(this).parents('div:first').prepend("<div></div>");
		$('div', $(this).parents('div:first')).loadFlash({filename: _root + 'web/flash/player-small.swf', width: 23, height: 23, params: {flashvars: 'autoPlay=1&fileURL='+$(this).attr('title')}});
		// Stop the rock, don`t stop the rock
		return false;
	});	
	
	// Media Audio player
	$('#media ol:not(.pager) li a').click(function() {
		// Clear all flash players
		clear_players();

		// Append data
		$(this).css('display', 'none');
		$(this).parents('li:first').prepend("<div></div>");
		$('div', $(this).parents('li:first')).loadFlash({filename: _root + 'web/flash/player-big.swf', width: 47, height: 47, params: {flashvars: 'autoPlay=1&fileURL='+$(this).attr('href')}});
		// Stop the rock, don`t stop the rock
		return false;
	});	
	
	// sidebar Media Audio player
	$('#sidebar .audio span').click(function() {
		// Clear all flash players
		clear_players();
		
		// Append data
		$(this).css('display', 'none');
		$(this).parents('li:first').prepend("<div></div>");
		$('div', $(this).parents('li:first')).loadFlash({filename: _root + 'web/flash/player-small.swf', width: 23, height: 23, params: {flashvars: 'autoPlay=1&fileURL='+$(this).attr('title')}});
		// Stop the rock, don`t stop the rock
		return false;
	});
	
	// Weather 
	$('#sidebar .weather select').change(function(){
		info = $('option:selected', $(this)).attr('title').split('::');
		$('#sidebar .weather .min span').html(info[1]);
		$('#sidebar .weather .max span').html(info[2]);
		$('#sidebar .weather .icon img').attr('src', _root + 'web/images/weather/84x55/symbol' + info[3] + '.png');
	});
	
	$('#weather .filter #city_id').change(function () {
		city_id = $('.filter #city_id').val();
		if (city_id != 0) {
			window.location = _root + 'weather/city/' + city_id;
		}
	});	
	
});

function clear_players() {
	$('#sidebar .audio li div').remove();
	$('#sidebar .audio span').css('display', 'block');	
	$('#sidebar .top40 .song div').remove();
	$('#sidebar .top40 .song span').css('display', 'block');
	$('#top40 .vote strong').removeClass('nobgr');
	$('#top40 .vote div').remove();
	$('#top40 .vote strong').css('display', 'block');
	$('#media ol li div').remove();
	$('#media ol li а').css('display', 'block');		
}
/* commented out old page carousel
var current = 1;

function rotate() {
	if (current + 1 <= 5) {
		current++;
	} else {
		current = 1;
	}
	$('.subheader .carousel ol li').eq(current - 1).click();
	setTimeout(function () { rotate() }, 5000);
}
*/ 
function slideSwitch() {
	
    var $active = $('.subheader .carousel ul li.active');
	
	var $activepage = $('.subheader .carousel ol li.selected');

    if ( $active.length == 0 ) $active = $('.subheader .carousel ul li:last');
	
	if ( $activepage.length == 0 ) $activepage = $('.subheader .carousel ol li:last');
	
    var $next =  $active.next().length ? $active.next()
        : $('.subheader .carousel ul li:first');
	
	var $nextpage =  $activepage.next().length ? $activepage.next()
        : $('.subheader .carousel ol li:first');
	
    $active.addClass('last-active');
	
	$activepage.addClass('last-active');
	
	
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
			$activepage.removeClass('selected');
			$nextpage.addClass('selected');
        });
	}
	
function slideSwitchGo(cindex) {
	cindex = cindex-1;
	var $active = $('.subheader .carousel ul li').eq(cindex);
	
	var $activepage = $('.subheader .carousel ol li').eq(cindex);
	
	$('.subheader .carousel ul li').each(function(){ 
		$(this).removeClass('active last-active');
    });
	
	$('.subheader .carousel ol li').each(function(){ 
		$(this).removeClass('selected');
    });
	
	var $next =  $active.next().length ? $active.next()
        : $('.subheader .carousel ul li:first');
	var $nextpage =  $activepage.next().length ? $activepage.next()
        : $('.subheader .carousel ol li:first');
		
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
			$activepage.removeClass('selected');
			$nextpage.addClass('selected');
        });
}
