// JavaScript Document

function ajaxSetup()
{
    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;
  		    }
	    }
    }
    return xmlHttp;
}

function setupJqueryThumbs(){
	$(".callout_image3").hover(
		function(){
			$(this).children(':first-child').next().animate({height:"160px", "marginTop":"-165px"}, 400);
		},
		function(){
			$(this).children(':first-child').next().stop(true,false);
			$(this).children(':first-child').next().animate({height:"0px", "marginTop":"0px"}, 400);
		}
	);
	
}

function filterResults(input, value, pagetype){
	
	document.getElementById(input).value = value;
	document.getElementById("pageno").value = "1";
	var filter = document.getElementById("filter").value;
	var artist = document.getElementById("artist").value;
	var price = document.getElementById("price").value;
	var pagesize = document.getElementById("pagesize").value;
	
	$('#' + input + value).parent().children().children().removeClass("black");
	$('#' + input + value).children().addClass("black");
	$('.staff_pick').children().removeClass("black");
	
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("artists_main").innerHTML = xmlHttp.responseText;
			setupJqueryThumbs();
			Cufon.replace('.cufon,body');
		}
	}
	xmlHttp.open("GET","../ajax/" + pagetype + ".php?filter=" + filter + "&artist=" + artist + "&price=" + price + "&pagesize=" + pagesize + "&pageno=1", true);
	xmlHttp.send(null);
	
}

function backfilterResults(pageno, filter, pagesize, artist, price, type){
	document.getElementById("pageno").value = pageno;
	document.getElementById("filter").value = filter;
	document.getElementById("artist").value = artist;
	document.getElementById("price").value = price;
	document.getElementById("pagesize").value = pagesize;

	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("artists_main").innerHTML = xmlHttp.responseText;
			setupJqueryThumbs();
			$('#filter' + filter).children().addClass("black");
			$('#price' + price).children().addClass("black");
			Cufon.replace('.cufon,body');
		}
	}
	xmlHttp.open("GET","../ajax/" + type + ".php?filter=" + filter + "&artist=" + artist + "&price=" + price + "&pagesize=" + pagesize + "&pageno=" + pageno, true);
	xmlHttp.send(null);
	
}
function artdetails(url, type){
	document.getElementById("artform").action = ".." + url;
	document.getElementById("pagetype").value = type;
	document.getElementById("artform").submit();
}
function artdetails2(url, type){
	document.getElementById("artform2").action = ".." + url;
	document.getElementById("pagetype").value = type;
	document.getElementById("artform2").submit();
}


function paging(pageno, pagetype){
	document.getElementById("pageno").value = pageno;
	var pagesize = document.getElementById("pagesize").value;
	var filter = document.getElementById("filter").value;
	var artist = document.getElementById("artist").value;
	var price = document.getElementById("price").value;
	
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("artists_main").innerHTML = xmlHttp.responseText;
			setupJqueryThumbs();
			Cufon.replace('.cufon,body');
		}
	}
	xmlHttp.open("GET","../ajax/" + pagetype + ".php?filter=" + filter + "&artist=" + artist + "&price=" + price + "&pagesize=" + pagesize + "&pageno=" + pageno, true);
	xmlHttp.send(null);
	
}

function pagesize(size, pagetype){
	document.getElementById("pagesize").value = size;
	document.getElementById("pageno").value = "1";
	var filter = document.getElementById("filter").value;
	var artist = document.getElementById("artist").value;
	var price = document.getElementById("price").value;
	
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("artists_main").innerHTML = xmlHttp.responseText;
			setupJqueryThumbs();
			Cufon.replace('.cufon,body');
		}
	}
	xmlHttp.open("GET","../ajax/" + pagetype + ".php?filter=" + filter + "&artist=" + artist + "&price=" + price + "&pagesize=" + size + "&pageno=1", true);
	xmlHttp.send(null);
	
}

