var id;
var debug = false;
var redir = false;

function updateTimeOut(){
	alert("Error loading the page. Please press 'ok' and refresh the page");	
}

function showBasket(){
	if(document.getElementById("prod")!= "undefined"){
		runAjax("showBasket");
	}
}

function addProduct(id,itterator){
	var qty = document.getElementById("qty[" + id + "]").value;
	if(qty <= 0 || qty > 20){
		alert("Please enter a quantity between 1 and 20");
		return 0;
	}
	
	//Check if lastSelected is chosen, if it is, we're on photographs
	var fGroup = $('#sel-group');
	var fSel = $('fieldset > [id*=div-] select');
	
	if(options.lenth != 0 && fGroup.length != 0){
		if(fGroup[0].selectedIndex == 0){
			alert("Please select a print type");
			return 0;
		}
		
		if(fSel[0].selectedIndex == 0){
			alert("Please select a print");
			return 0;
		}
	}
	
	var att = "";
	
	for(var i=0; i < itterator; i++){
		if(document.getElementById("attrib[" + id + "][" + i + "]")){
			att += document.getElementById("attrib[" + id + "][" + i + "]").options[document.getElementById("attrib[" + id + "][" + i + "]").selectedIndex].value + "|";
		}
	}

	runAjax("addProduct",id+","+att+","+qty);
}

function scroller(data){
	$('html, body').animate({scrollTop:0}, 'medium','linear',function () {
		 showMessage(data);
		 setTimeout(function() {
			$('#prod').fadeOut('slow', function(){
				$('#prod').fadeIn('slow');
			});}
		,1000);
	}); 	
}

function delProductWrapper(id,uid){
	if(confirm("Are you sure you want to delete this product from your basket?")){
		redir = true;
		delProduct(id,uid);	
	}
}

function delAllProductsWrapper(){
	if(confirm("Are you sure you want to delete all of the products in your basket?")){
		delAllProducts();	
	}
}

function delAllProducts(){
	runAjax("delAllProducts");	
}

function changeQty(id,uid){
	var qty = document.getElementById("qty[" + id + "][" + uid + "]").value;
	runAjax("changeQty",id + "," + uid + "," + qty);
}

function delProduct(id,uid){
	runAjax("delProduct",id+","+uid);
}

function switchPrice(dropdown,prod_id){
	var a = 0;
	var str;
	var varids="";
	while(str = document.getElementById("attrib["+prod_id+"]["+a+"]")){
		if(str){
			varids += str.options[str.selectedIndex].value + "|";
		}
		a++;
	}
	runAjax("getNewPrice",prod_id+","+varids);
}

// Admin

function loadCategories(id){
	runAjax("getProductCategories",id);
}

function addCat(id,cid){
	if(!id || id == 0) return false;
	runAjax("addProductToCategory",id+","+cid);	
}

function delCat(id){
	if(!id) return false;
	runAjax("delProductFromCategory",id);
}

function loadFeature(featid){
	if(featid)
		id = featid;
	setTimeout("showFeature()",100);
}

function showFeature(){
	runAjax("getFeatures",id);
}

function addFeature(id){
	runAjax("addFeature",id+","+document.getElementById("featureadd").value);
	setTimeout("showFeature()",100);		
}

function delFeature(id){
	runAjax("delFeature",id);
	loadFeature();
}

function showAttributes(){
	runAjax("getAttribute",id);
}

function addAttribute(){
	var av = document.getElementById("attrib_value");
	var an = document.getElementById("attrib_name");
	var sa_div = document.getElementById("attributes");
	var price = document.getElementById("pricemod");
	runAjax("addAttribute",id+","+av.options[av.selectedIndex].value+","+price.value+","+an.options[an.selectedIndex].value);
	setTimeout("showAttributes()",100);	
}

function move(dir,an_id,id,p_id){
	runAjax("moveAttribute",id+","+p_id+","+an_id+","+dir);
	setTimeout("showAttributes()",100);	
}

