jQuery.noConflict();

jQuery(document).ready(function(){
	
	// sciezka konfiguracyjna do strony
	var APP_DIR = "";
	
	/**
	 * aktualnie wybrany produkt - id
	 */
	var currentProductId = 0;
	
	/**
	 * Dlugosc listy produktow
	 */
	var productListLength = 0;
	
	/**
	 * Surowe dane z serwera
	 */
	var productServerData = null;
	
	/**
	* Event handler
	*/
	// rozsuwanie menu
	jQuery('#menu li a').mouseover(function(){
		menuDown( this.parentNode );	
	});	
	jQuery('#menu li a').mouseout(function(){
		menuUp( this.parentNode );		
	});
	
	
	// zmiana tekstu glownego
	jQuery('#menu li a').click(function( e ){
		hideNaviagtion();
		e.preventDefault(); 
		changeMainText( this.className );
		if( appState.offerToggle ) setTimeout(function(){ toggleMainOffer()},500);		
	});
	
	
	// zmiana oferty
	jQuery('#bottomMenu a').click( function(e){
		e.preventDefault();
		var cid = parseInt( this.className.substr(2));
		loadMainOffer( cid );		
	});
	
	
	jQuery('#productNavigation img').click( function(e){
		e.preventDefault();
		var action = this.className;
		if( action == 'next' ){
			var choseNext = currentDataPack['p'+currentProductId].listIndex;
			if ( choseNext == (productListLength-1) ) {
				currentProductId = productServerData[ 0 ].id;
			}else {
				currentProductId = productServerData[ choseNext+1 ].id;
			}	 
			jQuery('#productDetail').fadeOut('slow', function(){showProductDetail( currentProductId )});	
		}else if( action == 'prev' ){
			var chosePrev = currentDataPack['p'+currentProductId].listIndex;
			if ( chosePrev == 0 ) {
				currentProductId = productServerData[ productListLength-1 ].id;
			}else {
				currentProductId = productServerData[ chosePrev-1 ].id;
			}	 
			jQuery('#productDetail').fadeOut('slow', function(){showProductDetail( currentProductId )});	
		}else{
			hideProductDetail();
		}
	});
	
	
	/**
	* Event function
	*/
	var selectedCatId = 0;
	var loadedDataCount = 0;
	// glowna oferta do gory / na dol
	var loadMainOffer = function( catId ){
		window.clearInterval(slider);		
		if( !appState.offerToggle ) toggleMainOffer();
		jQuery('#mainOffer ul *').remove();
		appState.currentOffer.cid = catId;
		dataRequest({query:"offer", cid: catId , p : appState.currentOffer.p, ps : appState.currentOffer.ps }, applyData );	
	};
	
	var currentDataPack = {};
	var applyData = function( data, where ){ 
		productListLength = 0;
		productServerData = {};
/*
{"id":"50",
"name":"Z - 62",
"desc":"wk",
"price":"0",
"h_price":"0",
"category":"1",
"status":"1",
"legend":null,
"burn":null,
"pack":null,
"height":"23",
"color":null,
"photo_url":"\/www\/img\/648a20321c77f2z62.jpg","photo_miniurl":"\/www\/img\/m648a20321c77f2z62.jpg"},
*/		currentDataPack = {};
		for( var i = 0, max = data.list.length; i < max; i++){
			var snippet = dataTemplates.offer
						.replace(/%1/,APP_DIR + data.list[i].photo_miniurl)
						.replace(/%2/,data.list[i].name)
						.replace(/%3/,data.list[i].name)
						.replace(/%4/g,data.list[i].id);
			jQuery('#mainOffer ul').append( snippet );
			currentDataPack[ 'p'+data.list[i].id ] = data.list[i];
			currentDataPack[ 'p'+data.list[i].id ].listIndex = i;
		}
		productListLength = data.list.length;
		productServerData = data.list;
		
		jQuery('#mainOffer ul').bind('click', function(e){
			e.preventDefault();
			var target = e.target || e.srcElement;
			if( target.nodeName != "A" ) target = target.parentNode;
			if( target.nodeName == "A" ){
				var pid = target.className.substr( target.className.indexOf('_')+1);
				if( parseInt( pid ) > 0 )
					showProductDetail( pid );
					currentProductId = pid;
			}
		}); 
		
		hideProductDetail();
		loadedDataCount = parseInt( i/5 )+1; 
		
		if( i > 10 ){
			showNaviagtion.blocked = false;
			showNaviagtion();
		}else{
			hideNaviagtion();
			showNaviagtion.blocked = true;
		}
		
		
	}
	
	/**
	 * Wyswietla informacje o produkcie
	 * @param {Integer} productId - id produktu
	 */
	var showProductDetail = function( productId ){
		window.clearInterval(slider);
		hideNaviagtion();
		jQuery( mainOfferHolder ).fadeOut('slow', function(){
			jQuery('#productDetail div.details').html();
			var html = "";
			if( currentDataPack[ 'p'+productId ].name ){
				html +='<p>Symbol : <span>'+currentDataPack[ 'p'+productId ].name+'</span></p>';
			}
			if( currentDataPack[ 'p'+productId ].desc ){
				html +='<p>Opis produktu : <span>'+currentDataPack[ 'p'+productId ].desc+'</span></p>';
			}
		
			if( currentDataPack[ 'p'+productId ].legend ){
				html +='<p>Dane produktu : <span>'+currentDataPack[ 'p'+productId ].legend+'</span></p>';
			}
	
			if( currentDataPack[ 'p'+productId ].burn ){
				html +='<p>Czas palenia : <span>'+currentDataPack[ 'p'+productId ].burn+'</span></p>';
			}
	
			if( currentDataPack[ 'p'+productId ].color ){
				html +='<p>Dostępne kolory : <span>'+currentDataPack[ 'p'+productId ].color+'</span></p>';
			}
	
			if( currentDataPack[ 'p'+productId ].height ){
				html +='<p>Wysokość : <span>'+currentDataPack[ 'p'+productId ].height+' cm</span></p>';
			}
			jQuery('#productDetail div.details').html(html);
			//console.log(currentDataPack[ 'p'+productId ].photo_url);
			jQuery('#productDetailImg').attr('src', APP_DIR + currentDataPack[ 'p'+productId ].photo_url );
			jQuery('#productDetail').fadeIn('slow');			
		});
		
	}
	
	var hideProductDetail = function(  ){
		showNaviagtion();
		jQuery('#productDetail').fadeOut('slow',function(){
					jQuery( mainOfferHolder ).fadeIn('slow'); 	
		});
		jQuery('#productDetail div.details').html('');
		jQuery('#productDetailImg').attr('src', '');
	}
	
	
	var addNavigation = function(){
		var up = document.createElement('span');
		up.className = "upnav";
		up.id = "upnav";
		jQuery(up).mousedown(slideUp)
		jQuery(up).mouseup(slideStop)
		
		var un = document.createElement('span');
		un.className = "unnav";
		un.id = "unnav";
		jQuery(un).mousedown(slideDown)
		jQuery(un).mouseup(slideStop)
		
		jQuery('#mainTextHolder').append(up);
		jQuery('#mainTextHolder').append(un);
		
		mainOfferHolder = jQuery('#mainOffer ul');
		
		
		
	}
	
	
	var hideNaviagtion = function(){
		jQuery('#upnav,#unnav').css('display','none');
	}
	
	var showNaviagtion = function(){
		if (showNaviagtion.blocked) {
			hideNaviagtion();
			return true;
		}
		jQuery('#upnav,#unnav').css('display','block');
		currentHeight = loadedDataCount*155; //parseInt( jQuery('#mainOffer ul').css('height') );
		holderHeight = parseInt( jQuery('#mainOffer').css('height') );
		
				jQuery(mainOfferHolder).css('top', '0px');
		slideBlockUp = false;slideBlockDown = false;
	}
	showNaviagtion.blocked = true;
	
	var mainOfferHolder = null;
	var slideBlockUp = false;var slideBlockDown = false;
	var slider = null;
	var currentHeight = 0;
	var holderHeight = 0;
	var maxSlide = 0;
	
	var slideUp = function(){
		if( slideBlockUp ) return true;
		slideBlockDown = false;
		slider = window.setInterval(function(){
			var currentTop = parseInt( jQuery(mainOfferHolder).css('top'));
			currentTop+=6;
			if( currentTop > 0 ){
				window.clearInterval(slider);
				slideBlockUp = true;
				return true;				
			}
			jQuery(mainOfferHolder).css('top', currentTop+'px');
		}, 5)		
	}
	
	var slideDown = function(){
		if( slideBlockDown ) return true;
		slideBlockUp = false;
		maxSlide = holderHeight - currentHeight;
		slider = window.setInterval(function(){
			var currentTop = parseInt( jQuery(mainOfferHolder).css('top'));
			if( currentTop < maxSlide ){
				window.clearInterval(slider);
				slideBlockDown = true;
				return true;
			}
			currentTop-=6;
			jQuery(mainOfferHolder).css('top', currentTop+'px');
		}, 5)
	}
	
	var slideStop = function(){
		window.clearInterval(slider);
	}
	
	
	var dataRequest = function( params , fn ){
		params.t = new Date().getTime();		
		jQuery.post('xhr',params, function( data ){
			fn( data, params.query);			
		},"json");		
	}
	
	////////////////////////////////////////////////////////////////
	
	
	//zmiana tekstu glownego z menu gornego
	var selectedText = "mainSiteText";
	var changeMainText = function( eid ){
		jQuery('#mainFoto')[0].className = eid;
				
		jQuery('#'+selectedText).fadeOut();
		setTimeout( function(){jQuery('#'+eid).fadeIn()}, 500);
		selectedText = eid;			
	};
	// pokazanie lub ukrycie obszaru z menu glownego
	var toggleMainOffer = function(){
		if( jQuery('#mainOffer').css("top") == "360px" ){
			jQuery('#mainOffer').animate({ 
		        top : "0px"    
		    }, 300 );
		    appState.offerToggle = true;		
	    }else{
	    	jQuery('#mainOffer').animate({ 
		        top : "360px"    
		    }, 300 );
		    appState.offerToggle = false;	
	    }
	};
	
	
	
	
	
	
	// menu gora wysuniecie
	var menuDown = function ( el ){
		jQuery(el).animate({ 
	        paddingTop : "5px"       
	    }, 200 );		
	};
	//menu gora schowanie
	var menuUp = function ( el ){
		jQuery(el).animate({ 
	        paddingTop : "0px"    
	    }, 200 );
		
	};
	
	
	
	var appState = {
		initialized : false,	// aplikacja zainicjowana?
		mainToggle : false,		// glowny obszar na wierzchu	
		offerToggle : false,	// oferta na wierzchu
		wip : false,			// praca w toku
		
		currentOffer : {
			cid : 0,
			p : 1,
			ps : 10
		}
		
	}
	
	
	var dataTemplates = {
		
		offer : '<li><a class="id_%4" href="/product,%4"><img width="97" height="115" src="%1" alt="%2"/><span>%3</span></a></li>'
		
		
	}
	
	
	// poczatkowa animacja
	var init = function(){
		
		var w = jQuery(document).width();
		var h  = jQuery(document).height();
		
		jQuery('#companylogo').css({ top :  parseInt((h/2)-100)+"px", left : parseInt((w/2)-200)+"px"});
		setTimeout(function(){ jQuery('#companylogo').fadeIn(2000) } ,2000 ); //2000
		
		jQuery('#companylogo').click (function( e ){
			e.preventDefault();
			if ( appState.initialized ) return false;
			appState.initialized
			
			jQuery(this).animate({
				top : "0px",
				left: "0px"
			},1000,"swing", function(){ // 1000
				jQuery('#menu').animate({
					left : "0px"
				},1500,"swing", function(){ // 1500
					jQuery('#main').fadeIn(500); // 500
					jQuery('#footer').fadeIn(500);	// 500			
					
					addNavigation();
				});
			})
			
		});
		
		
		
		
	}();
	
});