//required for IE ajax caching - effing Microsmut
$.ajaxSetup({
    cache: false
});

var link = base_url+"records/"; 

// cufon magic
Cufon.replace('.arialblack');

$(document).ready(function(){
						   
	$("#searchField").autocomplete(base_url+'records/ajaxSearch/');
	
	$('#searchField').result(function(event, data, formatted) {
		/*$("#result").html( !data ? "No match!" : "Selected: " + formatted);*/
		var release = formatted.match(/\x28[A-za-z0-9-]+\x29/ig);
		release = release[0].replace(/[\x28\x29]/g, "");
		window.location = base_url+'records/releases/'+release;
	});
	
	$("#esc").click(function() {
		if ($("#extendedSearchContainer").is(":hidden")) {
			$("#extendedSearchContainer").slideDown("slow");
		} else {
			$("#extendedSearchContainer").hide();
		}
	});
	
	$("#winZipOpen").click(function() {
		$("#macZip").hide();
		if ($("#winZip").is(":hidden")) {
			$("#winZip").slideDown("slow");
		} else {
			$("#winZip").hide();
		}
	});
	
	$("#macZipOpen").click(function() {
		$("#winZip").hide();
		if ($("#macZip").is(":hidden")) {
			$("#macZip").slideDown("slow");
		} else {
			$("#macZip").hide();
		}
	});
	
	$("table.products form").submit(function() {
		$("#cartInner").html('Updating...'); 
		var id = $(this).find('input[name=item_number]').val();  
		var qty = $(this).find('input[name=quantity]').val();  
		//alert('ID:' + id + '\n\rQTY:' + qty);  
		$.post(link + "cartAdd", { item_number: id, quantity: qty, ajax: '1' }, function(data){ 
			//alert('Sent to ' + link	+ 'cartAdd: ID:' + id + ' and QTY: ' + qty);						 
			if(data == 'true'){
				$.get(link + "cartShow", function(cart){ 
					//alert('ID:' + id + '\n\rQTY:' + qty + '\n\rCart:' + cart);  
					$("#cartInner").html(cart); 
				}); 		 
			} else {
				alert("Product does not exist");
			} 
		});  
		return false;
	});  
	
	$(".emptyButton").live("click", function(){
		$.get(link + "cartEmpty", function(){
			$.get(link + "cartShow", function(cart){
				$("#cartInner").html(cart);
			});
		});
		
		return false;
	});
	
	$(".checkoutButton").live("click", function(){
		top.location.href = link + "orderCheckout";		
		return false;
	});
	
		//Adjust panel height
	$.fn.adjustPanel = function(){ 
		$(this).find("ul, .subpanel").css({ 'height' : 'auto'}); //Reset subpanel and ul height
		
		var windowHeight = $(window).height(); //Get the height of the browser viewport
		var panelsub = $(this).find(".subpanel").height(); //Get the height of subpanel	
		var panelAdjust = windowHeight - 100; //Viewport height - 100px (Sets max height of subpanel)
		var ulAdjust =  panelAdjust - 25; //Calculate ul size after adjusting sub-panel (27px is the height of the base panel)
		
		if ( panelsub >= panelAdjust ) {	 //If subpanel is taller than max height...
			$(this).find(".subpanel").css({ 'height' : panelAdjust }); //Adjust subpanel to max height
			$(this).find("ul").css({ 'height' : ulAdjust}); //Adjust subpanel ul to new size
		}
		else if ( panelsub < panelAdjust ) { //If subpanel is smaller than max height...
			$(this).find("ul").css({ 'height' : 'auto'}); //Set subpanel ul to auto (default size)
		}
	};
	
	//Execute function on load
	//$("#chatpanel").adjustPanel(); //Run the adjustPanel function on #chatpanel
	$("#playerPanel").adjustPanel(); //Run the adjustPanel function on #playerPanel
	
	//Each time the viewport is adjusted/resized, execute the function
	$(window).resize(function () { 
		//$("#chatpanel").adjustPanel();
		$("#playerPanel").adjustPanel();
	});
	
	//Click event on Chat Panel + Alert Panel	
	$("#playerPanel a:first").click(function() { //If clicked on the first link of #chatpanel and #playerPanel...
		if($(this).next(".subpanel").is(':visible')){ //If subpanel is already active...
			$(this).next(".subpanel").hide(); //Hide active subpanel
			$("#footpanel li a").removeClass('active'); //Remove active class on the subpanel trigger
		} else { //if subpanel is not active...
			//$(".subpanel").hide(); //Hide all subpanels
			$(this).next(".subpanel").toggle(); //Toggle the subpanel to make active
			//$("#footpanel li a").removeClass('active'); //Remove active class on all subpanel trigger
			$(this).toggleClass('active'); //Toggle the active class on the subpanel trigger
		}
		return false; //Prevent browser jump to link anchor
	});
	
	//Click event outside of subpanel
	$(document).click(function() { //Click anywhere and...
		$(".subpanel").hide(); //hide subpanel
		$("#footpanel li a").removeClass('active'); //remove active class on subpanel trigger
	});
	$('.subpanel ul').click(function(e) { 
		e.stopPropagation(); //Prevents the subpanel ul from closing on click
	});
	
	//Delete icons on Alert Panel
	$("#playerPanel li").hover(function() {
		$(this).find("a.delete").css({'visibility': 'visible'}); //Show delete icon on hover
	},function() {
		$(this).find("a.delete").css({'visibility': 'hidden'}); //Hide delete icon on hover out
	});
	
	var CssToAdd = new Object();
	
	$.fn.getTwitter = function(options) {

		$.fn.getTwitter.defaults = {
			userName: null,
			numTweets: 5,
			showTimestamp: false,
			loaded: function(){} 
		};

		var o = $.extend({}, $.fn.getTwitter.defaults, options);

		return this.each(function() {
			var c = $(this);

			// add heading to container element
			if (o.showHeading) {
				c.append("<h2>"+o.headingText+"</h2>");
			}

			// add twitter list to container element
			var twitterListHTML = "<ul id=\"twitter_update_list\"><li></li></ul>";
			c.append(twitterListHTML);

			var tl = $("#twitter_update_list");

			// hide twitter list
			tl.hide();

			// show container element
			c.show();

			$.getScript("http://twitter.com/javascripts/blogger.js");
			$.getScript("http://twitter.com/statuses/user_timeline/"+o.userName+".json?callback=twitterCallback2&count="+o.numTweets, function() {

				//remove timestamp and move to title of list item
				if (!o.showTimestamp) {
					tl.find("li").each(function() {
						var timestampHTML = $(this).children("a");
						var timestamp = timestampHTML.html();
						timestampHTML.remove();
						$(this).attr("title", timestamp);
						//Add Target blank
						$(this).children('span').children('a').attr('target','_blank');
					});
				}
					
				//Show and callback time
				tl.show();
				o.loaded.call(this);
			});
		});
	};
	
	//TWITTER
	
	totalT = 0;
	
	$("#newsContent").getTwitter({
		userName: "sonicunyon",
		numTweets: 10,
		loaded: function(){
			
			//Apply cufon
			//Cufon.refresh('#twitter_update_list li');
			
			$('#newsContent p.loading').remove();
			
			$('#newsContent ul li').css('background-image','url('+base_url+'img/iconTwitter.png)');
			
			//$('#newsContent').jScrollPane();
			
		}
	});
	
});

