/* basket functions */
var basket;
var basketwindow;
var ajaxWait = '<table><tr><td><img src="images/ajax-loader-small.gif" alt="" /></td><td>Loading, please wait...</td></tr></table>';

function ShowBasket()
{
	/* ShowPopUpWindow('basket', basehref+'viewbasket.cfm', 635, 480, 'yes', 'no', 'yes'); */
	if(basketwindow){
		if(!basketwindow.closed) basketwindow.close()
	}

	l = ((screen.width / 2) - (635 / 2))
	t = ((screen.height / 2) - (480 / 2))

	winstring = "width=635, height=480,left=" + l + ",top=" + t +  ",status=yes,resizable=no,scrollbars=yes";

	basketwindow = window.open(basehref+'viewbasket.htm','basket',winstring);
	basketwindow.focus();

	return;
}

function OpenInPopup(url)
{
	ShowPopUpWindow("prpopup",basehref + url,870,650,"yes","yes","yes");
}


function getRadioButtonValue(radioButtonArray) {
	var value = '';
	
	if (!radioButtonArray.length) {
		// Only one radio button, so form.radiobuttonname returns a radiobutton element, not an array
		if (radioButtonArray.checked) {
			value = radioButtonArray.value;
		}
	}
	else {
		for (var i=0; i<radioButtonArray.length; i++) {
			if (radioButtonArray[i].checked) {
				value = radioButtonArray[i].value;
				break;
			}
		}
	}
	
	return value;
}


/* View basket scripts */

function viewTerms() {
	ShowTermsPopUp('Terms', basehref +  'terms.cfm', 670, 450, 'yes', 'no', 'yes');
}


function ShowTermsPopUp(Name, File, w, h, Status, Resize, Scroll){
	var t=0, l = 0, winstring = "";
	var infowindow;

	if(infowindow){
		if(!infowindow.closed) infowindow.close()
	}

	l = ((screen.width / 2) - (w / 2))
	t = ((screen.height / 2) - (h / 2))

	winstring = "width=" + w + ", height=" + h + ",left=" + l + ",top=" + t +  ",status=" + Status + ",resizable=" + Resize + ",scrollbars=" + Scroll;

	infowindow = window.open(File,Name,winstring);

	return;
}

function qtychange(el,i)
{
	//alert(el.options[el.selectedIndex].value)
	var q = parseInt(el.options[el.selectedIndex].value);
	//alert(q)

	qty[i] = q;
	el.value= q;
	return true;
}

function Checkout(count)
{
	var msg = "";

	update();
	document.frmBasket.SList.value = basket;

	msg += UpSell_Validate(document.frmBasket);

	if ("" == msg) {
		if (document.frmBasket.ShipCountry.selectedIndex == 0) {
			msg += "Please select a Country/Region";
		}
		if ("" == msg) {
			if( flgQuickOrder )
				document.frmBasket.action = securebasehref + "default.cfm/loadfile.BuyConf.cfm"
			else
				document.frmBasket.action = securebasehref + "default.cfm/loadfile.BuyBasket.cfm"
			
			document.frmBasket.submit();
		}
		else {
			alert(msg);
		}
	}
	else {
		alert(msg);
	}
}
/* End of viewbasket scripts */

function update()
{
	var count = $('#txtBasketCount').val();
	var n = 0;
	
	clearbasket();
	
	for(n = 0; n < count; n++)
	{
		if(qty[n]>0)
		{
			add2order(id[n],qty[n],col[n],descrip[n]);
		}
	}
}

function ClearBasket()
{

	if(confirm("Your Basket will be emptied."))
	{
		clearbasket();
		document.location.href = document.location.href;
	}
}

function applyoffercode()
{
	if(document.getElementById('txtOfferCode').value != '')
	{
		recalculate(1);
	}
	else
	{
		alert('Please enter an offer code.');
	}
}

function recalculate(count)
{
//	update();
	var basketForm = document.getElementById("frmBasket");
	basketForm.submit();
}

function remove(code)
{
	if(confirm("The item will be removed from your basket."))
	{
		var basketForm = document.getElementById("frmBasket");
		var itemId = '';
		
		$('#txtItemToRemove').val(code);
		
		// removeorder(code,qty,colour);
		basketForm.submit();
	}
}

function OpenViewBasket()
{
	var url = basehref + 'ViewBasket.htm';

	if( pageIndex > 0 )
		url += '/PreviousPage.' + pageIndex;
		
	document.location.href = url;
}

function RefreshBasket(Fin_Price)
{
	if(document.getElementById("Price"))
	{
		document.getElementById("Price").innerText = 'Fin_Price';
	}
}

