/*
 * Player
 *
 */
var playerSetInterval = 90000;
var requestURL  = 'plugins/player/api.php?service_name='+service_name+'&service_id='+service_id+'&return=json';
var selectedBlock = 'defaultBlock';

( function ExternalLinks() {
	$( document ).ready(function() {	
		$( '.external' ).click( function() {
		var target = $( this ).attr( 'href' );
			if (top.opener && !top.opener.closed) {
				top.opener.location.href = target;
				top.opener.focus();
				return false;
			} 
			return true;
		});
	});
}() );


( function ListenLive() {
	$( document ).ready(function() {	
		$( '#onAir > .browse > a' ).click( function() {		
			var target = $( this ).attr( 'href' );
			if (target == "#nextShow" ) {
				if (selectedBlock == 'defaultBlock') {
					showListenLiveBlock('nextBlock');
				} else if (selectedBlock == 'previousBlock') {
					showListenLiveBlock('defaultBlock');
				} 
			} else {
				if (selectedBlock == 'defaultBlock') {
					showListenLiveBlock("previousBlock");
				} else if (selectedBlock == 'nextBlock') {
					showListenLiveBlock('defaultBlock');
				} 
			}
		} );
		
		setInterval("getShowInfo()", playerSetInterval );
	});
}() );

function showListenLiveBlock(block) 
{
	selectedBlock = block;

	$( '#onAir > #previousBlock' ).hide();
	$( '#onAir > #nextBlock' ).hide();
	$( '#onAir > #defaultBlock' ).hide();
		
	switch (block) {
		case "nextBlock":
			$( '#onAir > #nextBlock' ).fadeIn("slow");
			break;
		case "previousBlock":
			$( '#onAir > #previousBlock' ).fadeIn("slow");
			break;
		default:
			$( '#onAir > #defaultBlock' ).fadeIn("slow");
			break;
	}
}
function getShowInfo() {
	$.getJSON(requestURL , function(data) {
	
		$( '#nowPlaying > h2 ' ).text( data.song_title + ' - ' + data.song_artist );
	
		$( '#songname' ).html(data.song_title);
		$( '#artistname' ).html(data.song_artist);
		$( '#djname' ).html(data.dj_name);
		$( '#djlink' ).attr("href", data.dj_url);
		$( '#djlinkdotted' ).attr("href", data.dj_url);
		$( '#djtime' ).html(data.dj_times);

		$( '#djname-next' ).html(data.dj_name_next);
		$( '#djtime-next' ).html(data.dj_times_next);
		$( '#djlinkplayer-next > a' ).attr("href", data.dj_url_next);
		$( '#djpic-next' ).attr("src", data.dj_name_next);
		
		$( '#djpic' ).attr("src", data.dj_image);
		$( '#djname-previous' ).html(data.dj_name_previous);
		$( '#djpic-previous' ).attr("src", data.dj_image_previous);
		$( '#djlinkplayer-previous > a' ).attr("href", data.dj_url_previous);
		$( '#djtime-previous' ).html(data.dj_times_previous);
		
		$( '#djtimegoout' ).html(data.dj_times);
		$( '#djnamegoout' ).html(data.dj_name_next);
		$( '#djpicgoout' ).attr("src", data.dj_image_small);
    });
}



( function leadPromotion() {
	$( document ).ready( function() {
			var timer;
			var index = 1;
			var length = $( '#promoContent div.lead ul.paging li' ).length;
			var thumbnail;
			var mainImage;
			var currentItem = $( '#promoContent div.lead ul.paging li:first' );
			var pagingAnchors = $( '#promoContent div.lead ul.paging a' );
			var leadContainer = $( '#promoContent div.lead' );
			
			function periodicallyUpdate() {
				if ( timer != null ) {
					clearTimeout( timer );
					timer = null;
					if ( index < length ) {
						currentItem.next().find( 'a' ).trigger( 'click' );
					}
					else {
						$( '#promoContent div.lead ul.paging li:first' ).find( 'a' ).trigger( 'click' );
						index = 0;
					};
					index ++;
				};
				timer = setTimeout( periodicallyUpdate, 2000 );
			};
			periodicallyUpdate();
			
			leadContainer.mouseover( function() {
				clearTimeout( timer );
				timer = null;
			} );
			leadContainer.mouseout( function() {
				periodicallyUpdate();
			} );
			
			pagingAnchors.click( function( event ) {
				if ( $( event.target ).is( 'a' ) ) {
					currentItem = $( this ).parent( 'li' );
					if ( $( this ).parent().hasClass( 'active' ) ) {
						return false;
					}
					else {
						$( '#promoContent div.lead ul.paging li:first' ).find( 'a' ).addClass( 'normal' );
						
						var target = $( this ).attr( 'rel' );
						$( '#promoContent > div.lead > ol.content > li' ).hide();
						$( target ).show();
					};
					$( '#promoContent div.lead ul.paging li' ).removeClass( 'active' );
					$( this ).parent().addClass( 'active' );
					return false;
				};
			} );
			currentItem.find( 'a' ).trigger( 'click' );
	} );
}() );

