/*!
 * 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);

			}
		});

	/*********************format products ****************************/

	this.each(function(){	
	
		/************* load xml configuration ***************/
		
		var xmldata = '';
		var currency = '';
		var container = $(this);
		var cat = container.html();
		container.html('');
		
		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();
				 		price = $(this).find('Price').text();
				 		teaser = $(this).find('Teaser').text();
				 		attribute = $(this).find('Attributes').text();
				 		details = $(this).find('Details').text();
				 		
				 		if (category == cat) {
				 		
						 		code = '<div id="p'+counter+'" class="product fixed">'; 
						 		
						 		if(jQuery().bloooming_roundPic) {


									code += '<div class="rounded" style="background:url('+picture+') top left no-repeat;"></div>';
								
								} else {
						 			
						 			code += '<img src="'+picture+'" alt="'+name+'" />'; 
								}
						 		
						 		code += '<h3>'+ name +'</h3>'; 
						 		code += '<p>'+ teaser +'</p>'; 
						 		code += '<span class="pp">'+ attribute +'</span>'; 
						 		code += '<span rel="'+price+'" class="price">'+ currency + ' ' + price +'</span>'; 
						 		code += '<span id="bp'+counter+'" class="addtocart">add to cart</span>';
						 		
						 		if (details != '') {
						 			
						 			code += '<a class="showdetails">Details zeigen</a>';
						 			code += '<div class="details">'+ details + '</div>';
						 			
						 		}
						 		
						 		
						 		code += '</div>'; 
		
							 	container.append(code);
					 	
					 	} // category fits
					 	
		
					counter ++;
					
			 		}); 
			}
		
		});
		

	});
	


	$('.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 formData = 'id=' + pid + '&name='+ name + '&price=' + price; 
		
		$.ajax({
			type : 'POST',
			url : 'lib/cart.php',
			data : formData,
			success : function (html) {
				$('#items').html(html);

			}
		});
		

	});
	
	$('.removeitem').live('click', function() {		
		rid = $(this).attr('id');

		var remData = 'remove=' + rid; 
		
		$.ajax({
			type : 'POST',
			url : 'lib/cart.php',
			data : remData,
			success : function (html) {
				$('#items').html(html);
			//	alert('thx');
			}
		});
		
	});
	
	$('#gotopaypal').click(function(){
			
		$.ajax({
			type : 'GET',
			url : 'lib/checkout.php',
			success : function (html) {
				window.location.href = html;
			}
		});
		
	});

}


		
