/*!
 * Bloooming Shop Plugin v1.2
 * 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 : '/modules/cart/minicart.php',
			success : function (html) {
				$('#items').html(html);
				$('#gotopayment').html('Checkout with '+ $('.gateway').attr('rel'));
			}
		});*/


	/*********************format products ****************************/
	
	
	$('.productoptions').change(function(){
										 
		var currency = $(this).parents('.product').attr('alt');
		var CurPrice = $(this).parents('.product').find('.product_price').val();
		CurPrice = parseFloat(CurPrice);
		var OptPrice = 0.00;
		var selid;
		$(this).parents('.product').find('.productoptions').each(function(){
				selid = $(this).attr('id');
				OptPrice = $('#'+selid+' option:selected').attr('rel');
				//alert(OptPrice);
				CurPrice += parseFloat(OptPrice);				
		});
		
		var phtml = CurPrice.toFixed(2);
		$(this).parents('.product').find('.sel_price').html(phtml);
		//$('.sel_price').html();

		
		if (Cufon) {
			Cufon.replace('.sel_price');
		}
	
	});


	$('.showdetails').click(function() {
			//$(this).next('.details').slideToggle(50);
			$(this).parents('.productForm').find('.details').slideToggle(50);
	});
	
	//Animate cart Event
	 $(".event_btn").click(function(){						
		if ($('.eventsoption').length > 0){
			var option_selected = 0;
			$('.eventsoption').each(function(){
					selid = $(this).attr('id');
					if($('#'+selid+' option:selected').val() != 0){
						option_selected++;
					}
			});
			if(option_selected <= 0){
				$('.eventsoption').css('border','2px solid #c9091d');
				alert('Please select an option');
				return false;
			}
		}	
									
		pid = $(this).attr('id');
	///animated shadow
		var pcont = $('#' + pid);
        var cart = $('#cart');		
        var shadow = $('#' + pid + '_shadow');
        
        $('body').prepend('<div class="shadow" id="'+pcont.attr('id')+'_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: 300 
		  		} )
		    .animate({ 
		    	opacity: 0 
		    },
		    { 
		    duration: 100,
		    complete: function(){
		    	
		    	shadow.remove();
		    	
		    }
		    
		    });
	
			$.post(  
                "/modules/cart/minicart.php",  
                $("#sel_prd").serialize(),  
               function(data){  
                   $('#items').html(data);
               }  
           );
			
     }); 
	
	
	////// animate cart Product ///////
	
	$('.addtocart').click(function(){

		pid = $(this).attr('id');

		var selid;
		var OptSelected;
		var mandError = '';
		$(this).parents('.product').find('.productoptions').each(function(){
			selid = $(this).attr('id');
			OptSelected = $('#'+selid+' option:selected').val();
			mandatory = $(this).attr('title');
			if(mandatory != '' && OptSelected <= 0){
				mandError = mandError + mandatory + ', '; 
			}
		});
		
		if(mandError != ''){
			mandError = mandError.slice(0, -2);
			alert("Please Select "+mandError);
			return false;			
		}

		///animated shadow
		var pcont = $('#' + pid + '_product');
        var cart = $('#cart');		
        var shadow = $('#' + pid + '_shadow');
        
        $('body').prepend('<div class="shadow" id="'+pcont.attr('id')+'_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: 300 
		  		} )
		    .animate({ 
		    	opacity: 0 
		    },
		    { 
		    duration: 100,
		    complete: function(){
		    	
		    	shadow.remove();
		    	
		    }
		    
		    });
		 
		    
	/// add to cart
		$.post(  
                "/modules/cart/minicart.php",  
                $(this).parents('.product').find('.productForm').serialize(),  
               function(data){  
                   $('#items').html(data);
				   		var remData = "product_info=1&type=product&prdId=" + pid;
		
						$.ajax({
							type : 'POST',
							url : '/modules/cart/minicart.php',
							data : remData,
							success : function (html) {
								$('#'+ pid + '_added').html(html);
								if (Cufon) {
									Cufon.replace('.addCount');
								}
							}
						});
               }  
           );

		

	});
	
	$('.removeitem').live('click', function() {		
		rid = $(this).attr('id');
		rop = $(this).attr('rel');
		type = $(this).attr('name');
		var remData = 'remove=' + rid + '&rop=' + rop + '&type=' + type; 
		
		$.ajax({
			type : 'POST',
			url : '/modules/cart/minicart.php',
			data : remData,
			success : function (html) {
				$('#items').html(html);
			//	alert('thx');
			}
		});
		
	});
	
	$('#gotopayment').click(function(){
			
		$.ajax({
			type : 'GET',
			url : 'lib/checkout.php',
			success : function (html) {
				window.location.href = html;
			}
		});
		
	});	
	
	$('.eventsoption').change(function(){
		var CurPrice = parseFloat($('.event_price').val());
		var OptPrice = 0.00;
		var selid;
		$('.eventsoption').each(function(){
				selid = $(this).attr('id');
				OptPrice = $('#'+selid+' option:selected').attr('rel');
				//alert(OptPrice);
				CurPrice += parseFloat(OptPrice);				
		});
		
		$('.sel_price').html(CurPrice.toFixed(2));
			
	});
	

	$('.eventsprodoption').change(function(){
		var CurPrice = $(this).parents('.productForm').find('.product_price').val();								
		CurPrice = parseFloat(CurPrice);
		var OptPrice = 0.00;
		var selid;
		//alert(CurPrice);
		$(this).parents('.productForm').find('.eventsprodoption').each(function(){
				selid = $(this).attr('id');				
				OptPrice = $('#'+selid+' option:selected').attr('rel');
				//alert(OptPrice);
				CurPrice += parseFloat(OptPrice);				
		});
		$(this).parents('.productForm').find('.prdsel_price').html(CurPrice.toFixed(2));
			
	});

	$('.eventspopprodoption').change(function(){
		var CurPrice = $(this).parents('.productRow').find('.product_price').val();								
		CurPrice = parseFloat(CurPrice);
		var OptPrice = 0.00;
		var selid;
		//alert(CurPrice);
		$(this).parents('.productRow').find('.eventspopprodoption').each(function(){
				selid = $(this).attr('id');				
				OptPrice = $('#'+selid+' option:selected').attr('rel');
				//alert(OptPrice);
				CurPrice += parseFloat(OptPrice);				
		});
		var qty = $(this).parents('.productRow').find('.eventsprodqty').val();
		CurPrice = CurPrice * qty;
		$(this).parents('.productRow').find('.prdsel_price').html(CurPrice.toFixed(2));
			
	});

	$('.eventsprodqty').change(function(){
		var CurPrice = $(this).parents('.productRow').find('.product_price').val();								
		CurPrice = parseFloat(CurPrice);
		var OptPrice = 0.00;
		var selid;
		//alert(CurPrice);
		$(this).parents('.productRow').find('.eventspopprodoption').each(function(){
				selid = $(this).attr('id');				
				OptPrice = $('#'+selid+' option:selected').attr('rel');
				//alert(OptPrice);
				CurPrice += parseFloat(OptPrice);				
		});
		var qty = $(this).parents('.productRow').find('.eventsprodqty').val();
		CurPrice = CurPrice * qty;
		$(this).parents('.productRow').find('.prdsel_price').html(CurPrice.toFixed(2));
			
	});

	////// animate cart Relative Products ///////
	
	$('.addrel_product').click(function(){

		$('.productForm').each(function(){
				fid = $(this).attr('id');								
				pid = $('#'+fid).find('.pid').val();
		});
		
		///animated shadow
		/*var pcont = $('#' + pid);
        var cart = $('#cart');		
        var shadow = $('#' + pid + '_shadow');
        
        $('body').prepend('<div class="shadow" id="'+pcont.attr('id')+'_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: 300 
		  		} )
		    .animate({ 
		    	opacity: 0 
		    },
		    { 
		    duration: 100,
		    complete: function(){
		    	
		    	shadow.remove();
		    	
		    }
		    
		    });
		 */
		    
	/// add to cart
		$('.productForm').each(function(){
			fid = $(this).attr('id');								
			pid = $('#'+fid).find('.pid').val();

			if($('#'+fid).find('.eventsrelprod').is(':checked')){
				$.post(  
						"/modules/cart/minicart.php",  
						$(this).serialize(),  
					   function(data){  
						   $('#items').html(data);
					   }  
				   );
			}
			
		});
		

	});

    //when the checkbox options are changed on the show.php page 
	$('.productlistoptions').change(function(){
										 
		//var currency = $(this).parents('.product').attr('alt');
		var CurPrice = $('#product_price').val();
		CurPrice = parseFloat(CurPrice);
		var OptPrice = 0.00;
		var selid;
		$('#sel_prd').find('.productlistoptions').each(function(){
				selid = $(this).attr('id');
	            var cb = $('#'+selid);
				OptPrice = 0.00;
                if (cb.is(':checked')) { 
					OptPrice = $('#'+selid).attr('rel');
				} 			
				CurPrice += parseFloat(OptPrice);				
		});
		
		$('#sel_prd').find('.productdropoptions').each(function(){
				selid = $(this).attr('id');
				OptPrice = $('#'+selid+' option:selected').attr('rel');
				CurPrice += parseFloat(OptPrice);				
		});
		
		var qty = $('#sel_prd').find('.productqty').val();
		if(qty > 0){
			//do nothing
		} else {
			qty = 1;		
		}
		CurPrice = CurPrice * qty;
		
		var phtml = CurPrice.toFixed(2);
		/*$('#price').html(phtml);*/
        $('.sel_price').html(phtml);
		
		if (Cufon) {
			Cufon.replace('#price');
		}
	
	});

	//when the dropdown options are changed on the show.php page 
	$('.productdropoptions').change(function(){
										 
		//var currency = $(this).parents('.product').attr('alt');
		var CurPrice = $('#product_price').val();
		CurPrice = parseFloat(CurPrice);
		var OptPrice = 0.00;
		var selid;
		$('#sel_prd').find('.productlistoptions').each(function(){
				selid = $(this).attr('id');
	            var cb = $('#'+selid);
				OptPrice = 0.00;
                if (cb.is(':checked')) { 
					OptPrice = $('#'+selid).attr('rel');
				} 			
				CurPrice += parseFloat(OptPrice);				
		});
		
		$('#sel_prd').find('.productdropoptions').each(function(){
				selid = $(this).attr('id');
				OptPrice = $('#'+selid+' option:selected').attr('rel');
				CurPrice += parseFloat(OptPrice);				
		});
		
		
		var qty = $('#sel_prd').find('.productqty').val();
		if(qty > 0){
			//do nothing
		} else {
			qty = 1;		
		}
		CurPrice = CurPrice * qty;
		
		var phtml = CurPrice.toFixed(2);
		/*$('#price').html(phtml);*/
        $('.sel_price').html(phtml);
		
		if (Cufon) {
			Cufon.replace('#price');
		}
	
	});

	//when the quantity is changed on Quantity dropdown on the show.php page 
	$('.productqty').change(function(){
		var CurPrice = $('#product_price').val();
		CurPrice = parseFloat(CurPrice);
		var OptPrice = 0.00;
		var selid;
		$('#sel_prd').find('.productlistoptions').each(function(){
				selid = $(this).attr('id');
	            var cb = $('#'+selid);
				OptPrice = 0.00;
                if (cb.is(':checked')) { 
					OptPrice = $('#'+selid).attr('rel');
				} 			
				CurPrice += parseFloat(OptPrice);				
		});
		
		$('#sel_prd').find('.productdropoptions').each(function(){
				selid = $(this).attr('id');
				OptPrice = $('#'+selid+' option:selected').attr('rel');
				CurPrice += parseFloat(OptPrice);				
		});
		
		var qty = $('#sel_prd').find('.productqty').val();
		if(qty > 0){
			//do nothing
		} else {
			qty = 1;		
		}
		CurPrice = CurPrice * qty;
		
		var phtml = CurPrice.toFixed(2);
		/*$('#price').html(phtml);*/
        $('.sel_price').html(phtml);
		
		if (Cufon) {
			Cufon.replace('#price');
		}
			
	});

	//when the add to cart button is clicked on the show.php page 
	$('.detaddtocart').click(function(){
		var errorcount = 1;
		if ($(".prodlistoptions").length > 0){
			$('.prodlistoptions').each(function(){
				OptPrice = 0.00;						 	
				$(this).find('.productlistoptions').each(function(){
						selid = $(this).attr('id');
						var cb = $('#'+selid);
						if (cb.is(':checked')) { 
							OptPrice = $('#'+selid).attr('rel');
							errorcount = 0;
						} 			
				});
				var errMsg = $(this).find('.validate_option').val();				
				if(errMsg == 0 ){
					errorcount = 0;	
				}
				if(errMsg != 0 && OptPrice <= 0){
					alert(errMsg);
				}
			});
		} else {
			errorcount = 0;	
		}

		var selid;
		var OptSelected;
		var mandError = '';
		$('#sel_prd').find('.productdropoptions').each(function(){
			selid = $(this).attr('id');
			OptSelected = $('#'+selid+' option:selected').val();
			mandatory = $(this).attr('title');
			if(mandatory != '' && OptSelected <= 0){
				mandError = mandError + mandatory + ', '; 
			}
		});
		
		if(mandError != ''){
			mandError = mandError.slice(0, -2);
			alert("Please Select "+mandError);
			return false;			
		}

		if(errorcount == 1){
			return false;	
		}
		
		pid = $(this).attr('id');
		///animated shadow
		var pcont = $('#' + pid + '_product');
        var cart = $('#cart');		
        var shadow = $('#' + pid + '_shadow');
        
        $('body').prepend('<div class="shadow" id="'+pcont.attr('id')+'_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: 300 
		  		} )
		    .animate({ 
		    	opacity: 0 
		    },
		    { 
		    duration: 100,
		    complete: function(){
		    	
		    	shadow.remove();
		    	
		    }
		    
		    });
		 
		    
	/// add to cart
		$.post(  
                "/modules/cart/minicart.php",  
                $('#sel_prd').serialize(),  
               function(data){  
                   $('#items').html(data);
				   		var remData = "product_info=1&type=product&prdId=" + pid;
		                
						$.ajax({
							type : 'POST',
							url : '/modules/cart/minicart.php',
							data : remData,
							success : function (html) {
								$('#'+ pid + '_added').html(html);
								if (Cufon) {
									Cufon.replace('.addCount');
								}
							}
						});
               }  
           );

		

	});



}
