

// do this lot on dom load...
$(function()
{
	
	// top menu
	$('#navigationOne > li')
	.mouseover(function() 
	{
		var t = $(this).attr('title');
		$(".subMenu").not('#' + t).css('display', 'none');
		$('#' + t).css('display', 'block').mouseover(function()
		{
			$(this).css('display', 'block');
		})
		.mouseout(function()
		{
			//$(".subMenu").css('display', 'none');
			var t = $(this).attr('title');
			$(".subMenu").not('#' + t).css('display', 'none');
		});
	});
	
	
	
	
	// populate sub categories from main top-level category
	$('#category_id').change(function(event, selected_sub_id)
	{
		populateSubCategories(this.options[this.options.selectedIndex].value, selected_sub_id);
	});
	
	
	// populate sub sub categories from sub categories
	$('#sub_id').change(function(event, product_id, selectedColours, selected_sub_id, selected_subsub_id)
	{
		populateSubSubCategories(this.options[this.options.selectedIndex].value, selected_sub_id, selected_subsub_id);
		
		// we need to also populate colours when sub cat is selected
		populateSubCategoryColours(this.options[this.options.selectedIndex].value, selectedColours);
		
		// populate sizes when subcat is selected
		//populateSubCategorySizes(this.options[this.options.selectedIndex].value, product_id);
		
	});
	
	
	$('#subsub_id').change(function(event, selected_product_id)
	{
		populateProducts(this.options[this.options.selectedIndex].value, selected_product_id);
	});
	
	
	
	
	
	// populate ranges from brand dropdown
	$('#brand_id').change(function(event, selected_range_id)
	{
		populateRanges(this.options[this.options.selectedIndex].value, selected_range_id);
	});
	
	
	
	
	
	// do the layoutblock links (cat, sub, subsub etc)
	prepareLayoutBlockLinks();
	
	
	
	
	
	// ie fixes
	if ( $.browser.msie )
	{
		// padding in sheet is 22px 0 21px 21px. lose 1px off the top for ie.
		$('#breadcrumbWrap').css('padding-top', '21px');
		
		// productImage needs float:left in ie
		$('#productImage').css('float', 'left');

		// sidebar h3 - 1px too high...
		$('#sidebar h3').css('margin', '13px 0 14px 0');

		// shitty ie 7 positioning
		if ( $.browser.version > 6 )
		{
			$('#shopOnlineThing').css({'position':'absolute', 'top':'60px', 'left':'0px'});
			$('#header h1').text('');
			$('#headPhone').css({'position':'absolute', 'top':'80px', 'left':'15px'});
		}

		//  ie 6 
		if ( $.browser.version == 6 )
		{
			
			
			$('h1').css({'font-size':'0'});
			$('.layoutBlockWrap h3 a').css({'display':'block', 'padding':'0 0 0 0', 'margin':'0 0 0 0'});
			// padding:0 0 20px 0; margin:10px 0 0 0;

			$('#breadcrumbWrap').css('padding-top', '21px');
			
			//fix to center #wrapper
			var l = window.location.href;
			if ( l.indexOf('admin') == -1 )
			{
				$('#wrapper').css({'position':'relative', 'left':'35px'});
			}
			
		}

	}
	
	
	
	// safari fixes
	if ( $.browser.safari )
	{
		// padding in sheet is 22px 0 21px 21px. lose 1px off the top for ie.
		$('#breadcrumbWrap').css('padding-top', '20px');
		
		// safari 3.1 on macbook
		$('#navigationWrap').css({'width':'100%'});
		
	}
	
	
	
	
	
	
	
	// copy address to delivery address handler
	$('#copyAddressToDeliveryButton').click(function()
	{
		copyAddressToDelivery();
		// select top option in the previous address dropdown now
		$('option:first', 'select#delivery_address_id').attr('selected','selected');
	});
	
	
	
	// clickable h1
	$('#header h1').click(function()
	{
		window.location = '/';
	});
	
	
	
});

















// populate colours based on sub category
function populateSubCategoryColours(sub_id, selectedColours)
{
	var x = new Hijax(); x.setContainer( document.getElementById('sub_id'));
	x.setUrl("/p/populateSubCategoryColours.php?sub_id=" + sub_id + '&selectedColours=' + selectedColours);
	x.setCanvas(document.getElementById('colourSelectWrapper')); x.captureData();
}


// populate sizes based on sub category
function populateSubCategorySizes(sub_id, product_id)
{
	var x = new Hijax(); x.setContainer( document.getElementById('sub_id'));
	x.setUrl("/p/populateSubCategorySizes.php?sub_id=" + sub_id + "&product_id=" + product_id);
	x.setCanvas(document.getElementById('sizeSelectWrapper')); x.captureData();
}