function moveName(dir,an_id){
	runAjax("moveAttributeName",dir+","+an_id+","+id);
	setTimeout("showAttributes()",100);	
}

function delAttribute(aid){
	runAjax("delAttribute",aid+","+id);
	setTimeout("showAttributes()",100);
}

function showMessage(data){
	if(data[1] == ": ") data[1] = "";
	$('#message').html("<a href='/home.php?/shop/checkout'>" + data[0] + "</a> : Added");
	$('#right > ul.message').fadeIn('slow', function(){
		setTimeout("removeMessage()",10000);
	});
}

function removeMessage(){
	$('#right > ul.message').fadeOut('slow');
}

//

function runAjax(func,funcargs){
	if(funcargs==null){
		funcargs = "";	
	}
	
	var handlerFunc = function(t) {
    	switch(func){
			case "countBasket":
				if(t != 0){
					$('#view > div').removeAttr("class");
					$('#basket > ul').addClass("items");
					$('#viewBasket').removeAttr("class");
				}else{
					$('#basket > ul').removeAttr("class");
					$('#view > div').addClass("hide");
					$('#viewBasket').addClass("hide");
				}
			break;
			case "showBasket":
				var data = t.split("\|\|\|");
				if(data[0] != "0"){
					$("#viewBasket").html(data[0]);
				}
				$("#prod").html(data[1]);
				runAjax("countBasket");
			break;
			case "addProduct":
				var args = funcargs.split(",");
				if(document.getElementById("product-" + args[0])){
					$('#product-' + args[0]).addClass("product-selected");
				}
				var prod = document.getElementById("prod");
				var data = t.split("\|");
				
				scroller(data);
				
				runAjax("countBasket");
				runAjax("showBasket");
			break;
			case "delProduct":
				var args = funcargs.split(",");
				if(document.getElementById("product-" + args[0])){
					$('#product-' + args[0]).removeClass("product-selected");
				}
				if(redir==true){
					window.location = "/home.php?/shop/checkout";
					redir = false;
				}
				runAjax("countBasket");
				runAjax("showBasket");
			break;
			case "getFeatures":
				var feature = document.getElementById("addfeatures");
				feature.innerHTML = t;
			break;
			case "getAttribute":
				var cont = document.getElementById("attributes");
				cont.innerHTML = t;
			break;
			case "getNewPrice":
				var strloc = t;
				var strlocarr = strloc.split("|");
				var cont = document.getElementById("price"+strlocarr[0]);
				cont.innerHTML = "&pound;"+strlocarr[1];
			break;
			case "delAllProducts":
				window.location = "/home.php?/shop/checkout";
			break;
			case "getProductCategories":
				$('#cat').html(t);
			break;
			case "addProductToCategory":
			case "delProductFromCategory":
				runAjax("getProductCategories",id);
			break;
			break;
		}
	}
	
	
	var i = Math.round(100000*Math.random());
	var pars = "";
	pars += '&func=' + func;
	pars += '&funcargs=' + funcargs;
    pars += '&val' + i;
	
	$.ajax({
		url: '/admin/shop/ajax.php',
		data: pars,
		type: 'post',
		success: handlerFunc,
		timeout: updateTimeOut
	});
}

function viewBasket(){
	$('#view').mouseover(function(e){
		$('#view').html('<a href="/home.php?/shop/checkout">VIEW</a> <p class="floatbox">I like to float</p>');
	});
}

var options = new Array;
var lastSelected = 0;
function changeGroup(sel){
	if(lastSelected != 0){
		$('#div-' + lastSelected).empty();
	}
	$('#div-' + sel.selectedIndex).html(options[sel.selectedIndex]);
	lastSelected = sel.selectedIndex;
}

function storeOptions(){
	var group = $('fieldset > [id*=div-]');
	if(group.length == 0){
		return false;
	}
	//remove html
	var a = 1;
	options[0] = "";
	$(group).each(function(){
		options[a] = document.getElementById('div-'+a).innerHTML;
		$(this).empty();
		a++;
	});
}

window.onload=function(){ 
	storeOptions();
}