window.mem=0;

jQuery(document).ready(function(){

	jQuery("#navigation > .page_item").hover(
      function () {
        jQuery(".page_item ul").addClass("menu-inactive");
        jQuery(this).children("ul").addClass("menu-active");
      }, 
      function () {
        jQuery(".page_item ul").removeClass("menu-inactive");
        jQuery(".page_item ul").removeClass("menu-active");
      }
    );


	/* Update Song Info on Home Page */
	if (jQuery(".radio-playing").length > 0) {
		var c=0;
		var t;
		timedCount();
	}

	if (jQuery("#idobi-radio-widget").length > 0) {
		var so = new SWFObject('/assets/idobi-radio-widget.swf','iradioplayer','360','140','8');
		so.addParam("wmode","opaque");
		so.addParam("allowScriptAccess", "always");
		so.addVariable("width", "300");
		so.addVariable("height", "150");
		so.write('idobi-radio-widget');
	}

	if (jQuery("body.player #radio-flash-player").length > 0) {
		var so = new SWFObject('/assets/idobi-radio-player.swf','iradioplayer','200','70','8');
		so.addParam("wmode","transparent");
		so.addParam("allowScriptAccess", "always");
		so.addVariable("width", "200");
		so.addVariable("height", "70");
		so.write('radio-flash-player');
	}

	/* Show amazon top 10 widget */
	if (jQuery("#chart-list-amazon").length > 0) {
		var so = new SWFObject('http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fidobinetwork-20%2F8014%2F2660d812-0549-41f4-9bdc-4587c59a4b7d&Operation=GetDisplayTemplate','Player_2660d812-0549-41f4-9bdc-4587c59a4b7d','234','60','9');
		so.addParam("allowScriptAccess", "always");
		so.addVariable("width", "234");
		so.addVariable("height", "60");
		so.write('chart-list-amazon');
		jQuery("#chart-list-amazon").css("display","block");
	}

	/* Show amazon top 10 widget, sidebar */
	if (jQuery("#chart-list-amazon-sidebar").length > 0) {
		var so = new SWFObject('http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fidobinetwork-20%2F8014%2F8034876e-336b-443a-9351-76c65d6f2ccc&Operation=GetDisplayTemplate','Player_8034876e-336b-443a-9351-76c65d6f2ccc','250','250','9');
		so.addParam("allowScriptAccess", "always");
		so.addVariable("width", "250");
		so.addVariable("height", "250");
		so.write('chart-list-amazon-sidebar');
		jQuery("#chart-list-amazon-sidebar").css("display","block");
	}

/*
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab" id="Player_8034876e-336b-443a-9351-76c65d6f2ccc"  width="250px" height="250px"> <param name="movie" value="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fidobinetwork-20%2F8014%2F8034876e-336b-443a-9351-76c65d6f2ccc&Operation=GetDisplayTemplate"><param name="quality" value="high"><param name="bgcolor" value="#FFFFFF"><param name="allowscriptaccess" value="always"><embed src="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fidobinetwork-20%2F8014%2F8034876e-336b-443a-9351-76c65d6f2ccc&Operation=GetDisplayTemplate" id="Player_8034876e-336b-443a-9351-76c65d6f2ccc" quality="high" bgcolor="#ffffff" name="Player_8034876e-336b-443a-9351-76c65d6f2ccc" allowscriptaccess="always"  type="application/x-shockwave-flash" align="middle" height="250px" width="250px"></embed></object> <noscript><a href="http://ws.amazon.com/widgets/q?ServiceVersion=20070822&MarketPlace=US&ID=V20070822%2FUS%2Fidobinetwork-20%2F8014%2F8034876e-336b-443a-9351-76c65d6f2ccc&Operation=NoScript">Amazon.com Widgets</a></noscript>
*/

	/* Show featured artists. Pause on hover, Resume on roll out */
	if (jQuery(".promo-artist").length > 0) {
		jQuery(".promo-artist").css("display","block");
		jQuery(".promo-artist").cycle({ 
			fx:			'scrollDown',  
			speed:		800, 
			timeout:	12000, 
			random:		1
		});
		jQuery(".promo-artist").hover(
			function () {
				jQuery(this).cycle("pause");
			}, 
			function () {
				jQuery(this).cycle("resume");
			}
		);
		jQuery(".promo-artist").fadeIn("fast");
	}

	/* Show featured articles. Pause on hover, Resume on roll out */
	if (jQuery(".featured-articles").length > 0) {
		jQuery(".featured-articles").cycle({ 
			fx:			'shuffle',  
			speed:		800, 
			timeout:	6000, 
			random:		1
		});
		jQuery(".featured-articles").hover(
			function () {
				jQuery(this).cycle("pause");
			}, 
			function () {
				jQuery(this).cycle("resume");
			}
		);
		jQuery(".featured-articles").fadeIn("fast");
	}

	/* Show Promos. Pause on hover, Resume on roll out */
	if (jQuery(".show-promos").length > 0) {
		jQuery(".show-promos").cycle({ 
			fx:			'scrollLeft',  
			speed:		800, 
			timeout:	5000, 
			random:		1
		});
		jQuery(".show-promos").hover(
			function () {
				jQuery(this).cycle("pause");
			}, 
			function () {
				jQuery(this).cycle("resume");
			}
		);
		jQuery(".show-promos").fadeIn("fast");
	}

	/* Intercept links and do somethign with them */

	jQuery(".snap_noshots").click(function () {
		return false;
	});

	jQuery(".tweets-item a").click(function () { 
		url = jQuery(this).attr("href"); 
		window.open(url,"twitter");
		return false;
	});

	jQuery(".radio-listen-type-web a").click(function () { 
		url = jQuery(this).attr("href"); 
		window.open(url,"idobiradioplayer","width=418,height=450,status=0,toolbar=0,location=0,resizable=0,menubar=0");
		return false;
	});

	jQuery(".page-item-52878 a").click(function () { 
		url = jQuery(this).attr("href"); 
		window.open(url,"idobiradioplayer","width=418,height=450,status=0,toolbar=0,location=0,resizable=0,menubar=0");
		return false;
	});

	jQuery(".chat-popout a").click(function () {
		url = jQuery(this).attr("href");
		window.open(url,"chat","width=310,height=400,status=0,toolbar=0,location=0,resizable=0,menubar=0");
		return false;
	});

	if (jQuery("#query").length > 0) {
		jQuery('#query').autocomplete({ 
			serviceUrl:'/services/request_query.php',
			minChars:3, 
			delimiter: /(,|;)\s*/, // regex or character
			maxHeight:400,
			width:300,
			deferRequestBy: 0 //miliseconds
		});
	}

	if (jQuery("#request-table").length > 0) {
		jQuery("#request-table").tablesorter(); 
	}

	/* Initiate Radio Song Ratings */
	initiateSongRating();

	setUpCalendarEvent();

});

