
$(document).ready(function(){
						   
	$('.picture_slide ul, .slide_video ul').jcarousel();
 	
	$('.picture_slide ul li a').click(function (event) {
		event.preventDefault();
		$('img.current_image').attr('src', $(this).attr('href'));
		$('.image_description').text($('img', $(this)).attr('alt'));
		$('#gallery_link').attr('href', $(this).attr('rel'));
		$('.img_container_relative a').attr('href',$(this).attr('href').replace('thumb_274x182','thumb_500x333'));
	});
	
	$('.img_container_relative a').click(function(e) {
		e.preventDefault();
		$('.gallery_container a[href="'+ $(this).attr('href') +'"]').click();
							  
	});
	
	$('.slide_video ul li a').click(function (event) {
		event.preventDefault();
		$('img.current_video').attr('src', $('img', $(this)).attr('src').replace('2.jpg', '0.jpg'));
		$('.video_description').text($('img', $(this)).attr('alt'));
		$('#play').attr({'href':$(this).attr('href'),'title':$('img', $(this)).attr('alt')});
		$("#play").colorbox({href:_root + 'web/flash.php?url=' + $("#play").attr('href').replace('watch?v=', 'v/')});
	});
	
	$('#pictures .img_container').mouseenter(function() {
		$('#gallery_link').stop().animate({opacity: 1.0},300);											  
	});
	$('#pictures .img_container').mouseleave(function() {
		$('#gallery_link').stop().animate({opacity: 0.0},300);											  
	});
	
	$("a[rel='gallery']").colorbox();
	if($("#play").length > 0)
	$("#play").colorbox({href:_root + 'web/flash.php?url=' + $("#play").attr('href').replace('watch?v=', 'v/')});
	
	/* News Videos */
	$("#news_video a").each(function() {
		$(this).colorbox({href:_root + 'web/flash.php?url=' + $(this).attr('href').replace('watch?v=', 'v/')});
	});

	// sidebar Media Audio player
	$('#sidebar #audio a, #rank_list li a, #news_audio a').click(function(event) {
		event.preventDefault();
		clear_players();
		$(this).css('display', 'none');
		$(this).parent('li').append("<div class='player'></div>");
		$('div.player', $(this).parent('li')).loadFlash({filename: _root + 'web/flash/MusicPlayerMini.swf', width: 17, height: 17, params: {flashvars: 'autoPlay=1&fileURL='+$(this).attr('href')}});
		return false;
	});
	
	// Top7 Media Audio player
	$('.vote p a').click(function(event) {
		event.preventDefault();
		clear_top7_players();
		$(this).css('display', 'none');
		$(this).parent('p').append("<div class='player'></div>");
		$('div.player', $(this).parent('p')).loadFlash({filename: _root + 'web/flash/MusicPlayerMini.swf', width: 17, height: 17, params: {flashvars: 'autoPlay=1&fileURL='+$(this).attr('href')}});
		return false;
	});
	
	// Sidebar Tab Navigation
	$(function () {
	    var tabContainers = $('div.tabs > div');	    
	    $('div.tabs ul.tabNavigation a').click(function () {
	        tabContainers.addClass('hidden').filter(this.hash).removeClass('hidden');	        
	        $('div.tabs ul.tabNavigation a').removeClass('selected');
	        $(this).addClass('selected');	        
	        return false;
	    }).filter(':first').click();
	});
	
	
	// Concert categories
	$('#concert_categories').change(function() {
		var cat_id = $(this).val();
		if(!cat_id) return;
		var location = String(self.location);
		if(location.indexOf('?') != -1) {
			var cl = location.substr(0,location.indexOf('?'));
			self.location = cl + '?category_id=' + cat_id;
		}
		else {
			self.location = location + '?category_id=' + cat_id;
		}
	});
	
	// 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;
		}
	});	
	
	$('#gallery_link').css('opacity','0.0');
	$('a, button').focus(function() {$(this).blur();});
	
	$("#ticker").Scroll();
	setInterval(function() {get_now_playing();}, 30 * 1000);	
	
	if($.browser.msie){
		$('#rank_list li:nth-child(odd), #audio ul li:nth-child(odd), .program li:nth-child(odd), .price_list tr:nth-child(odd)').css('background-color', '#f4f4f4');
	} 
	
	$('.gallery li:nth-child(3n+3)').css('padding-right','0');
	
	
});	

function slider_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


function get_now_playing() {
	date = new Date();
	$.ajax({
		url: xhr + '?method=get_now_playing&nocache=' + date.getTime(),
		success: function(html){			
			var current = $('#ticker span').html();
			if(current != html) {
				$('#ticker').remove();
				$('#radiobar .mask').append($('<p id="ticker" class="newsticker"><span>' + html + '</span></p>'));
				$("#ticker").Scroll();
			}
		}
	});
}

$.fn.Scroll = function(settings) {
	settings = $.extend({
		travelocity: 0.06
	}, settings);		
	return this.each(function(){
		var $strip = $(this);
		var containerWidth = $strip.parent().width();
		var stripWidth = $strip.children().width();
		var totalTravel = stripWidth+containerWidth;
		var defTiming = totalTravel/settings.travelocity;				
		function scrollnews(spazio, tempo){
			$strip.animate({left: '-='+ spazio}, tempo, "linear", function(){$strip.css("left", containerWidth); scrollnews(totalTravel, defTiming);});
		}
		scrollnews(totalTravel, defTiming);				
		$strip.hover(function(){ $(this).stop(); }, function(){
			var offset = parseInt($(this).css('left'));
			var residualSpace = offset + stripWidth;
			var residualTime = residualSpace/settings.travelocity;
			scrollnews(residualSpace, residualTime);
		});			
	});	
}

function clear_players() {
	$('#sidebar #audio li div').remove();
	$('#sidebar #audio a').css('display', 'block');	
	$('#rank_list div.player').remove();
	$('#rank_list a').css('display', 'inline-block');	
	$('#news_audio li div').remove();
	$('#news_audio li a').css('display', 'block');
}

function clear_top7_players() {
	$('.vote p div.player').remove();
	$('.vote p a').css('display','block');	
}


