﻿	var context = window;
	context.player = null; 
	context.currentItem = ""; 
	
	
	$(window).load(function() {
		var g = $("#gear-rotator");
		if (g.length > 0) {
			g.load("/handlers/ajax.ashx/GetFeaturedGear", function() {
				
				$("#hot-gear-container").show();

				$(this).jCarouselLite({
				    btnNext: "#hot-gear-next",
				    btnPrev: "#hot-gear-prev",
				    auto: 5000,
				    visible: 1
				});

			});
		}
	});
	
	
	
	$(function() {
		
		$(".vote").click(function (e) {
			e.preventDefault();

			var href = $(this).attr("href");
			var id = $(this).attr("rel");
			
			$.get(href, function(html) {
				
				$("#modal-content").hide().html(html);
				
				$("#vote-yes", this.content).click(function(e) {
				
					$.getJSON("handlers/ajax.ashx/Vote?id="+ id, function(json) {
						if (json.result == "OK") {
							$.blockUI({ 
								message : "<div class='vote-box'><h2>Thank you for voting</h2><p><button id='vote-ok' class='button'><span>OK</span></button></p></div>"
							});
							$("#vote-ok").click($.unblockUI);
						}
					});
				
					return false;
				});		
				
				$("#vote-cancel", this.content).click(function(e) {
					$.unblockUI();
					return false;
				});
				
				$.blockUI({ 
					message : $("#modal-content")
				});
			});
			
			return false;
		});
		
		
		$(".playButton").click(function(e) {
			e.preventDefault();
			
			var url = "player.aspx";
			var id = $(this).attr("rel");
			if (id) url += "?id="+ id;

			var p = $("#player");
			
			if (p.length > 0) {
				p.hide(10, function() {
					p.remove();
					openPlayerWindow(url);
				});
			}
			else
				openPlayerWindow(url);
					
			return false;
        });

        $("#search-term").focus(function() {
            if ($(this).val() === "Enter City & State OR Zip")
                $(this).val('');
        });
            
        $("#search-term").blur(function() {
                if ($(this).val() === "")
                    $(this).val('Enter City & State OR Zip');
        });
	});
	
	function openPlayerWindow(url) {
		context = window.open(url, "playerWin", "width=265,height=130");
		if (!context)
			alert("It appears that your browser has disabled popup windows.\nPopup windows must be enabled to open the music player.");					
		else 
			context.focus();
	}
		
	function playerReady(thePlayer) {
		context.player = context.document[thePlayer.id]; 
		context.player.addControllerListener("ITEM","itemTracker");

	
		if (typeof context.bandID != "undefined")
		{
			context.currentItem = context.bandID;
			playBand(context.bandID);
		}
	}

	function itemTracker(obj) {
		if (typeof context.bandID != "undefined") return;
		
		var id = context.player.getPlaylist()[obj.index].description;
		
		if (context.currentItem != id)
		{
			var url = "handlers/ajax.ashx/GetBand?id="+ id;
			
			// Using the animate() method to achieve a delay
			// in the execution of the code inside the callback.
			// For some reason FW player wants to fire the
			// 'ITEM' event like 1 second too early and so the 
			// band info gets updated too quickly and looks funny.
			
			$("#trackinfo", context.document).animate({opacity: 1}, 700, function() {
				$.getJSON(url, function(data){
					if (data.result == "OK") {
						updatePlayerInfo(id, data);
					}
				});
			});
		}
		context.currentItem = id;
	};
	
	function removePlayer(context) {
		$("#player", context).remove();
		return true;
	}
	
	function createPlayer(context) {

		context.player = null; 
		context.currentItem = ""; 

		var so = new SWFObject("player.swf","ply","245","20","9","#FFFFFF");
		so.addParam("allowfullscreen","false");
		so.addParam("allowscriptaccess","always");
		so.addParam("wmode","transparent");
		so.addVariable("config", "player.config.xml");

		if (typeof context.bandID == "undefined")
		{
		    so.addVariable("file", "handlers/ajax.ashx/GetWinnerAudioUrl");
		}
		else 
			so.addVariable("repeat", "none");
			
		so.write($("#player-flash", context.document)[0]);
	}
	
	function playBand(id) {
		var url = "handlers/ajax.ashx/GetBand?id="+ id;
		$.getJSON(url, function(data){
			if (data.result == "OK") {
				updatePlayerInfo(id, data);
				context.player.sendEvent("LOAD", data.AudioUrl);
			}
		});
	};
	
	function updatePlayerInfo(id, data) {
		$("#trackinfo", context.document).show();
		$("#trackinfo #band-link", context.document).attr("href", "bom-profile.aspx?id="+ id);
		$("#trackinfo #band-photo", context.document).attr("src", data.PhotoUrl);
		$("#trackinfo #band-name", context.document).text(data.Name);
		$("#trackinfo #band-song", context.document).text(data.AudioTitle);
		$("#trackinfo #band-location", context.document).text(data.Location);
	}