function setUpCalendarEvent() {
	/* Intercept Calendar clicks */
	jQuery(".ics-calendar-event").click(function () { 
		text = jQuery(this).html().toLowerCase();
		a = 0;
		if (text.match("man of the hour") == "man of the hour") {
			a = 1;
			url = "http://idobi.com/radio/shows/man-of-the-hour/";
		}
		if (text.match("the gunz show") == "the gunz show") {
			a = 1;
			url = "http://idobi.com/radio/shows/the-gunz-show/";
		}
		if (text.match("bitch slap radio") == "bitch slap radio") {
			a = 1;
			url = "http://idobi.com/radio/shows/bitch-slap-radio/";
		}
		if (text.match("m radio") == "m radio") {
			a = 1;
			url = "http://idobi.com/radio/shows/sm-radio/";
		}
		if (text.match("altop20") == "altop20") {
			a = 1;
			url = "http://idobi.com/radio/shows/alternative-top-20/";
		}
		if (a) {
			window.open(url,"_parent");
		}
	});
}

function openLink(opt) {
	if (opt == "itunes") {
		url = jQuery(".radio-itunes a").attr("href"); 
	} else if (opt == "itms") {
		url = jQuery(".album-information a").attr("href"); 
	} else {
		url = jQuery(".radio-album a").attr("href"); 
	}
	window.open(url,"itunes_store");
}

