// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

$(document).ready( function(){
	itemLists();
	externalLinks();
	
	var u = new initEqualBoxes();
	
	/* Basket */

	// setup the basket with callbacks
	window.mySlotBasket = new MyslotBasket({
	    onAddItem: function(item, basket) {
	        renderBasket(basket);
	    },
	    onRemoveItem: function(item, basket) {
	        renderBasket(basket);
	    },
	    onClear: function(basket) {
	        renderBasket(basket);
	    }
	});
	// init will load items from cookie
	mySlotBasket.init();
});

itemLists = function() {
	var self = this;
	$(".item li").hover(
	function() {
		$(this).addClass("hover");
	},
	function() {
		$(this).removeClass("hover");
	}).click(
	function(event) {
		if(/child/.test($(this)[0].className)) {
			self.sendToPlayer($(this));
			return false;
		}
		if(event.target.tagName != "A" && $("a", $(this)).length > 0) document.location = $("a", $(this)).attr("href");
	});
}

initEqualBoxes = function(root) {
	var self = this;
	if(!root) root = $(document);
	this.setHeight = function(obj) {
		var h = 0;
		obj.each(function() { h = Math.max(h, $(this).height()); });
		obj.css({ minHeight: h + "px" });
		// compensate for stubborn IE6
		if(window.isIE6) {
			obj.css({ height: h + "px" });
		}
	}
	$(".equal", root).each(function() {
		self.setHeight($(this).children());
	});	
}

externalLinks = function() {
	var self = this;
	$("a[rel='external']").click(function() { 
		window.open(this.href);
		return false;
	});
	$("a[rel='popup']").click(function() { 
		var attr = "width=1024,height=850,location=yes"
		var title = this.title.replace(" ", "_");
		window.open(this.href,title,attr);
		return false;
	});
}

thisMovie = function (movieName) {
     if (navigator.appName.indexOf("Microsoft") != -1) {
         return window[movieName];
     } else {
         return document[movieName];
     }
 }

sendToPlayer = function (obj) {
	var movieName = "mediaplayer";
	var itemID = ($("a", obj).attr("href")).split("/").pop();
	
	id = parseFloat(itemID)
	thisMovie(movieName).sendIDtoFlash(id);
}

// little script to do Google Analytics calls from Flash
function trackPlayer(permalink) {	
	pageTracker._trackPageview(permalink);
	return true;
}


// to update the visual basket container
function renderBasket(basket) {
    var items = basket.getItems();
    if (items.length == 0) {
        
    } else {
		$basket = $('.basket');
        $('span', $basket).text(items.length);
		$basket.addClass('highlighted').pulse({
			speed: 250,    
			backgroundColors: ['#ff0000','#8888888'],
			duration: 2000
		});
		
    }
}

	
function addToMySlotFromFlash(id, title) {
  window.mySlotBasket.addItem(id, title);
}