	$(function(){
		$(document).pngFix();
		//load all the pretty search stuff
	var otherDiv = '<label for=\"downloadssearch\" style=\"opacity: 0.999999;\"><div class=\"search-wrapper empty blurred\"><span class=\"left\"></span><input name=\"Keyword\" id=\"downloadssearch\" class=\"prettysearch\" type=\"text\"><div id=\"ajaxIndicator\" ><img src=\"images/ajax-loader.gif\" width=\"16\" height=\"16\" alt=\"Ajax Loader\"></div><span class=\"right\"></span><div class=\"reset\"></div></div></label>';
	var safariDiv = '<label for=\"downloadssearch\" style=\"display: block; \"><input type=\"search\" name=\"Keyword\" id=\"downloadssearch\" class=\"prettysearch\" results=\"5\" placeholder=\"Search for music...\" autosave=\"\"></label>';
if($.browser.safari){
	//alert("you're using safari");
	$("div#searchfield").html(safariDiv);
}else{
	$("div#searchfield").html(otherDiv);
}

// attach the click function of the nav buttons to the buttons
	$('img.anImage').click(function() {
	//	alert(this.id);
		$('#mainColumn').load('includes/'+this.id+'.php'); //load the correct page into div#layer3
		return false;
	});
//attach the same to the text in the footer
	$('li.navButtons').click(function() {
	//	alert(this.id);
		$('#mainColumn').load('includes/'+this.id+'.html'); //load the correct page into div#layer3
		return false;
	});
	ajaxIndicatorStop();
	searchable();
	pageLinks();
	//details();
	//load the pages into the divs, and make the connections to the relevant functions
	$('#innerCart').load('cart.php',function(){
		$("#cartTable").hide();
		$("#cartHeaderLabel").click(function(){
			$("#cartTable").toggle(function(){
				if($("#cartTable").is(":visible")){
					bestSellerHeight($("#cartTable").height());
				}
				});
			$("#cartSummary").toggle(function(){
				if($("#cartSummary").is(":visible")){
					bestSellerHeight($("#cartSummary").height());
					}
				});
		});
//		tb_ready();
		checkout();
		deleteMe();
	});
	$('#bestSellers').load('includes/bestSellers.php',function(){
		if($("#cartSummary").height()>0){
			var cartHeight = $("#cartSummary").height();
		}else{
			var cartHeight = 0;
		}
		bestSellerHeight(cartHeight);
		init_thickbox();
		});
	$('#leftColumn').load('includes/featuredSearch.php',function(){featured();});
	$('#mainColumn').load('includes/specials.php',function(){specialLink();});
});

function bestSellerHeight(cartHeight){
	var innerHeight = cartHeight + 40;
	var newCartHeight = cartHeight + 80;
	if(cartHeight == 0||cartHeight == null){
		$("#innerCart").height(0);
		$("#bestSellerList").height(495);
	}else{
		if(innerHeight >= 465 && $("#cartTable").is(":visible")){
			$("#bestSellerList").hide();
			$("#innerCart").height(455).css("overflow","auto");
		}else{
			$("#innerCart").height(newCartHeight);//.css("overflow","visible");
			$("#bestSellerList").height(455 - innerHeight).show();
		}
	}
}
function details(){
	$('p.detail').click(function() {
		alert("you clicked "+this.id);
	});
}