function searchkeyword(pagetype){
	document.getElementById("pageno").value = "1";
	document.getElementById("filter").value = "0";
	document.getElementById("artist").value = "0";
	document.getElementById("price").value = "0";
	var pagesize = document.getElementById("pagesize").value;
	
	$('#filter_links_holder').children().children().removeClass("black");
	document.getElementById("artist_dropdowns").selectedIndex = 0;
	$('#price_boundaries').children().children().removeClass("black");
	$('.staff_pick').children().removeClass("black");
	
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("artists_main").innerHTML = xmlHttp.responseText;
			setupJqueryThumbs();
			Cufon.replace('.cufon,body');
		}
	}
	xmlHttp.open("GET","../ajax/" + pagetype + ".php?keyword=" + document.getElementById("keyword_search").value + "&pagesize=" + pagesize + "&pageno=1", true);
	xmlHttp.send(null);
}

function staffPick(input, index, pagetype){
	document.getElementById("staffpick").value = input;
	document.getElementById("pageno").value = "1";
	document.getElementById("filter").value = "0";
	document.getElementById("artist").value = "0";
	document.getElementById("price").value = "0";
	var pagesize = document.getElementById("pagesize").value;
	
	$('#filter_links_holder').children().children().removeClass("black");
	document.getElementById("artist_dropdowns").selectedIndex = 0;
	$('#price_boundaries').children().children().removeClass("black");
	
	$('.staff_pick').children().removeClass("black");
	$('.staff_pick:eq(' + index + ')').children().addClass("black");
	
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("artists_main").innerHTML = xmlHttp.responseText;
			setupJqueryThumbs();
			Cufon.replace('.cufon,body');
		}
	}
	xmlHttp.open("GET","../ajax/" + pagetype + ".php?staffpick=" + input + "&pagesize=" + pagesize + "&pageno=1", true);
	xmlHttp.send(null);
	
}

function artistList(letter){
	
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("artistList").innerHTML = xmlHttp.responseText;
			Cufon.replace('.cufon,body');
		}
	}
	xmlHttp.open("GET","../ajax/artistlist.php?letter=" + letter, true);
	xmlHttp.send(null);
}

function newsletterEmail(){

	var name = document.getElementById("contact_name").value;
	var email = document.getElementById("contact_email").value;
	if (name == "Your Name..." && email.indexOf("@") < 1){
		alert("Please input your name and email address");	
	}
	else if (name == "Your Name..."){
		alert("Please input your name");
	}
	else if (email.indexOf("@") < 1){
		alert("Please input your email address");
	}
	else{
		var xmlHttp = ajaxSetup();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState == 4)   // 4 = Completed
			{
				document.getElementById("newsletter_text").innerHTML = String(xmlHttp.responseText).substr(1);
				Cufon.replace('.cufon,body');
				if (String(xmlHttp.responseText).substr(0,1) == 1){
					document.getElementById("surv").href = "../includes/survey.php?name=" + name + "&email=" + email;
					$("#surv").click();
				}
			}
		}
		xmlHttp.open("GET","../ajax/newsletter.php?name=" + name + "&email=" + email, true);
		xmlHttp.send(null);
	}
	
}


function searchkeyword2(){
	location.href="../browse/keyword/" + document.getElementById("keyword_search").value + "/";
}
function filterResults2(url){
	location.href="../browse/artist/" + url + "/";
}


/* ===================================== Home page ==================================== */

function searchkeyword3(){
	location.href="../browse/any/any/any/12/1/" + document.getElementById("browse_cell").value + "/";
}

function newsletterEmail2(){

	var name = document.getElementById("name_cell").value;
	var email = document.getElementById("email_cell").value;
	if (name == "Your Name..." && email.indexOf("@") < 1){
		alert("Please input your name and email address");	
	}
	else if (name == "Your Name..."){
		alert("Please input your name");
	}
	else if (email.indexOf("@") < 1){
		alert("Please input your email address");
	}
	else{
		var xmlHttp = ajaxSetup();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState == 4)   // 4 = Completed
			{
				document.getElementById("newsletter").innerHTML = String(xmlHttp.responseText).substr(1);
				Cufon.replace('.cufon,body');
				if (String(xmlHttp.responseText).substr(0,1) == 1){
					document.getElementById("surv").href = "../includes/survey.php?name=" + name + "&email=" + email;
					$("#surv").click();
				}
			}
		}
		xmlHttp.open("GET","../ajax/newsletter.php?name=" + name + "&email=" + email, true);
		xmlHttp.send(null);
	}
	
}


