$(function() {

	var R_url = location.href;
	var R_prod = 0;
	
	var rating = 5;

        $('.deladdr2').click(function() {
                $('#deladdr').show("slow");
        });
        
        $('.deladdr').click(function() {
        	$('#deladdr').hide("slow");
        });
        
        $('.showOrder').click(function() {
                $('#basketStuff').show("slow");
        });

	$('#bask_cont').click(function() {
	
		addToBasket();
	
	});
	
	$('.AJAX_rating img').hover(function() {
	
		var tmp = parseInt($(this).attr('cap'));
		$('.AJAX_rating img').each(function() { 
			
			if(parseInt($(this).attr('cap')) <= tmp) { $(this).attr('src','/templates/products/images/star.jpg'); } else { $(this).attr('src','http://www.rainbowzebra.co.uk/templates/products/images/un_star.jpg'); }
		
		});
	
	},function() {
	
		$('.AJAX_rating img').each(function() { 
			
			if(parseInt($(this).attr('cap')) > rating) { $(this).attr('src','/templates/products/images/un_star.jpg'); } else { $(this).attr('src','http://www.rainbowzebra.co.uk/templates/products/images/star.jpg'); }
		
		});
	
	});
	
	$('.AJAX_rating img').click(function() {
	
		var tmp = parseInt($(this).attr('cap'));
		rating = tmp;
		$('#rating').attr('value',rating);
		$('.AJAX_rating img').each(function() { 
			
			if(parseInt($(this).attr('cap')) <= rating) { $(this).attr('src','/templates/products/images/star.jpg'); }
		
		});
	
	});
	

	$('.AJAX_err').dialog({
	
		modal:true,
		autoOpen:false,
		title:'Oh No!',
		buttons:{ 'Ok':function() { $(this).dialog('close'); $('.AJAX_attr').effect('pulsate'); } }
	
	});


	$('.delv_sub').click(function() {

            
		if($("#tandc:checked").val())
		{
			$('#deliv_form').submit();
		}
		else
		{
		
			alert("Please read and accept the terms and conditions before proceeding");
			$('#tandc').parent().effect('pulsate');
		
		}
	
	});
	
	$('.delv_sub2').click(function() {
	
		$('#deliv_form2').submit();
	
	});
	
	
	function displayAttrErr()
	{
	
		$('.AJAX_err').dialog('open');
	
	}
	
	function addToBasket()
	{
		var qty = $('.qty').html();
		var attStr = ""; var count = 0; var err = 0;
		$('.AJAX_attr').each(function() {
		  
		  	if(parseInt($(this).val()) == 0) { err = 1; displayAttrErr(); }
			attStr = (count == 0) ? $(this).attr('cap') + "=" + $(this).val() : attStr + "|" + $(this).attr('cap') + "=" + $(this).val();
			count ++;
			
		});
		var col = $('#COLOUR_chooser').val();
		if(err == 0)
		{
		
		
			var res = $.ajax({
		
				type:"POST",
				url:"/includes/js/php/index.php",
				data:"type=add_bask&id=" + R_prod + "&qty=" + qty + "&attr=" + attStr + "&col=" + col,
				async:false
		
			}).responseText;
			if(!res) { alert("OH NO! Something went wrong when adding this item to your basket!"); } else {
			
				$('.total_items').html(res);
				displayConfirmation();
		
			}
		}
	
	}
	
	
	function fetchIdprod()
    {
		
        var tmp = R_url.split("?");
        var tmp2 = tmp[1].split("&");
        for(var i = 0; i<tmp2.length; i++)
        {

            var tmp3 = tmp2[i].split("=");
            if(tmp3[0] == "id") { return tmp3[1]; }

        }
        return 0;

    }
	
	function init()
	{
		
		R_prod = fetchIdprod(); 
	
	}
	
	init();
	
	
	/******************************************/
	
	
	$('.del-bask').click(function() {
	
		var id = $(this).attr('cap');
		var res = $.ajax({
		
			type:"POST",
			url:"/includes/js/php/index.php",
			data:"type=bask_del&id=" + id,
			async:false,
			error:function(a,b,c)
			{
			
				alert(c);
			
			}
		
		}).responseText;
		$(this).parent().parent().fadeOut('slow');
                $('.update_bask').html(res);
	
	});
	
	
	/******************************************/
	
	$('#AJAX_added').hide();
	
	function displayConfirmation()
	{
	
		var pos = $('#bask_cont').offset();
		$('#AJAX_added').css('left',pos.left+117);
		$('#AJAX_added').css('top',pos.top-2);
		$('#AJAX_added').fadeIn('fast');
		$('#basket2').effect('pulsate');
		$('body').oneTime(3000,function() { $('#AJAX_added').fadeOut('slow'); });
	
	}
	
	

});