function refreshViewBasket()
{
	if(basketwindow)
		basketwindow.document.location.href = basketwindow.document.location.href;

	return;
}

 // function SewCookie(s)
 // {
	 // var ExpDate = new Date();
	 // ExpDate.setTime(ExpDate.getTime() + 43200000);
	 // strcookie="BTSheets=" + escape(s) + ";path=/; expires=" + ExpDate.toGMTString();
	
	 // alert(strcookie);
	
	 // document.cookie = strcookie;
 // }

// function add2order(id,qty,col,descrip)
// {
	// if(basket != "") basket += ':';
	// basket += id+','+qty+','+col+','+descrip;
	// SewCookie(basket);
// }

function updateHeadingBasketTotal(numItems, totalAmount){
	$('#headerBasketItems').html(numItems);
	$('#headerBasketAmount').html(totalAmount);
}

function SubmitItem()
{
	var frm = document.frmQty;
	var qty = parseInt(frm.qty.options[frm.qty.selectedIndex].text, 10)
	var gridPricingId = $('#txtGridPricingID').val();
	var description = $('#txtDescription').val();
	var col = $('#ddlColour').val();
	var duoCol = $('#ddlDuoSelect').val();
	var msg = '';
	
	if( col === -1 )
		msg += '\nPlease select a colour!';
		
	if( $('#ddlDuoSelect').is(':visible') && duoCol === -1 )
		msg += '\nPlease selct a duo colour!';
		
	if(isNaN(qty) || qty < 0)
		msg == '\nQuantity Must be a positive whole number';
	else if( qty == 0 )
	{
		if(confirm("You have entered a quantity of 0, the item will not be added to your basket."))
			$('#dlgAddToBasket').dialog('close');
		return;
	}
	
	if( msg != '' ){
		alert(msg);
		return;
	}
	
	var id = $('#txtProductPriceId').val();
	var desc = $('#txtGridPricingID').val();

	if( $('#ddlDuoSelect').is(':visible')  )
		 col = col + '/' + duoCol;
	
	if( basket != '') 
		basket += ':';
	basket += id + ',' + qty + ',' + col + ','+ desc;
	
	// SewCookie(basket);
	
	// $('#dlgAddToBasket').dialog('open');
	
	url = 'BasketConfirmPopup.cfm?change=false&id=' + id + '&qty=' + qty + "&colour=" + col + '&desc=' + desc;
	
	$.post(url, function(data){
		$('#dlgAddToBasket').html(data);
	});	
	
	// document.location.href = "BasketConfirmPopup.cfm?change=false&id=#url.id#&gid=#url.gid#&qty=" + qty + "&colour=" + col;
}

// function removeorder(id,qty,colour)
// {
	// var tmpArray;
	// var item;
	// var found = false;

	// tmpArray = basket.split(':');
	// basket = "";
	
	// for( var i = 0; i < tmpArray.length; ++i )
	// {
		// item = tmpArray[i].split(',');
		// if( !found && item[0] == id && item[1] == qty && item[2] == colour )
		// {
			// found = true;
		// }
		// else {
			// if( i != 0 ) basket += ':';
			// basket += tmpArray[i];
		// }
	// }
	// SewCookie(basket);
// }

function AddToBasket(GridPricingID) /* ,PriceGridID,bloadindex) */
{
	var url = basketbasehref + 'AddToBasketPopup.cfm?gridpricingid=' + GridPricingID;
	
	$('#dlgAddToBasket').dialog('open');
	$('#dlgAddToBasket').html(ajaxWait);
	
	$.post(url, function(data){
		$('#dlgAddToBasket').html(data);
	});
}


// function clearbasket()
// {
	// basket = "";
	// SewCookie(basket);
// }


function OpenQtyPopup(id,PriceGridID,bloadindex)	
{
	AddToBasket(id,PriceGridID,bloadindex);
}
/* end of basket functions */


var dlgAddToBasketDialogOptions = {
    autoOpen: false,
    bgiframe: true,
    modal: true,
    resizable: false,
    width: 725,
    height: 'auto', // 380,
	open: function () {
                        $(this).parents(".ui-dialog:first").find("#dlgAddToBasket").addClass('.addToBasketDialogClass .ui-dialog.ui-dialog-content.addToBasketDialogClass2');
                    },
    maxHeight: 500,
	dialogClass: 'addToBasketDialogClass'
};

function dlgAddToBasketDialogOptions_Clicked(){

}

function NoneLink() {
	return;
}