/* ------------------------------------------------------------
+Current Songs Routines
------------------------------------------------------------ */

function playerReady(obj) {
  player = document.getElementsByName(obj.id)[0];
  player.addControllerListener('ITEM','playlistTracker');
};

function playlistTracker(obj) {
	var path = player.getPlaylist()[obj.index].link;

//	Update Title

	var vtitle	= player.getPlaylist()[obj.index].title;
	var vdate	= player.getPlaylist()[obj.index].tags;
	var vshow	= player.getPlaylist()[obj.index].author;
	var vdesc	= player.getPlaylist()[obj.index].description;

	var newtitle = vtitle + ' - <span class="date">' + vdate + '</span><br/><span class="info">' + vshow + ' - ' + vdesc +'</span>';

	jQuery('.entry-title').html(newtitle);

//	link = temp.split("?",2);
//	permalink = link[1];
	url = 'http://idobi.com/services/update-media-info.php?url='+path;
//	window.open(url);
	ajaxObjReplace(url,'#media-info');

//	'<h2 class="entry-title"></h2>'


}

function radioAlbumCycle() {

	if (jQuery(".radio-playing-1 .radio-album-cycle").length > 0) {
		jQuery(".radio-playing-1 .radio-album-cycle").before('<div id="album-nav">').cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout: 0, 
			pager:  '#album-nav' 
		});
		jQuery(".radio-playing-1 .radio-albums").fadeIn("fast");
	}

}

function ajaxRadioUpdate(opta,optb) {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var info = xmlHttp.responseText;
			jQuery(optb).html(info);
			radioAlbumCycle();
			initiateSongRating();
		}
	}

	xmlHttp.open("GET",opta + Math.random(),true);
	xmlHttp.send(null);

}

function ajaxObjReplace(opta,optb) {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var info = xmlHttp.responseText;
			jQuery(optb).replaceWith(info);
		}
	}

	xmlHttp.open("GET",opta + '&random=' + Math.random(),true);
	xmlHttp.send(null);

}

function ajaxRadioFunction(opta,optb) {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var change = xmlHttp.responseText;
			var response = change + "::" + window.mem;
			/*
				Store change.txt value in mem
				If value is different, update radio currently playing data
			*/
			if (change != window.mem) {
				ajaxRadioUpdate("/services/idobiradio_playing.txt?",".radio-update");
				window.mem = change;
			}
		}
	}

	xmlHttp.open("GET",opta + Math.random(),true);
	xmlHttp.send(null);

}

function timedCount() {

	/*
		Reduce server load by checking change.txt for update time
		instead of just loading the station currently playing time
	*/
	ajaxRadioFunction("/services/change.txt?","#debug");
	t=setTimeout("timedCount()",12000);
}

/* Song Rating Functions */

function initiateSongRating() {
	var rand = Math.random(10000);
	jQuery(".radio-playing-1 .rating-box .song-rated").load("/services/rating-info.php?rand="+rand, function(){
		$rating = jQuery(".radio-playing-1 .rating-box .song-rated").text().replace(/\n/g,'');
		if ($rating == 0) {
			setupRating();
		} else {
			displayRating($rating);
		}
 	});
}