function toggleCart(){
	$("#cartHeaderLabel").click(function(){
		//check for the cart height to ensure correct display
		$("#cartSummary").toggle('slow',function(){
			if($("#cartSummary").is(":visible")){
				bestSellerHeight($("#cartSummary").height());
			}
			});
		$("#cartTable").toggle('slow',function(){
			if($("#cartTable").is(":visible")){
					bestSellerHeight($("#cartTable").height());
				}
				});
	});
}
function specialLink(){
	$("a.specialLink").click(function(){
		$("#mainColumn").load(this.href,function(){
			$("#returnButton").click(function(){
				$('#mainColumn').load('includes/specials.php',function(){
					specialLink();
					});	
			});
		});
		return false;
	})
}
function searchable(){
	$("form#search").submit(function(){
			var options = {
				target:   '#mainColumn', 
				beforeSubmit: searchValid, 
				success: function(){
					ajaxIndicatorStop();
					advancedSearch();
					tb_ready(),
					pageLinks();
					addMe();},
				semantic: true 
				};
			$("form#search").ajaxSubmit(options);
			return false;
	});
	
	$('#simpleSearch').click(function() {
		var options = {
			target:   '#mainColumn', 
			beforeSubmit: searchValid, 
			success: function(){
				ajaxIndicatorStop();
				advancedSearch();
				tb_ready(),
				pageLinks();
				addMe();},
			semantic: true 
			};
		$("form#search").ajaxSubmit(options);
		return false;
	});
	$('#advancedSearch').click(function() {
		$('#mainColumn').load('search.php',function(){
			advancedSearch();
			});
		return false;
	});
}

function advancedSearch(){
	$("#advSearch").submit(function(){
		var options = {
			target:   '#mainColumn',
			beforeSubmit: ajaxIndicatorStart,  
			success: function(){
				tb_ready(),
				pageLinks();
				addMe();
				},
			semantic: true 
			};
		$("form#advSearch").ajaxSubmit(options);
		return false;
	});
	$('#advSearchButton').click(function() {
		var options = {
			target:   '#mainColumn', 
			beforeSubmit: ajaxIndicatorStart, 
			success: function(){
				ajaxIndicatorStop();
				advancedSearch();
				tb_ready(),
				pageLinks();
				addMe();
				},
			semantic: true 
			};
		$("form#advSearch").ajaxSubmit(options);
		return false;
	});

}

function pageLinks(){
	
	$('li.aLink').click(function() {
		var options = {
			target:   '#mainColumn',  
			success: function(){
				tb_ready();
				pageLinks();
				addMe();
				},
			semantic: true 
			};
	$("form#"+this.id).ajaxSubmit(options);
	});
}

function featured(){
	$('li.featured').click(function() {
	var options = {
	target:   '#mainColumn',
	beforeSubmit: ajaxIndicatorStart,
	success: function(){
		ajaxIndicatorStop();
		tb_ready(); 
		pageLinks();
		addMe();},
	semantic: true 
	};
	$("form#"+this.id).ajaxSubmit(options);
	return false;
});
}
function addMe(str){
	$("p.addMe").click(function()
		{
		
		var theForm = 'form#'+ this.id;
		var options = {
		target:   '#innerCart', 
		success:    function() {
			$("#innerCart").show()
			$("#cartSummary").hide();
			bestSellerHeight($("#cartTable").height());
			toggleCart();
			deleteMe();
			checkout();
			},
			semantic: true 
				};
		//submit the form, using ajax
		$(theForm).ajaxSubmit(options);
		return false;
	}	
	);

	return false;
}

function detailAddMe(){
	$(".detailAdd").click(function()
		{
		tb_remove();
		//$("#bestSellers").hide('slow');
		var theForm = 'form#'+ this.id;
		var options = {
		target:   '#innerCart', 
		success:    function() {
			$("#innerCart").show()
			$("#cartSummary").hide();
			bestSellerHeight($("#cartTable").height());
			toggleCart();
			deleteMe();
			checkout();
			},
			semantic: true 
				};
		//submit the form, using ajax
		$(theForm).ajaxSubmit(options);
		return false;
		}	
	);
}