function ShowPopUpWindow(Name, File, w, h, Status, Resize, Scroll){
	var t=0, l = 0, winstring = "";

	if(infowindow){
		if(!infowindow.closed) infowindow.close()
	}

	l = ((screen.width / 2) - (w / 2))
	t = ((screen.height / 2) - (h / 2))

	winstring = "width=" + w + ", height=" + h + ",left=" + l + ",top=" + t +  ",status=" + Status + ",resizable=" + Resize + ",scrollbars=" + Scroll;

	infowindow = window.open(File,Name,winstring);
	infowindow.focus();

	return;
}


function loadImg(path,id){
	var strImgName = 'swatchImg_'+ id

	document.getElementById(strImgName).src = path;
}

function openMenu_Click(){

		$("div#panel").animate({
			height: "550px" 
			/* height: "540px" */
		})
		.animate({
			height: "510px" 
			/* height: "500px" */
		}, "fast");
		
		$("div.panel_button").toggle();
		
		$(".panel_bot").css("background-image", "url(graphics/bot-curve.png)");
		
		return false;
	
}
			
$(document).ready(function(){
	
	$("div.panel_button").click(openMenu_Click);
	
	$("div#hide_button").click(function(){
		$("div#panel").animate({
			height: "0px"
		}, "fast", function() {
			$(".panel_bot").css("background-image", "")});
		
		return false;
	});	
	
	var large3dCarousel = {			
						xPos: 460,
						yPos: 60,
						/*
						altBox: $("#alt-text"),
						titleBox: $("#title-text"),
						*/
						mouseWheel: false,
						FPS:30,
						autoRotate: 'left',
						autoRotateDelay: 5000,
						speed:0.2,
						bringToFront:true,
						yRadius: -50,
						xRadius: 360,
						loadingDiv: $('.divCarouselLoading')
					};
			
	var small3dCarousel = {
		xPos: 245,
		yPos: 40,
		/*
		altBox: $("#alt-text"),
		titleBox: $("#title-text"),
		*/
		mouseWheel: false,
		FPS:30,
		autoRotate: 'left',
		autoRotateDelay: 2400,
		speed:0.2,
		bringToFront:true,
		yRadius: 40 
	}
	// $(".carouselSmall").css('width', '490px');
	// $(".carouselSmall").css('height', '210px');
	
	var largeFlatCarousel = {
		xPos: 350,
		yPos: 30,
		buttonLeft: $("#left-but"),
		buttonRight: $("#right-but"),
		altBox: $("#alt-text"),
		titleBox: $("#title-text"),
		mouseWheel: false,
		FPS:30,
		autoRotate: 'left',
		autoRotateDelay: 2400,
		speed:0.2,
		bringToFront:true,
		yRadius: -20
	}
	
	var smallFlatCarousel = {
					xPos: 245,
					yPos: 40,
					buttonLeft: $("#left-but"),
					buttonRight: $("#right-but"),
					altBox: $("#alt-text"),
					titleBox: $("#title-text"),
					mouseWheel: false,
					FPS: 30,  
					autoRotate: 'left',
					autoRotateDelay: 5000,
					speed: 0.2,
					bringToFront:true,
					yRadius: -30,
					xRadius: 140,
					loadingDiv: $('.divCarouselLoading')
				};
				
				
	$(".carouselSmall").css('width', '490px');
	$(".carouselSmall").css('height', '250px');

	// This initialises carousels on the container elements specified, in this case, carousel1.
	// $(".carouselSmall").CloudCarousel(small3dCarousel);
	$(".carouselSmall").CloudCarousel(smallFlatCarousel);
	$(".carouselLarge").CloudCarousel(large3dCarousel);
	
	$('.priceHeadingGroup').children('.silverheader').click(function(){
		$(this).next('.submenu').slideToggle();
		return false;
	});
	
	
//	 $('.priceHeadingGroup').accordion({ 
//		header: 'div.silverheader',
//		icons: { 'header': '.priceGridGroupAccordionIcon', 'headerSelected': '.priceGridGroupAccordionIcon' }
//	 });
	 
	 $('#dlgAddToBasket').dialog(dlgAddToBasketDialogOptions);

	 $('.languageHover').hover(function(){
		var imgPath = $(this).children('img').attr('src');
		imgPath = imgPath.replace(/.gif/, '-glow.gif');
		$(this).children('img').attr('src', imgPath);
	 },
	 function(){
		var imgPath = $(this).children('img').attr('src');
		imgPath = imgPath.replace(/-glow.gif/, '.gif');
		$(this).children('img').attr('src', imgPath);
	 });
});

