$(document).ready(function () {

	/* Make sure to update */
	//var APP_WEBPATH = 'http://localhost/cobuspotgieter_online';
	//var APP_WEBPATH = 'http://www.engram.nu/cobuspotgieter_com';
	var APP_WEBPATH = 'http://www.cobuspotgieter.com';

	$("a[rel^='prettyPhoto']").prettyPhoto();


	/* Find correct menu option and mark it */
	var page = $('#menu-selected').text();
	$('#menu a#' + page).attr('class', 'selected').find('img').attr('src', APP_WEBPATH + '/public/images/menu_' + page + '_marked.png');

	/* Menu rollover */
	$('#menu a').not('.selected').hover(function() {		
		$(this).find('img').attr('src', APP_WEBPATH + '/public/images/menu_' + $(this).attr('id') + '_marked.png');
	}, function() { 
		$(this).find('img').attr('src', APP_WEBPATH + '/public/images/menu_' + $(this).attr('id') + '.png');
	});


	/* Find correct sub menu option and mark it */
	if($('#menu-upforgrabs').length > 0) {

		var page = $('#menu-upforgrabs-selected').text();
		$('#menu-upforgrabs a#' + page).attr('class', 'selected').find('img').attr('src', APP_WEBPATH + '/public/images/up_for_grabs_menu_' + page + '_marked.png');
	
		/* Menu rollover */
		$('#menu-upforgrabs a').not('.selected').hover(function() {
			$(this).find('img').attr('src', APP_WEBPATH + '/public/images/up_for_grabs_menu_' + $(this).attr('id') + '_lined.png');
		}, function() { 
			$(this).find('img').attr('src', APP_WEBPATH + '/public/images/up_for_grabs_menu_' + $(this).attr('id') + '.png');
		});
	}
	
	$('.image-switch').click(function() { 
		var image = $(this).attr('href');
		$('#image-switch-box').html('<img src="' + image + '" style="max-width: 320px; max-height: 240px;"/>');
		return false;
	});
	
	$('.email-to-friend').click(function() {
		var id = $(this).attr('href');	
 		$('#' + id).toggle();
 		return false;
	});

	if($("#shipping").length > 0 ) {
		$('#shipping').attr('disabled', 'disabled');
		$('#shipping option:first').text('Add your shipping details first.');
	}

	$('#shipping').change(function () {
		shipping_changed();
	});

	$('#customer-country').change(function () {
		fetch_shipping();
		var value = $(this).val();
		if(value == 'USC') {
			$('#customer-state').val('California').attr('READONLY', 'READONLY');
			$('#tax-div').show();
			$('#handling-div').hide();
		} else {
			if($('#customer-state').val() == 'California')
				$('#customer-state').val('');
			$('#customer-state').removeAttr('READONLY');
			$('#tax-div').hide();	
			$('#handling-div').show();		
		}
	});
	
	function fetch_shipping() {
		$("#customer-country option:selected").each(function () {
    	var text = $(this).text();
    	var value = $(this).val();
     	if(text != 'Country...') {
	     	$.post(APP_WEBPATH + '/application/pages/cart/_shipping.php', { country: value },
	     		function(data) {
	     			$('#shipping').html('');
	     			$('#shipping').append('<option value="0">Select your shipping</option>');
	     			$('#shipping').removeAttr('disabled');
	     			shipping_changed();
	     			data = data.split('::');
	     			for(var i = 0; i < data.length; i++)
	     				$('#shipping').append('<option>' + data[i] + '</option>');
	     		}, 'text');
	   	} else {
	   		$('#shipping').attr('disabled', 'disabled');
	   		$('#shipping').html('');
				$('#shipping').append('<option>Add your shipping details first.</option>');	
	   	}
    });
  }

	function shipping_changed() {
		$("#shipping option:selected").each(function () {
    	var text = $(this).text();
     	values = text.split("$");
     	if($(this).val() == 0)
     		values[1] = 0;
     	var total_cost = $("#cart_items").val();
     	$("#cart_total").text(Math.round((parseFloat(total_cost) + parseFloat(values[1]))* 100)/100);
    });		
	}

	$('#contact-option').change(function() {
		$("#contact-option option:selected").each(function () {
			var val = $(this).val();
			if(val == '5') {
				$('#display-order-number-field').show();
			} else {
				$('#display-order-number-field').hide();
			}
		});
		
	});
	
	 $("input[type=file]").filestyle({ 
     image: APP_WEBPATH + "/public/images/button_browse.png",
     imageheight : 30,
     imagewidth : 130,
     width : 243
 	});
 	
 	$('#button-submit').attr('disabled', 'disabled').attr('style', 'border: 0');

	$('#agree').change(function() {
		if($(this).is(':checked')) {
			$('#button-submit').attr('disabled', '').attr('style', 'border: 0');
		}		
	});
	
	$('#display_promo_link').click(function() {
		$('#display_promo').toggle();
		return false;
	});
	
	
});