/*  $Id$
 *  
 *  This file is part of the Jamiedia Toolkit.
 *  Copyright 2007/2008, Jamiedia Ltd., http://www.jamiedia.co.uk
 *  
 *  This file may not be used or (re)distributed for any other
 *  purposes than a commercial deployment by Jamiedia of a system
 *  based on the Jamiedia Toolkit. No modifications may be made to
 *  this file by anyone, except for individuals working for Jamiedia Ltd.
 *
 *  File description:
 *
 */ 

// Print document function
var printPage = function() {
	window.print();
	return false;
}

// Bookmark the current page
var bookmarkPage = function(_title) {
	var url = window.location;
	var title = (typeof(_title) == "undefined" ? document.title : _title);
	
	var ua=navigator.userAgent.toLowerCase();
    var isSafari = (ua.indexOf('webkit') != -1);
	
	if (window.sidebar) {
		// firefox
		window.sidebar.addPanel(title, url, "");
	}
	else if (isSafari) {
		// Safari
		alert('Press CTRL+D to bookmark this site.');
	}
	else if (window.opera) {
		// opera
		alert('Press CTRL+D to bookmark this site.');
	} 
	else if (document.all) {
		// ie
		window.external.AddFavorite(url, title);
	}
	
	return false;
}


$(document).ready(function() {
	var sizes = $('#select-sizes');
	var colours = $('#select-colours');
	
	// Function to update the colour select box
	var updateColourBox = function() {
		if (colours.find('option').size()) {
			var options = '<option value="-1">Select a colour...</option>';
			var sizeID = sizes.val();
			
			colours.removeAttr('disabled');
				
			for (var i in stockData[sizeID]) {
				var disabled = (stockData[sizeID][i].stock > 0 ? '' : ' disabled="disabled" class="disabled"');
				options += '<option value="'+stockData[sizeID][i].id+'"'+disabled+'>'+stockData[sizeID][i].name+'</option>';
			}
			colours.html(options);
		}
		
		if (sizes.size()) {
			var sizeID = sizes.val();
			
			$.each(sizes.find('option'), function() {
				var el = $(this);
				var sizeID = el.val();
				var hasStock = false;

				for (var i in stockData[sizeID]) {
					if (stockData[sizeID][i].stock > 0) {
						hasStock = true;
						break;
					}
				}
				
				if (sizeID != -1 && !hasStock) {
					el.attr('disabled', 'disabled');
					el.addClass('disabled');
				}
			});
		}
	}

	// Bind change event to update function
	sizes.change(updateColourBox);
	
	// Run update function one time
	updateColourBox();
	
	// Bind the submit event
	$('#product-form').submit(function() {
		var sizeID = sizes.val();
		var colourID = colours.val();
		
		var sizeDisabled = sizes.children('option:selected').hasClass('disabled');
		var colourDisabled = colours.children('option:selected').hasClass('disabled');
		
		if (sizeID == -1 || sizeDisabled) {
			alert('Please select a size.');
			return false;
		} else if (colourID == -1 || colourDisabled) {
			alert('Please select a colour');
			return false;
		}
		
		return true;
	});
	
	// Bind all selects to throw an error if a disabled element is/gets selected
	$('#product-details select').change(function() {
		var option = $(this).children('option:selected');

		if (option.hasClass('disabled'))
		{
			alert('You can not select this option.');d
			$(this).parent().blur();
		}
	});
	
	// Link all checkboxes to their option selects and initially disable them
	$('#product-accessories input[type="checkbox"]').change(function() {
		var accessoryID = this.id.substring(10);
		
		if ($(this).attr('checked')) {
			$('#accessoryoptions-'+accessoryID).removeAttr('disabled');
		}
		else {
			$('#accessoryoptions-'+accessoryID).attr('disabled', 'disabled');
		}
	}).filter(':disabled').each(function() {
		var accessoryID = this.id.substring(10);
		$('#accessoryoptions-'+accessoryID).attr('disabled', 'disabled');
	});
    
    // Bind alternate images
    $('#alternate-images a').click(function() {
        var bigImage = $('div.product-image img');
        var currentImage = $(this);
        var allImages = $('#alternate-images');
        
        // Remove currently active image
        allImages.find('.active').removeClass('active');
        
        // Swap images
        bigImage.attr('src', currentImage.attr('href'));

        // Add 'active' border to currently active thumbnail
        currentImage.addClass('active');
        
        return false; 
    });
});

