if (typeof(window["console"]) == "undefined") { console = {}; console.log = function(a) {}; }

var ty = {};
ty.currentIndex;
ty.rotTimeout;
ty.rotTimer = 5000;
var $rotSlides;
var $rotThumbs;

var login = true; 

jQuery(document).ready(function($) {
		
	// Nav items
	$('#nav li').hover(function(){
		var $this = $(this);
		$this.find('ul').css('display', 'block');
	}, function(){
		var $this = $(this);
		$this.find('ul').css('display', 'none');
	});
	
	// Remove the noscript content from DOM
	$('#nojs').remove();	


	// Show hotlist and songs with edit system attached
//	$('#songs').radiomatic( { json: '/app/top-songs.asp?callback=?', view: '#viewTopSongs', form: '#formTopSongs', login: true, callback: 'fixBottomAlignment()' } );
//	$('#hotlist').radiomatic( {	json: '/app/hot-list.asp?callback=?', view: '#viewHotlist', form: '#formHotlist', login: true, callback: 'fixBottomAlignment()' } );

	if($('#songs').length>0){
		$.getJSON('/app/top-songs.asp?callback=?', { }, function(data) {
			var stuff = $('#viewTopSongs').tmpl(data).appendTo('#songs');
		});
	}
	
	if($('#hotlist').length>0){
		$.getJSON('/app/hot-list.asp?callback=?', { }, function(data) {
			var stuff = $('#viewHotlist').tmpl(data).appendTo('#hotlist');
		});
	}




	
	// Show prayers
	if($('#prayer').length>0){
		$.getJSON('/app/prayers.asp?callback=?', { }, function(data) {
			console.log("prayer");
			$('#prayerTemplate').tmpl(data).appendTo('#prayer');
		});
	}
	
	// Show ads
	$.getJSON('/app/ads.asp?callback=?', { }, function(data) {
		$('#viewAd').tmpl(data[0]).appendTo('#ad1');
		$('#viewAd').tmpl(data[1]).appendTo('#ad2');
		$('#viewAd').tmpl(data[2]).appendTo('#ad3');
		$('#viewAd').tmpl(data[3]).appendTo('#ad4');
	});
	
	// Show events
	if($('#calendar').length>0){
		$.getJSON('/app/station-events.asp?callback=?', { }, function(data) {
			$('#stationEventsTemplate').tmpl(data, {
				prettyDate: function(d) {
					var dt = new Date(d);
					return (dt.getMonth() + 1) + "/" + (dt.getDate());
				}
			}).appendTo('#calendar');
		});
	}

	// Show spotlight
	if($('#spotlight').length>0){
		$.getJSON('/app/spotlight.asp?callback=?', { }, function(data) {
			var stuff = $('#spotlightTemplate').tmpl(data).appendTo('#spotlight');
		});
	}

	// Show listener stories
	if($('#stories').length>0){
		$.getJSON('/app/stories.asp?callback=?', { }, function(data) {
			$('#storyTemplate').tmpl(data).appendTo('#stories');
		});
	}

	// Build rotator
	if($('#slides').length>0){
		$.getJSON('/app/features.asp?callback=?', { }, function(data) {
			$('#viewRotator').tmpl(data).appendTo('#slides');
			$('#viewRotatorThumbnails').tmpl(data).appendTo('#featureThumbs');
			
			var activeSlide = 0;
			
			// Now that images are loaded, we need to animate things...
			$rotSlides = $('#slides li');
			$rotThumbs = $('#featureThumbs').find('img');
			
			
			var $thumbs = $('#featureThumbs').find('img');
			
			$rotThumbs.hover(
				function(){
					$(this).css('border-color', 'red');
				},
				function(){
					$(this).css('border-color', 'green');
				}
			);
			
			$rotSlides.hover(
				function(e){
					clearTimeout(ty.rotTimeout);
				},
				function(e){
					ty.rotTimeout = setTimeout("rotate()",ty.rotTimer);
				}
			);
			
			$thumbs.click(function(){
				// Thumbnail click handler to display correct image
				clearTimeout(ty.rotTimeout);
				ty.currentIndex = $(this).index()
				$rotSlides.hide();
				$rotSlides.eq(ty.currentIndex).show();
				ty.rotTimeout = setTimeout("rotate()",ty.rotTimer);
			});
	
			// Position Things			
			var $featureThumbs = $('#featureThumbs');
			var thumbCount = $featureThumbs.find('img').length;
			var featureWidth = 404;
			var thumbWidth = 40; // with margin, etc.
			var thumbMargin = 5;
			
			var posLeft = ((featureWidth - (thumbWidth * thumbCount)) / 2) - (thumbMargin / 2);
			posLeft += "px";
			$('#featureThumbs').css('left', posLeft);
			
			ty.currentIndex = 0;
			ty.rotTimeout = setTimeout("rotate()",ty.rotTimer);
		});
	}
});




/**********************************************
**  FUNCTIONS HERE
**********************************************/

function fixBottomAlignment(){
	try {
		var $l = $('#bottomContainer');
		var $r = $('#bottomSidebarContainer');
		
		var posBottom = $r.offset().bottom;
		var newBottom = $l.offset().bottom;
		
		// See if the left is shorter than the right
		if(newBottom < posBottom){
			var h = $l.height();
			$l.css('height', h + (posBottom - newBottom));		
			
		}
		$l.css('margin-bottom', '20px');
	}
	catch(err){
	}
}

function rotate(currentIndex){
	$rotSlides.hide();
	ty.currentIndex++;
	if(ty.currentIndex >= $rotThumbs.length){ ty.currentIndex = 0; }
	$rotSlides.eq(ty.currentIndex).show();
	ty.rotTimeout = setTimeout('rotate()', ty.rotTimer);
}
	
function trimLength( data, length ) {
	if(data.length < length){
		return data;
	}
	else {
    	return data.substring(0, length) + '...';
	}
}

	





















	
	
	
	
	
	
	
	
	
	

function trimLengthExpand( data, length ) {
	if(data.length < length){
		return data;
	}
	else {
    	return data.substring(0, length) + '<span class="viewAll">... <a href="#">View all</a></span><span class="remainingText">' + data.substring(length) + '</span>';
	}
}

function ShowPlayer() {
	openPlayer();
	//window.open("/Player/","MediaPlayer","width=600,height=330,left=50,top=50,border=0,toolbar=no,menubar=no,status=no,scrollbars=no,resizable=no");
}

function ShowOldPlayer() {
	window.open("/Player/","MediaPlayer","width=440,height=218,left=50,top=50,border=0,statusbar=no");
}

function openPlayer(){
	mywindow = window.open ("http://myradiomatic.com/player/star933/player.htm","rmPlayer","location=no,status=no,toolbar=no,menubar=no,resizeable=no,scrollbars=no,width=320,height=480");
//	mywindow.moveTo(20,20);
} 

// For backwards compatibility...remove if not needed.
function toggle(elementId){
	if(document.getElementById){
		var obj = document.getElementById(elementId);
		obj.style.display = obj.style.display;
		obj.style.display = (obj.style.display == "block") ? "none" : "block";
	}
}

function hide(elementId){
	if(document.getElementById){
		var obj = document.getElementById(elementId);
		obj.style.display = "none";
	}
}

function show(elementId){
	if(document.getElementById){
		var obj = document.getElementById(elementId);
		obj.style.display = "block";
	}
}









// Google Analytics Code Here:

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-22577861-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();




