function setupRating() {

	jQuery(".radio-playing-1 .rating-box").css( {display: "block"} );
	jQuery(".radio-playing-1 .rating-box ul").css( {display: "block"} );
	jQuery(".radio-playing-1 .rating-box .song-rated").css( {display: "none"} );
	jQuery(".radio-playing-1 .rate-1").hover(
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 -16px"} )
      }, 
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 0"} )
      }
    );
	jQuery(".radio-playing-1 .rate-2").hover(
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 -32px"} )
      }, 
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 0"} )
      }
    );
	jQuery(".radio-playing-1 .rate-3").hover(
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 -48px"} )
      }, 
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 0"} )
      }
    );
	jQuery(".radio-playing-1 .rate-4").hover(
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 -64px"} )
      }, 
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 0"} )
      }
    );
	jQuery(".radio-playing-1 .rate-5").hover(
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 -80px"} )
      }, 
      function () {
      	jQuery(".radio-playing-1 .rating-box ul").css( {backgroundPosition: "0 0"} )
      }
    );

}

function rateAction(opt) {

	var rand = Math.random(10000);
	jQuery(".radio-playing-1 .rating-box ul").css( {display: "none"} );
	jQuery(".radio-playing-1 .rating-box .song-rated").load("/services/rating-info.php?rand="+rand+"&rating="+opt, function(){
		$rating = jQuery(".radio-playing-1 .rating-box .song-rated").text().replace(/\n/g,'');;
		if ($rating == 0) {
			setupRating();
		} else {
			displayRating($rating);
		}
 	});

}

function displayRating($rating) {
	jQuery(".radio-playing-1 .rating-box").css( {display: "block"} );
	jQuery(".radio-playing-1 .rating-box ul").css( {display: "none"} );
	jQuery(".radio-playing-1 .rating-box .song-rated").css( {display: "block"} );
	$position = $rating * (-16);
	jQuery(".radio-playing-1 .rating-box .song-rated").css( {backgroundPosition: "0 "+$position+"px"} )
}

/* ------------------------------------------------------------
+Media Section Routines
------------------------------------------------------------ */
function mediaCategoryToggle(id,obj) {
	if (document.getElementById(id).style.display != "block") {
		jQuery('.media-category').hide('fast');
		jQuery('#'+id).toggle(400);
	}
	if (obj != "none") {
		jQuery('.media-category-nav a').removeClass("active");
		jQuery(obj).addClass("active");
	}
}

function mediaAction(id,obj) {
	jQuery('#media-action a').removeClass("active");
	if (document.getElementById(id).style.display != "block") {
		jQuery('.media-xtra').hide('fast');
		jQuery('#'+id).show(400);
		jQuery(obj).addClass("active");
	} else {
		jQuery('#'+id).hide(400);
	}
}

function mediaToggle(id) {
	var $sec = '#' + id + " ul";
	var $temp = '#' + id + " .media-section .media-section-select";
	if (jQuery($sec).css("display") == "block") {
		jQuery($temp).removeClass("active");
		jQuery($sec).hide(300);
	} else {
		jQuery($temp).addClass("active");
		jQuery($sec).show(300);
	}
}


function logStatsRefresh() {
	var URL = "/services/idobiradio-logs.php?t=1&r=" + Math.random(10000);
	jQuery("#stats-ajax").load(URL); 
}

function processLogRequest(obj) {
//	var URL = window.location.protocol + "//" + window.location.host + window.location.pathname;
	var URL = "/radio/logs/?";

	var year = document.getElementById('stats-input-year').value;
	var month = document.getElementById('stats-input-month').value;
	var day = document.getElementById('stats-input-day').value;
	var hour = document.getElementById('stats-input-hour').value;

	if (obj == 1) {
		URL = URL + "trend=" + year + "-" + month + "-" + day + " " + hour + ":00:00";
	} else if (obj == 0) {
		URL = URL + "d=" + year + "-" + month + "-" + day + " " + hour + ":00:00";
	}
	window.location.href = URL;
	
	return false;
}

function loginCI() {

	var username = document.getElementById('user_login').value;
	var modUser = username.toLowerCase();
	document.getElementById('user_login').value = modUser;

	return true;
}
