/*!
 * 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 : 'lib/cart.php',
			success : function (html) {
				$('#items').html(html);
				$('#gotopayment').html('Checkout with '+ $('.gateway').attr('rel'));
			}
		});


	/*********************format products ****************************/

	this.each(function(){	
	
		/************* load xml configuration ***************/
		
		var xmldata = '';
		var currency = '';
		var container = $(this);
		var cat = container.html();
		container.html('');
 		hasopt = Array();
 		optionsprice = Array();
 		

		
		var counter = 1;
		
		$.ajax({
		    type: "GET",
			url: "lib/config.xml",
			async: false,
			dataType: "xml",
			success: function(xml) {

			 		$(xml).find('Shop').each(function () {
			 			currency = $(this).find('Currency').text();
			 		});

			 		$(xml).find('Product').each(function () {
			 			category = $(this).find('Category').text();
			 			
				 		name = $(this).find('Name').text();
				 		picture = $(this).find('Picture').text();
				 		bigpicture = $(this).find('BigPicture').text();
				 		price = $(this).find('Price').text();
				 		teaser = $(this).find('Teaser').text();
				 		attribute = $(this).find('Attributes').text();
				 		details = $(this).find('Details').text();
				 		hasopt[counter] = '0';
				 		optionsprice[counter] = '0';

				 		if (category == cat) {
				 		
						 		code = '<div id="p'+counter+'" class="product fixed" alt="'+currency+'">'; 
						 		
						 		if (bigpicture != '') {
						 			
						 			code += '<a href="'+bigpicture+'" rel="'+name+'" class="zoom"><img src="'+picture+'" alt="'+name+'" /></a>';
						 			
						 		}
						 		else {
								 		if(jQuery().bloooming_roundPic) {
		
											code += '<div class="rounded" style="background:url('+picture+') top left no-repeat;"></div>';
										
										} else {
								 			
								 			code += '<img src="'+picture+'" alt="'+name+'" />'; 
										}
						 		} // no big pic
						 		
						 		
						 		
						 		code += '<h3>'+ name +'</h3>'; 
						 		code += '<p>'+ teaser +'</p>'; 
						 		code += '<span class="pp">'+ attribute +'</span>'; 

						 		
						 		opt = '';
						 		optc = '';
						 		oprice = '';
						 		
						 		var optcount = 0;
						 		optar = Array();
						 		
						 		$(this).find('Option').each(function () {
						 			
						 			oprice = '';
						 			
						 			optext = $(this).text();
						 			
						 			oprice = $(this).attr('price');
						 			optar[optcount] = oprice;
						 			
			 						opt += '<option rel="'+oprice+'" value="'+optext+'">'+optext+'</option>';

			 						if (oprice != undefined) {
			 							optionsprice[counter] = '1';
			 						}
			 						
			 						if (optext != '') {
			 							hasopt[counter] = '1';	
			 						}
			 						
			 					optcount ++;
			 							
			 					});
			 					

			 					
			 					if (optionsprice[counter] == '0') {
							 	
							 		code += '<span rel="'+price+'" class="price">'+ currency + ' ' + price +'</span>'; 
			 					
			 					} 
			 					else {

							 		code += '<span rel="'+optar[0]+'" class="price">'+ currency + ' ' + optar[0] +'</span>'; 
			 						
			 					}
			 					
			 					code += '<div class="shopcontrols">';						 		
						 		
						 		code += '<span id="bp'+counter+'" class="addtocart">add to cart</span>';
						 		
			 								 					
			 					if (hasopt[counter] == '1') {
			 						
			 						optc = '<select class="productoptions" id="op'+counter+'" rel="'+optionsprice[counter]+'">'+opt+'</select>';
			 					}

						 		code += optc;
						 		
						 		code += '</div><!--end controls-->';
						 		
						 		gal = '<ul class="littlegal fixed">';
						 		
						 		$(this).find('Pic').each(function () {
			 						gal += '<li><a href="'+$(this).find('ZoomPic').text()+'" rel="'+name+'" class="zoom"><img height="85" src="'+$(this).find('Thumbnail').text()+'" alt="'+name+'" /></a></li>';
			 					});
			 					
			 					gal += '</ul>';
			 					

						 		
						 		if (details != '') {
						 			
						 			code += '<a class="showdetails">show details</a>';
						 			code += '<div class="details"><p>'+ details + '</p>' + gal +'</div>';
						 			
						 		}
						 		
						 		
						 		code += '</div>'; 
		
							 	container.append(code);
					 	
						 		if (jQuery().sSelect) {
						 			
						 			$('#op'+counter).sSelect();
						 			
						 		}
					 	} // category fits
					 	
		
					counter ++;
					
			 		}); 
			}
		
		});
		

	});
	
	
	$('.productoptions').change(function(){
	
		var selid = $(this).attr('id');
		var selprice = $('#'+selid+' option:selected').attr('rel');
		var prid = selid.substr(1);
		var currency = $('#'+prid).attr('alt');
		var optprice = $(this).attr('rel');
		
		var phtml = currency+' '+selprice;
		
		if (optprice == 1) {
			
			$('#'+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(){

		pid = $(this).attr('id').substring(1);
		
		///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
	
		var name = $('#' + pid + ' > h3').text();
		var price = $('#' + pid + ' > .price').attr('rel');
		var option = '';
		option = $('#o'+pid+' option:selected').attr('value');
		var oprice = $('#o'+pid).attr('rel');
		
		var formData = 'id=' + pid + '&name='+ name + '&price=' + price + '&option=' + option + '&oprice=' + oprice; 
		
		$.ajax({
			type : 'POST',
			url : 'lib/cart.php',
			data : formData,
			success : function (html) {
				$('#items').html(html);
			}
		});
		

	});
	
	$('.removeitem').live('click', function() {		
		rid = $(this).attr('id');
		rop = $(this).attr('rel');

		var remData = 'remove=' + rid + '&rop=' + rop; 
		
		$.ajax({
			type : 'POST',
			url : 'lib/cart.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;
			}
		});
		
	});	
	

}