function deleteMe () {
	$("a.deleteButton").click(function()
	{  
		  var options = {
        target:   '#innerCart',    // target element to update
		//bind the "deletable" options to the cart when it has finished
		success:    function() {
			$("#cartSummary").hide('');
			bestSellerHeight($("#cartTable").height());
			toggleCart();
			deleteMe();
			//tb_ready();
			},
        semantic: true 
     };
		
		$('form#'+this.id).ajaxSubmit(options);
	return false;
	}
);
}
function checkout(){
	pleaseConfirm = false;
	$("#checkout").click(function(){
		$("#mainColumn").load("customerid.php",function(){
			if(!pleaseConfirm){
				$('#edit').hide();
				buttonNext();
			}else{
				changeButton();
				paymentDetails();
			}
			billingInfo();
		});
	});

}
function billingInfo(){
	if(!$('input#billingIndicator').is(":checked")){
		$('#billingInfo').hide();
	}
	$('input#billingIndicator').click(function(){
		if($('input#billingIndicator').is(":checked")){
			$('#billingInfo').slideDown();
		}else{
			$('#billingInfo').slideUp();
		}
		
	});
}
function buttonNext(){
	$(".aButton").click(function(){
		var thisID = this.id
			var theForm = 'form#infoform';
			var options = {
			target:   '#mainColumn', 
			beforeSubmit: errorCheckCustomerDetails,
			success:    function() {
				$('#edit').show();
				paymentDetails();
				billingInfo();
				changeButton();
				},
				semantic: true 
					};
			$(theForm).ajaxSubmit(options);
			return false;
	});
}
function changeButton(){
	$("#edit").click(function(){
		var thisID = this.id
			var theForm = 'form#infoform';
			var options = {
			target:   '#mainColumn', 
			beforeSubmit: errorCheckCustomerDetails,
			success:    function() {
				$('#edit').show();
				paymentDetails();
				billingInfo();
				},
				semantic: true 
					};
			$(theForm).ajaxSubmit(options);
			return false;
	});	
}
function paymentDetails(){
	$("#payment").click(function(){
		var thePaymentType = $("input[@name='paymentType']:checked").val();
		$("#mainColumn").load("paymentDetails.php",{paymentType: thePaymentType}, function(){
		//	$('#edit').hide();
		$(".aButton").click(function(){
			var thisID = this.id
				var theForm = 'form#confirmform';
				var options = {
				success: function(){ 
					hideCart();
					 },
					target:   '#mainColumn', 
					semantic: true 
						};
				$(theForm).ajaxSubmit(options);
				return false;
		});
		$("#confirmform").submit(function(){
			var thisID = this.id
				var theForm = 'form#confirmform';
				var options = {
				success: function(){ 
				hideCart();
				 },
				target:   '#mainColumn', 
					semantic: true 
						};
				$(theForm).ajaxSubmit(options);
				return false;
		});
		//	buttonNext();
		});
	});
}

function hideCart(){
//hides the cart and shows the best sellers
	$("#innerCart").hide();
	$("#bestSellerList").height(495).show();
}
function searchValid(){
	theVal = $("#downloadssearch").val();
	if(theVal==""){
		return false;
	}
	ajaxIndicatorStart();

}
function errorCheckCustomerDetails(formData, jqForm, options){
	$("#theResult").html("");
	//iterate through the inputs and mark which ones are "bad"
	$("#infoform :input").each(function(){
		$(this).removeClass("error");
	 	var thisInput = $(this).val().replace(" ", "");
		if ($(this).hasClass("required")){
		//	alert(this.id);
				if(isEmpty(this)){
					$(this).addClass("error");
					$("#theResult").html("Sorry, you have not completed a required field");
			}
		}
		if ($(this).hasClass("email")){
				if(!isEmail(this.value)){
					$(this).addClass("error");
					$("#theResult").html("Sorry, you have not completed a required field<br />Please ensure all required fields are completed including a valid email address");
				}
			}
		});
	var theResult = $("#theResult").html();
	if(theResult.length > 0){
		$("#theResult").fadeIn("fast");
		return false;
	}else{
		return true;
	}
}
function isEmail(str) 
    {
   var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
   return regex.test(str);
}

function isEmpty(item) 
    {
    if (item.value == "")
     return true;
     return false;
}
function init_thickbox(){
//$(document).ready(function(){  
	tb_init('a.thickbox2, area.thickbox2, input.thickbox2');
	//tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	
//});
}
function tb_ready(){
//$(document).ready(function(){  
	//tb_init('a.thickbox2, area.thickbox2, input.thickbox2');
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	
//});
}

function ajaxIndicatorStart(){
	$("#ajaxIndicator").show();
}
function ajaxIndicatorStop(){
	$("#ajaxIndicator").hide();
}