/* =============================== Contact page ====================================== */

function contactSubmit(){
	var name = document.getElementById("contact_us_name").value;
	var email = document.getElementById("contact_us_email").value;
	var message = document.getElementById("contact_us_message").value;
	if (name == "Your Name..." && email.indexOf("@") < 1){
		alert("Please input your name and email address");	
	}
	else if (name == "Your Name..."){
		alert("Please input your name");
	}
	else if (email.indexOf("@") < 1){
		alert("Please input your email address");
	}
	else{
		var xmlHttp = ajaxSetup();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState == 4)   // 4 = Completed
			{
				document.getElementById("contact_us_form").innerHTML = xmlHttp.responseText;
				Cufon.replace('.cufon,body');
			}
		}
		xmlHttp.open("GET","../ajax/contact.php?name=" + name + "&email=" + email + "&message=" + message, true);
		xmlHttp.send(null);
	}
}


/* ============================== Shopping Basket ===================================== */

function addToBasket(itemid){
	alert("This item has been added to your basket");
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			document.getElementById("basket").innerHTML = xmlHttp.responseText;
			$("#add_to_cart").removeAttr('onclick');
			Cufon.replace('.cufon,body');
		}
	}
	xmlHttp.open("GET","../ajax/homebasket.php?id=" + itemid, true);
	xmlHttp.send(null);
}
function addVoucherToBasket(){
	var vouchervalue = document.getElementById("vouchervalue").value;
	
	if (isNaN(vouchervalue)){
		alert ("Please input a proper monetary value");
	}
	else if (vouchervalue == ""){
		alert ("Please input a proper monetary value");
	}
	else{
		var xmlHttp = ajaxSetup();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState == 4)   // 4 = Completed
			{
				document.getElementById("basket").innerHTML = xmlHttp.responseText;
				Cufon.replace('.cufon,body');
				//$("#add_to_cart").removeAttr('onclick');
			}
		}
		xmlHttp.open("GET","../ajax/voucherbasket.php?id=" + vouchervalue, true);
		xmlHttp.send(null);
	}
}


function removeProduct(bid){
	if (confirm("Are you sure that you want to remove this item?"))
	{
		var xmlHttp = ajaxSetup();
		xmlHttp.onreadystatechange=function()
		{
			if(xmlHttp.readyState == 4)   // 4 = Completed
			{
				document.getElementById("about_the_gallery").innerHTML = xmlHttp.responseText;
				Cufon.replace('.cufon');
				document.getElementById("basketCount").value = parseInt(document.getElementById("basketCount").value) - 1;
				
				if (parseInt(document.getElementById("basketCount").value) == 0)
				{
					$("#proceedBtn").hide();
				}
				Cufon.replace('.cufon,body');
			}
		}
		xmlHttp.open("GET","../ajax/basket.php?type=remove&id=" + bid, true);
		xmlHttp.send(null);
	}
}

function updatedelivery(){
	var delivery = document.getElementById("deliverycharges");
	var deliveryid = delivery.options[delivery.selectedIndex].value;
		
	var xmlHttp = ajaxSetup();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState == 4)   // 4 = Completed
		{
			//location.reload(true);
			location.href = location.href;
		}
	}
	xmlHttp.open("GET","../ajax/deliverycost.php?id=" + deliveryid, true);
	xmlHttp.send(null);
}


/* navigation menu drop downs */
	$(document).ready(function(){	
		$(".home_submenu").hide();
		
		$('.menuitem').mouseover(function(){
			var index = $(this).attr("id");
			$("#sub" + index).show();
			$('.menu_link_left:eq(' + index + ')').addClass("link_left_image");
			$('.menu_link_right:eq(' + index + ')').addClass("link_right_image");
		});
	
		$('.menuitem').mouseout(function(){
			$('.home_submenu').hide();
			$('.menu_link_left').removeClass("link_left_image");
			$('.menu_link_right').removeClass("link_right_image");
		});
	});	