function goOver(button) {
	button.style.border = "1px solid #fff";
}
function goOut(button) {
	button.style.border = "1px solid #000";
}

function getStoreOrders(storeID) {
	if ($("#orderDiv").is(":hidden")) {
	} else {
		$("#orderDiv").hide();
		$("#orderDiv").html('');
	}
	
	$("#orderSelectDiv").html('<img src="'+base_url+'img/ajax-loader.gif" alt="loading" />'); 
		//alert('Store ID:' + storeID);  
		$.post(link + "ajaxGetOrders", { store_id: storeID, ajax: '1' }, function(data){ 
			if(data == 'true'){
				$.get(link + "orderSelectShow/"+storeID, function(order){ 
					//alert('ID:' + id + '\n\rQTY:' + qty + '\n\rCart:' + cart);  
					$("#orderSelectDiv").html(order); 
					if ($("#orderSelectDiv").is(":hidden")) {
						$("#orderSelectDiv").slideDown("slow");
					}
				}); 		 
			} else {
				alert("No orders for this store exist");
				$("#orderSelectDiv").html('');
			} 
		});  
		return false;
	}
	
	function getOrder(orderID) {
		$("#orderDiv").html('<img src="'+base_url+'img/ajax-loader.gif" alt="loading" />'); 
		$("#orderDiv").show();
		$.post(link + "ajaxShowOrder", { order_id: orderID, ajax: '1' }, function(data){ 
			if(data == 'true'){
				$.get(link + "orderShow/"+orderID, function(order){ 
					//alert('ID:' + id + '\n\rQTY:' + qty + '\n\rCart:' + cart);  
					$("#orderDiv").html(order); 
					if ($("#orderDiv").is(":hidden")) {
						$("#orderDiv").slideDown("slow");
					}
				}); 		 
			} else {
				alert("No orders exist");
				$("#orderDiv").html('');
			} 
		});  
		return false;
	}
