/*!
 * Bloooming Shop Plugin v1.3 Wordpress
 * http://www.bloooming.com/
 *
 * Copyright 2010, Tina Coric
 * All rights reserved
 *
 * Date: Sun Apr 04 22:22:22 2010 -0500
 */


jQuery.fn.bloooming_shop = function(){

	/*********************get cart ****************************/
		$.ajax({
			type : 'GET',
			url : $('#pathtocart').attr('href'),
			success : function (html) {
				$('#items').html(html);

			}
		});

	if (jQuery().sSelect) {
		
		$('.productoptions').sSelect();
		
	}
	
	////// animate cart ///////
	
	$('.productoptions').change(function(){

	
		var selid = $(this).attr('id');
		
		var selprice = $('#'+selid+' option:selected').attr('rel');
		var prid = selid.substr(1);
		var currency = $('#'+prid).attr('rel');
		
		var phtml = currency+' '+selprice;
			
		$('#'+prid+' .price').html(phtml);
		$('#'+prid+' .price').attr('rel',selprice);


		if (Cufon) {
			Cufon.replace('.price');
		}
		
	});


	$('.showdetails').click(function() {
			$(this).next('.details').slideToggle(50);
	});
	

	////// animate cart ///////
	
	$('.addtocart').click(function(){
		the_td = $(this).parent().parent();
		
		product = [
			$("td:nth-child(0) p span",the_td).html(),
			$("td:nth-child(1) p span",the_td).html(),
			$("td:nth-child(2) p span",the_td).html().substr(1),
			$("td:nth-child(3) p span",the_td).html().substr(1)
		];
		
		pid = product[1];
		
		///animated shadow
		var pcont = the_td;
        var cart = $('#cart');		
        var shadow = $('#' + pid + '_shadow');
        $("#footercontent").append("<p>"+pcont.attr('id')+"</p>");
        $('body').prepend('<div class="shadow" id="'+pid+'_shadow"></div>');
          var shadow = $('#'+pcont.attr('id')+'_shadow');

	     shadow.width(pcont.css('width')).height(pcont.css('height')).css('top', pcont.offset().top).css('left', pcont.offset().left).css('opacity', 0.5).show();
    	 shadow.css('position', 'absolute');
		 
		 shadow.animate( {
		  		width: cart.innerWidth(), 
		  		height: cart.innerHeight(), 
		  		top: cart.offset().top, 
		  		left: cart.offset().left 
		  		}, { 
		  		duration: 200 
		  		} )
		    .animate({ 
		    	opacity: 0 
		    },
		    { 
		    duration: 700,
		    complete: function(){
		    	
		    	shadow.remove();
		    	
		    }
		    
		    });
		 
		    
	/// add to cart
	
		var name = product[1];
		var price = product[3];
		var option = '';
		option = $('#o'+pid+' option:selected').attr('value');
		var oprice = $('#o'+pid).attr('rel');
		
		var formData = 'productid=' + pid + '&productname='+ name + '&productprice=' + price + '&productoption=' + option + '&productoprice=' + oprice; 
		
		$.ajax({
			type : 'POST',
			url : $('#pathtocart').attr('href'),
			data : formData,
			success : function (html) {
				$('#items').html(html);
			}
		});
		

	});
	
	$('.order_notes').change(function(){
		var instructions = $(this).val();
		$.ajax({
			type : 'POST',
			data : '_order_instructions='+instructions
		});		
	});
	
	$('.removeitem').live('click', function() {		
		rid = $(this).attr('id');
		rop = $(this).attr('rel');

		var remData = 'removeproduct=' + rid + '&rop=' + rop; 
		
		$.ajax({
			type : 'POST',
			url : $('#pathtocart').attr('href'),
			data : remData,
			success : function (html) {
				$('#items').html(html);
			}
		});
		
	});
	
	$('#gotopayment').click(function(){
	
		var instructions = $('.order_notes').val();
		$.ajax({
			type : 'POST',
			data : '_order_instructions='+instructions,
			success : function (html) {
				$.ajax({
					type : 'GET',
					url : $('#gatewaypath').attr('href'),
					success : function (html) {
						window.location.href = html;
					}
				});
			}			
		});		
	});		

}

$(document).ready(function() {
	if ($('#ice-rad').html() == "Logged In") {
		$('table').each(function(index,TableElement){
			$('tr',TableElement).slice(2).each(function(index,RowElement){	
				$("td:last",RowElement).html('<span id="bp'+index+'" class="addtocart">add to cart</span>');
					
			});
		});
	}
});


		

