$(document).ready(function(){

	$('.zoom').jqzoom();

	get_sizes();
	get_colours();

	$('#Size').live('change', get_colours );
	$('#Colour').live('change', get_sizes );

	//open sizing guide in pop-up
	$('.sizingguide').attr('target', '_blank');

	$('#BuyNowButton').click( function() {
		
		$.ajax({
			url: '/shoppingbag/add/'+$('#ProductID').val()+'/'+$('#Size').val()+'/'+$('#Colour').val(),
			success: function(r) {
				if( r == 'success' ) {
					window.location = '/shoppingbag';
				}
				else
					alert(r);
			}
		});

		return false;
	});

	$('#AddToShoppingBag').click( function() {
		$.ajax({
			url: '/shoppingbag/add/'+$('#ProductID').val()+'/'+$('#Size').val()+'/'+$('#Colour').val(),
			success: function(r) {
				if( r == 'success' ) {
					$('#BagCount').text( parseInt($('#BagCount').text()) + 1 );
					$.modal('<div style="text-align: center"><br /><strong>Item added to shopping bag.</strong><br /><br /><br /><a href="/shoppingbag">Click here to proceed to checkout.</a><br /><br />OR<br /><br /><a href="#" class="simplemodal-close">Click here to continue shopping.</a></div>');
				}
				else {
					//$.modal(r);
					alert(r);
				}
			}
		});

		return false;
	});


	/*
		the gallery
	*/
	var gallery = $('#Gallery li');
	var currently_showing = gallery.length - 1; //set to the last item, allows it to play nice with jqzoom

	gallery.eq(currently_showing).show();

	$('#Back').click( function() {

		gallery.stop(false, true);

		var show_next = currently_showing + 1;

		if( show_next == gallery.length )
			show_next = 0;

		gallery.eq(currently_showing).fadeOut();
		gallery.eq(show_next).fadeIn();

		currently_showing = show_next;
		
		return false;
	});


	$('#Next').click( function() {
		
		gallery.stop(false, true);
		
		var show_next = currently_showing - 1;

		if( show_next == -1 )
			show_next = gallery.length -1;

		gallery.eq(currently_showing).fadeOut();
		gallery.eq(show_next).fadeIn();

		currently_showing = show_next;
		
		return false;
	});






	function get_sizes() {
		
		//alert('size');
		
		$.ajax({
			url: '/product/size_options/'+$('#ProductID').val()+'/'+$('#Colour').val()+'/'+$('#Size').val(),
			success: function(html) {
				$('#Size').replaceWith(html);
				
				//if there's only one real option, choose it
				var children = $('#Size').children();
				if( children.size() == 2 ) {
					$('#Size').val( children.eq(1).val() );
				}

				ieWorkaround();

			}
		});
	}

	function get_colours() {
		
		//alert('colour');

		$.ajax({
			url: '/product/colour_options/'+$('#ProductID').val()+'/'+$('#Size').val()+'/'+$('#Colour').val(),
			success: function(html) {
				$('#Colour').replaceWith(html);
				
				var children = $('#Colour').children();
				if( children.size() == 2 ) {
					$('#Colour').val( children.eq(1).val() );
				}

				ieWorkaround();
				if( html=='' ) {
					$('#AddToShoppingBag').attr('disabled', 'disabled');
					$('#BuyNowButton').attr('disabled', 'disabled');
				}
			}
		});
	}

	//force disabled OPTIONs for buggy IE
	function ieWorkaround() {
		$('option[disabled]').css({'color': '#cccccc'});
	}

/*
		we could do all this stuff to, but stuff it, greying it out is enough :)
		
		$('select').change(function() {
			
			if(this.options[this.selectedIndex].disabled) {

				if(this.options.length == 0) {
					this.selectedIndex = -1;
				else
					this.selectedIndex--;
	  
			 $(this).trigger('change');
		   }

		 });

		 $('select').each(function(it) {
		 	if(this.options[this.selectedIndex].disabled)
				this.onchange();
		 });

	} //end ieWorkaround()
*/

});

