/**
*  Product Manager
*
* @version 0.1
*/ 

$(document).ready(function(){
	
	var $this = this;
	var _productCount = 9;
	
	this.mainCategories;
	
	// duration plugin 
	$.fn.pause = function(duration) {
	    $(this).animate({ dummy: 1 }, duration);
	    return this;
	};
	
	document.productManager = new function()
	{
		var $this = this;
		
		this.query = function(manager, act, request, onComplete){
			$.ajax({
				url: 		'/'+ window.ABS_PATH +'api.php?dataType=json&manager='+manager+'&act='+act,
				data: 		request,
				success: 	function(r){
					onComplete(r.responce);
				},
				dataType: "json"
			});
		};
		
		/**
		 * dropdown manager
		 */
		this.dropDown = new function()
		{
			var $dropDown = this;
			var $opened = false;
			
			// prepare dropdown layer
			$("#dropdownItems").css("top","47px");
			$("#dropdownItems").css("left","44px");
			$("#dropdownItems").css("display","block");
			$('#dropdownItems').slideUp(0);
			
			
			this.open = function(){
				$('#dropdownItems').slideDown(300,'linear',function(){
				});
				$opened = true;
				$("#productGroupSelector").css("background-image","url(/media/elements/upButton.gif)");
			}
			
			this.close= function(){
				$('#dropdownItems').slideUp(300,'linear',function(){
						
				});		
				$opened = false;	
				$("#productGroupSelector").css("background-image","url(/media/elements/downButton.gif)");
			}
			
			this.swap = function(){
				$opened?$dropDown.close():$dropDown.open();
			}
			
			
			this.getUriVars = function(){
				var a = location.href.split("#")[0].split("?");
				var url = a[0];
				console.dir(a);
				var params = a[1].split("&");
				var vars = {};
				var hash ;
				for(k in params ){   
				    hash = paramsEncoded[k].split("=");
				    vars[unescape(hash[0])] = unescape(hash[1]);
				}
			}
			
			this.setUri = function(params){
				var a = location.href.split("#")[0].split("?");
				var url = a[0];
				var uriParams = [];
				
				for(var k in params){
					uriParams.push( k +"="+escape(params[k]) );
				}
				
				location.href=url+"?"+uriParams.join("&");
			}
			
			this.select  = function(id,close){
				var categorie = $this.getCategorieInfo(id);
				
				// set title
				$("#productGroupSelector").html(categorie["title"]);
				var params = $dropDown.getUriVars();
				params['cat1'] = categorie["id"];
				$dropDown.setUriVars(params);
				if(close)
					$dropDown.close();
				
			}
		}; // end of product categories dropdown


		this.getCategorieInfo = function(id){
			var cat;
			var r;
			for(var k in $this.mainCategories){
				cat = $this.mainCategories[k];
				if(cat["id"] == id){
					r = cat;
					break;
				}
			}
			
			return r;
		}
		

		var lastOpened = null;
		var easing = "linear";
		var duration = 250;	

		this.openDetails = function (button)
		{	
			var buttonDetails 		= $(button);
			var productInfo 		= buttonDetails.parents(".productInfo");
			var text1 				= productInfo.find(".text1");
			var longText1 			= productInfo.find(".longText1");
			var previewImage 		= productInfo.find(".preview");
			var h2 					= productInfo.find("h2");
			var closeButton 		= productInfo.find(".closeButton");
			var originalPrice 		= productInfo.find(".originalPrice");
			var offerPrice 			= productInfo.find(".offerPrice");
			var galleryPrevButton 	= productInfo.find(".prev");
			var galleryNextButton 	= productInfo.find(".next");
			var subNavigation 		= productInfo.find(".subNavigation");
			var similarProducts 	= productInfo.find(".similarProducts");
			var id					= productInfo.attr("id").replace(/[^0-9]+/,"");
			
			similarProducts.show();
			
			
			
			
			// add click
			$this.query("Products","addClick",{id:id},function(r){
				
			});
			
			productInfo.attr("status","opened");
			subNavigation.fadeIn(duration,easing);
			
//			galleryPrevButton.fadeIn(duration,easing);
//			galleryNextButton.fadeIn(duration,easing);
//			
			productInfo.css("background-color","#ffffff");
			productInfo.css("border-top","1px solid #04f2f2");
			buttonDetails.fadeOut(0,easing);
			closeButton.fadeIn(duration,easing);
			 
			previewImage.animate({
			    width: "200px"
			  }, duration,easing);
			
			h2.animate({
				lineHeight: "33px",
			    fontSize: "28px",
			    marginBottom: 20,
			    marginTop: 10
			  }, duration,easing,function(){
			  });
			originalPrice.animate({
			    fontSize: "13px",
			    lineHeight: "15px",
			    padding: 5
			  }, duration,easing,function(){
				 
			  
				 
			  });
			 offerPrice.animate({
				    fontSize: "13px",
				    lineHeight: "15px",
				    padding: 3
				  }, duration,easing);
			
			text1.fadeOut(duration,function(){
				
			},easing);
			//$('html,body').animate({scrollTop: productInfo.offset().top},duration);
			longText1.fadeIn(duration,function(){
				
			},easing);
			window.setTimeout(function(){
				$('html,body').animate({scrollTop: $("#product"+id+"link").offset().top},duration,function(){
					location.href = '#product'+id;
				});
			},0);
			
			if(lastOpened != null && button != lastOpened){
				$this.closeDetails($(lastOpened).parents(".productInfo").find(".closeButton"));
				lastOpened = null;
			}
			lastOpened = button;	
		}
		
		this.closeDetails = function (button)
		{
			var closeButton 		= $(button);
			var productInfo 		= closeButton.parents(".productInfo");
			var buttonDetails 		= $(productInfo.find(".buttonDetails"));
			var text1 				= productInfo.find(".text1");
			var longText1 			= productInfo.find(".longText1");
			var previewImage 		= productInfo.find(".preview");
			var h2 					= productInfo.find("h2");
			var buttonDetails 		= $(productInfo.find(".buttonDetails"));
			var originalPrice 		= productInfo.find(".originalPrice");
			var offerPrice 			= productInfo.find(".offerPrice");
			var galleryPrevButton 	= productInfo.find(".prev");
			var galleryNextButton 	= productInfo.find(".next");
			var subNavigation 		= productInfo.find(".subNavigation");
			var similarProducts 	= productInfo.find(".similarProducts");
			
			productInfo.attr("status","closed");
			subNavigation.fadeOut(duration,easing);
			
			similarProducts.hide();
			
			
//			galleryPrevButton.fadeOut(duration,easing);
//			galleryNextButton.fadeOut(duration,easing);
//			
			
			
			productInfo.css("background-color","transparent");
			productInfo.css("border","0");
				
			closeButton.fadeOut(duration);//button.css("display","none");
			
			h2.animate({
			    fontSize: "20px",
			    marginBottom: 20,
			    marginTop: 10
			  }, duration,easing);
			
			previewImage.animate({
			    width: 150
			  }, duration,easing);
			
			
			
			originalPrice.animate({
			    fontSize: "10px",
			    lineHeight: "10px",
			    padding: 4
			  }, duration,easing,function(){

			  
				 
			  });
			
			 offerPrice.animate({
				    fontSize: "10px",
				    lineHeight: "10px",
				    padding: 4
				  }, duration,easing);
			 
			 
			longText1.fadeOut(duration,function(){
				buttonDetails.fadeIn(duration,easing);
				text1.fadeIn(duration,function(){
					
				});
			});
			
		}
		this.selectFilterCategorie = function (title)
		{
			location.href = '?filterCategorie='+escape(title);
		}
		
		this.selectFilterDays = function (title)
		{
			location.href = '?filterDays='+escape(title);
		}
		
		// init
		
		// lightbox for images
		$('.productGallery a.productImage').click(function(){
			var imageLink			= $(this);
			var productInfo 		= imageLink.parents(".productInfo");
			if(productInfo.attr("status") == "closed" || !productInfo.attr("status")){
				var buttonDetails 		= $(productInfo.find(".buttonDetails"));
				buttonDetails.click();
				return false;
			}
		
		}).colorbox();;
		//.colorbox();
		
		// product formulars lightbox
		$('.cart').colorbox();
		$('.question').colorbox();
		$('.leaf').colorbox();
		
		// deprecated
		$("#closeButton").click(function(){
			$this.closeProductDetail();
		});
		
		//  add event on click for product details button
		$(".productInfo .buttonDetails").click(function(){
			$this.openDetails(this);
		});
		
		//  add event on click for close details button
		$(".productInfo .closeButton").click(function(){
			$this.closeDetails(this);
		});
		
		
		// add event on mouse over product subnavigation
		$(".productInfo .subNavigation a").hover(function(){
			var subNavigation 		= $(this).parents(".subNavigation");
			var label = subNavigation.find("span");
			var title = $(this).attr("title");
			label.html(title);
			label.fadeIn(0);
		});
		$(".productInfo .subNavigation a").mouseout(function(){
			var subNavigation 		= $(this).parents(".subNavigation");
			var label = subNavigation.find("span");
			label.fadeOut(0);
		});
		
		//$this.closeProductListDirectly();		
		
		
//		$this.query("Products","getCategories",{},function(r){
//			$this.mainCategories = r;
//		});
		//$this.openProductList();		
	};
	
	
	

});


