/*
Video Controls for Vimeo and YouTube videos in home page feature.
*/

var params = {
	allowscriptaccess: 'always',
	allowfullscreen: 'true',
	wmode: 'transparent'
};

var attributes = {};

jQuery(document).ready(function($) {
	
	//do live show status
	$.ajax({
	  url: $('#radio_stat_url').val(),
	  data: {},
	  success: processLiveShows,
	  dataType: 'json'
	});
	
	$("#featured").easySlider({
		auto: true,
		continuous: true,
		pause: 10000,
		speed: 400,
		numeric: true
	});
	
	$('#prevTag,#nexTag').bind('click', pageVideoTags);
	$('#prevVidPage,#nextVidPage').bind('click', pageVideos);
	$('.vidThumb').bind('click', viewFeatureVideo);
    $('#adspace_header').bind('mouseover', function(){
        //$('#premium_mem_ad').show('slow');
    }).bind('mouseout', function(){
        //$('#premium_mem_ad').hide();         
    });
    $('#premium_mem_ad').bind('mouseout', function(){
        $(this).hide();
    }).click(function(){$(this).hide();});
});

function viewFeatureVideo(event){
	event.preventDefault();
	postId = jQuery(this).attr("id");
	jQuery.ajax({
		url: jQuery('#prem_vid_loader_url').val(),
		data: { p: postId, r: "feature" },
		success: loadPremVideo,
		dataType: 'json'
	});
}

function loadPremVideo(data){
	jQuery("#featured_content").replaceWith(data['html']);
	playerParams = data['params'];
	swfobject.embedSWF(
			playerParams.url, 
			playerParams.domEl,
			playerParams.width,
			playerParams.height,
			playerParams.minFlash,
			playerParams.altFlash,
			playerParams.flashVars,
			params,
			playerParams.attr
	);
	pos = jQuery("#featured_content").offset();
	window.scrollTo(pos.left, pos.top);
}

function processLiveShows(data){
	data = eval(data);
	jQuery('#radio_status').html("");
	list = jQuery("<ul id='liveradio'></ul>");
	items = [];
	if(data.length > 0 ){
		for(var i = 0; i < data.length; i++){
			list.append('<li>' + data[i].station + '<a href="' + data[i].url +'">listen now &raquo;</a></li>');
		}
		jQuery('#radio_status').append(list).slideDown();
		jQuery('#liveradio').newsticker(8000); 
	}else{
		jQuery('#radio_status').hide();
	}
}

function pageVideoTags(event){
	id = jQuery(this).attr("id");
	nextPage = jQuery('#currentpage').val();
	//do nothing at paging limits
	if((nextPage == 1 && id == "prevTag") ||
		(nextPage == jQuery('#pages').val() && id =="nexTag"))
		return;
		
	if(id == "prevTag"){
		nextPage--;
	}else{
		nextPage++;
	}	
		
	jQuery.ajax({
        type: "POST",
		url: jQuery('#tag_page_url').val(),
		data: { action: 'video_data', page: nextPage, pagesize: 15, seltags: jQuery("#currentTag").val(), req: "tags" },
		success: updateTagPage,
		dataType: 'json'
	});
}

function pageVideos(event){
	id = jQuery(this).attr("id");
	nextPage = jQuery("#current_vid_page").val();
	
	//do nothing at paging limits
	if((nextPage == 1 && id == "prevVidPage") ||
		(nextPage == jQuery('#vid_page_count').val() && id =="nextVidPage"))
		return;
	
	if( id == "prevVidPage"){
		nextPage--;
	}else{
		nextPage++;
	}
	
	msg = '<img src="' + jQuery('#blockui_img_url').val() + '"/><h2>Just a moment...</h2>';
	
	jQuery("#vid-grid-container").block({		
		message: msg,
		css: { backgroundColor: 'transparent', color: '#000', border: 'none', width: '50%' },
		overlayCSS: { backgroundColor: '#fff', opacity: '0.8'}
	});	
	jQuery.ajax({
        type: "POST",
		url: jQuery('#tag_page_url').val(),
		data: { action: 'video_data', page: nextPage, pagesize: 12, seltags: jQuery("#currentTag").val(), req: "videos" },
		success: updateVideoPage,
		dataType: 'json'
	});
		
}

function updateTagPage(data){
	data = eval(data);
	jQuery('#currentpage').val(data['page']);
	jQuery('#pages').val(data['pages']);
	jQuery('.vidTag').remove();
	jQuery('#allTags').after(data['html']);
	jQuery('#page_count').html( data['page'] + '/' + data['pages']);
}

function updateVideoPage(data){
	data = eval(data);
	jQuery("#current_vid_page").val(data['page']);
	jQuery("#vid_page_count").val(data['pages']);
	jQuery('.vid-grid').html(data['html']);
	jQuery('#vid_pages').html( "Page " + data['page'] + ' of ' + data['pages']);
	jQuery("#vid-grid-container").unblock();
}