// to populate sub categories
function populateSubCategories(category_id, selected_sub_id)
{
	var x = new Hijax(); x.setContainer( document.getElementById('category_id'));
	x.setUrl("/p/populateSubCategories.php?category_id=" + category_id + '&selected_sub_id=' + selected_sub_id);
	x.setCanvas(document.getElementById('subCategorySelectWrapper')); x.captureData();
}



// to populate sub sub categories
function populateSubSubCategories(sub_id, selected_sub_id, selected_subsub_id)
{
	var x = new Hijax(); x.setContainer( document.getElementById('sub_id'));
	x.setUrl("/p/populateSubSubCategories.php?sub_id=" + sub_id + '&selected_sub_id=' + selected_sub_id + '&selected_subsub_id=' + selected_subsub_id);
	x.setCanvas(document.getElementById('subSubCategorySelectWrapper')); x.captureData();
}


function populateProducts(subsub_id, selected_product_id)
{
	var x = new Hijax(); x.setContainer( document.getElementById('subsub_id'));
	x.setUrl("/p/populateProducts.php?subsub_id=" + subsub_id + '&selected_product_id=' + selected_product_id);
	x.setCanvas(document.getElementById('productSelectWrapper')); x.captureData();
}









// to populate ranges froma brand_id
function populateRanges(brand_id, selected_range_id)
{
	var x = new Hijax(); x.setContainer( document.getElementById('brand_id'));
	x.setUrl("/p/populateRanges.php?brand_id=" + brand_id + '&selected_range_id=' + selected_range_id);
	x.setCanvas(document.getElementById('rangeSelectWrapper')); x.captureData();
}











function AddProductSizeColourPriceRow()
{
	//var clonedRow = $('table#productSizeColourPriceTable tr:last').clone();
	//$('table#productSizeColourPriceTable').append(clonedRow);
	
	
	//$('table#productSizeColourPriceTable').append($('table#productSizeColourPriceTable tr:last').clone());
	$('table#sizeSelectWrapper').append($('table#sizeSelectWrapper tr:last').clone());
	// highlight the new row so we know which one is the clone...
	$('table#sizeSelectWrapper tr:last td:first strong').text('New Size');
	$('table#sizeSelectWrapper tr:last td strong').css('color', 'red');
	
	
}






function doSlideToggle(subcatClick, arrowId)
{
	$(subcatClick).slideToggle(function()
	{
		var s = $(arrowId).attr('src');
		if ( s == 'img/arrow_down.gif' )
		{
			$(arrowId).attr('src', 'img/arrow_right.gif');
		}
		else
		{
			$(arrowId).attr('src', 'img/arrow_down.gif');
		}
	});
}






// fucntion to prepare links based on dic.layoutBlock
function prepareLayoutBlockLinks()
{
	$('.layoutBlock').click(function()
	{
		window.location = $(this).attr('title');
	});
	
}







// populate delivery address from previous address list
function populateDeliveryAddress(whichAddress)
{
	$.getJSON('/p/getDeliveryAddress.php?address_id=' + whichAddress, function(data)
	{
		var theForm = $('#customerRegistrationForm'); 
		for (var i=0; i<data.length; i++) 
		{ 
		    for ( key in data[i] )
		    { 
		            var value = eval('data[i].' + key); // the cheek ;)
		            $('#' + key, theForm).attr({value:value}); 
		    } 
		}
	});
	// lush!!!
}








// copy address details into delivery address fields...
function copyAddressToDelivery()
{
	// rewrite this you lazy ****. 
	// note to self:
	// surely it took longer to type out that it would to write a nice loop (like above)
	$('#cust_del_title').attr('value', 		$('#cust_title').attr('value'));
	$('#cust_del_first_name').attr('value', $('#cust_first_name').attr('value'));
	$('#cust_del_last_name').attr('value', 	$('#cust_last_name').attr('value'));
	$('#cust_del_address1').attr('value', 	$('#cust_address1').attr('value'));
	$('#cust_del_address2').attr('value', 	$('#cust_address2').attr('value'));
	$('#cust_del_town').attr('value', 		$('#cust_town').attr('value'));
	$('#cust_del_county').attr('value', 	$('#cust_county').attr('value'));
	$('#cust_del_postcode').attr('value', 	$('#cust_postcode').attr('value'));
	$('#cust_del_country_id').attr('value', $('#cust_country_id').attr('value'));
	$('#cust_del_telephone').attr('value', 	$('#cust_telephone').attr('value'));
	$('#cust_del_mobile').attr('value', 	$('#cust_mobile').attr('value'));
	$('#cust_del_fax').attr('value', 		$('#cust_fax').attr('value'));
	$('#cust_del_email').attr('value', 		$('#cust_email').attr('value'));
}







