var qproCatSeries;
var dataCache= {}

jQuery(document).ready(function() {
	var $jq				= jQuery							 
	var $scroller 		= $jq('#qpro_cat_series .series_content ul');
	var $links 			= $jq('#qpro_cat_series .series_content ul a');
	var $productsBlock 	= $jq('#qpro_cat_series .products_block');
	var linkCount 		= $links.length;
	var hasTabs 		= $jq('body').hasClass('tabs');
	

	initaliseQProCatSeriesControls ();
	// set up number of series

	$scroller.parent().parent().addClass('c' + (linkCount <= 6 ? linkCount : 6));


	// nav setup
	var pageCount = parseInt((linkCount - 1) / 6) + 1;
	if (pageCount == 1) {
		var $seriesNav	= $jq('#qpro_cat_series .series_nav');
		if($seriesNav.length > 0)		
	  		$seriesNav[0].style.visibility= 'hidden';
    	if (hasTabs) { $seriesNav.remove(); }
	}
	else {
		// populate nav
		for (var i = 1; i < pageCount; i++) {
			var $pageLinks = $jq('#qpro_cat_series .series_nav ul a[class^="page"]');
			$pageLinks.eq($pageLinks.length - 1).parent().after('<li><a href="#" class="page page' + (i + 1) + '"><span>' + (i + 1) + '</span></a></li>');
		}

		// setup buttons
		var $backLink 	= $jq('#qpro_cat_series .series_nav ul a[class="back"]');
		var $pageLinks 	= $jq('#qpro_cat_series .series_nav ul a[class^="page"]');
		var $nextLink 	= $jq('#qpro_cat_series .series_nav ul a[class="next"]');

		$backLink.parent().addClass('disabled');
		$pageLinks.eq(0).parent().addClass('active');
		
		// click handlers
		$pageLinks.click(function(e) {
			var documentDirection;					  
			e.preventDefault();
			
			var clickedPageIndex = $pageLinks.index(this);


				
			$pageLinks.parent().removeClass('active');
			$jq(this).parent().addClass('active');
			
			$backLink.parent().add($nextLink.parent()).removeClass('disabled');
			if (clickedPageIndex == 0) {
				$backLink.parent().addClass('disabled');
			}
			else if (clickedPageIndex == $pageLinks.length - 1) {
				$nextLink.parent().addClass('disabled');
			}
			
			//-- check if the document direction is left to right or right to left
			documentDirection = $jq("body").css ("direction");
			qproCatSeries.collapseProductsDisplay();
			if (documentDirection == "rtl")
				$scroller.css('right', clickedPageIndex * -864);
			else	
				$scroller.css('left', clickedPageIndex * -864);
			
			
		});
		
		$backLink.click(function(e) {
			e.preventDefault();
			var currentIndex = $pageLinks.index($jq('#qpro_cat_series .series_nav ul li.active a')[0]);
			if (currentIndex != 0) {
				$pageLinks.eq(currentIndex - 1).click();
			}
		});

		$nextLink.click(function(e) {
			e.preventDefault();
			var currentIndex = $pageLinks.index($jq('#qpro_cat_series .series_nav ul li.active a')[0]);
			if (currentIndex != $pageLinks.length - 1) {
				$pageLinks.eq(currentIndex + 1).click();
			}
		});

    $jq('#qpro_cat_series .series_nav')[0].style.visibility = 'visible';
	}
	
	// click handlers for series
	$links.click(function(e) {
		e.preventDefault();
		var pss				= qproCatSeries;
		var clicked 		= this;
		var clickedIndex 	= $links.index(clicked);
		var currentIndex 	= $links.index($jq('#qpro_cat_series .series_content ul li.active a')[0]);
		
		pss.unHighlightProduct();
		pss.highlightProduct(clicked);
		if (currentIndex >= 0) {

			if (clickedIndex != currentIndex) {
				pss.closeProductsBlock(function(){							
					pss.loadProducts ();
				});
			}
			else {
				if (pss.selectedSeries.href == clicked.href)
					pss.collapseProductsDisplay();
			}

		}
		else {					
			pss.loadProducts ();
		}
	});

  if($jq('#qpro_cat_series .series_block').length > 0)	
  	$jq('#qpro_cat_series .series_block')[0].style.visibility = 'visible';
  
  if($jq('#qpro_cat_series').length > 0)
  	$jq('#qpro_cat_series')[0].style.display = 'block';
  
  if($scroller.length > 0)
  	$scroller[0].style.display ='block';

});



function initaliseQProCatSeriesControls (){
	qproCatSeries = {
		//-- jquery html items
		$scroller:		$jq('#qpro_cat_series .series_content ul'),
		$links:			$jq('#qpro_cat_series .series_content ul a'),
		$productsBlock:	$jq('#qpro_cat_series .products_block'),
		$preloaderBlock:$jq('#qpro_cat_series .preloader_block'),
		$contentBlock:	$jq('#qpro_cat_series #content_block'),
		//-- variables for checks
		selectedSeries:"",
		speed:			800,
		isPreloaderShown:false,
		isProductsLoaded:false,
		isAnyProductsSelected:false,

		data:"",
		isProductsRevealing:false,
		isPreloaderRevealing:false,
		
		
		loadProducts: function (callback){
			var pss = qproCatSeries ;
			var $selectedProducts;

			this.data					= "";
			$selectedProducts			= $jq('#qpro_cat_series .series_content ul li.active a');
			this.selectedSeries 		= $selectedProducts [0];


			if ($selectedProducts.length > 0){
				this.openLoadingBlock();
			}
			else {
				pss.collapseProductsDisplay();
			}

		},
		
		
		
		processLoadRequest:function (callback){
			var pss = qproCatSeries ;
			var tmp;
			var seriesName ;
			var selectedProduct;
			var $selectedProducts;
			var undefinedVar
			$selectedProducts			= $jq('#qpro_cat_series .series_content ul li.active a');
			this.selectedSeries 		= $selectedProducts [0];
			if (this.selectedSeries  != undefinedVar){

				if (this.isSeriesCached()){
					if (dataCache [this.getSeriesName()].split (this.getSeriesName()).length > 1)
						this.checkIfReadyToReveal();
					else{
						selectedProduct			=  this.selectedSeries
						$jq.get(selectedProduct.href, function(data, textStatus){
							pss.productsDataRecieved(data, textStatus);
							if (typeof callback == "function")
								callback (data, textStatus);
						});		
					}	
				}
				else{
					selectedProduct			=  this.selectedSeries
					$jq.get(selectedProduct.href, function(data, textStatus){
						pss.productsDataRecieved(data, textStatus);
						if (typeof callback == "function")
							callback (data, textStatus);
					});		
				}
			}
			else {
				pss.collapseProductsDisplay();
			}
		},
		
		
		
		productsDataRecieved: function (data, textStatus){
			var pss = qproCatSeries ;
			var $selectedProducts;
			var seriesName;
			
			//-- check if series laoded is correct
			if (textStatus != "success"){
				this.loadProducts()	
			}
			else if (this.selectedSeries != ""){
				seriesName = this.getSeriesName();
				if (data.split (seriesName).length > 1){
					dataCache[seriesName]	= data;
					pss.isProductsLoaded 	= true;
					pss.checkIfReadyToReveal();
				}
				else{
					this.loadProducts();
				}
			}
			else{
				pss.collapseProductsDisplay();
			}

		},
		
		
		checkIfReadyToReveal:function (){
			var pss = qproCatSeries ;
			var $selectedProducts;
			var currentSelectedSeries;


			if (this.selectedSeries != ""){
				if (this.isSeriesCached()){
					this.$productsBlock[0].innerHTML = dataCache [this.getSeriesName()];
					this.$productsBlock.hide();
					this.initialiseProductsView();
						
				}
				else{
					this.loadProducts();
				}
			}
			else {
				this.collapseProductsDisplay();
			}
		},
		
		
		initialiseProductsView :function(callback){							
			// click handler for close button
			$jq('#qpro_cat_series .products_close a').click(function(e) {
				e.preventDefault();
				qproCatSeries.collapseProductsDisplay();
			});
			
			// convert products list to table
			var $products = $jq('#qpro_cat_series .products_content > ul > li');

			var table = '<table style="width:100%;">';

			$products.each(function(i) {

				var remainder = (i + 1) % 3; 
				if (remainder == 1) { table += '<tr>'; }
					table += '<td>' + this.innerHTML + '</td>';
				if (remainder == 0) { table += '</tr>'; }
			});
			
			var remainder = ($products.length % 3);
			var buffer = (remainder == 0) ? 0 : 3 - remainder;

			
			if (buffer != 0) 
				table += '<td colspan="'+buffer+'">&nbsp;</td>';	
			
			if ($products.length == 1)
				table += '<td class="no_leftborder">&nbsp;</td>';	
			
			table += '</tr>';
			table += '</table>';
			
			$jq('#qpro_cat_series .products_content').html(table);
			$jq('#qpro_cat_series .products_content tr:first').addClass('first');
			$jq('#qpro_cat_series .products_content tr:last').addClass('last');
			$jq('#qpro_cat_series .products_content tr td:first-child').addClass('first');
			$jq('#qpro_cat_series .products_content tr td:last-child').addClass('last');
			
			window.setTimeout ("qproCatSeries.doDelayedDisplay()", 300);
			if (typeof callback == "function"){
				callback();
			}
			
		},
		
		//-- call to open the products display is chained here, so that the preloader does not hide itself prematurely
		doDelayedDisplay:function (){
			var pss = qproCatSeries ;

			if ($jq('#qpro_cat_series .series_content ul li.active a').length > 0){
				if (this.isCorrectSeries()){
					pss.closeLoadingBlock ();
					pss.openProductsBlock ();

				}
				else{
					pss.loadProducts();
				}
			}
			else {
				pss.collapseProductsDisplay();
			}
		},
		
		
		
		openProductsBlock: function(callback) {
			var pss = qproCatSeries ;

			if (pss.$productsBlock [0].innerHTML != ""){
				this.isProductsRevealing = true;
				this.$productsBlock.css("display", "block");

				pss.$productsBlock.slideDown(pss.speed, function (){
					if (this.selectedSeries == ""){
						pss.collapseProductsDisplay();
					}
					else if (!pss.isCorrectSeries()){
						pss.closeProductsBlock (function(){
							pss.loadProducts();								 
						});
						
					}
					else if (typeof callback =="function"){
						callback();
					}										  
				});
			}
			else{
				pss.checkIfReadyToReveal()	
			}
			 
		},
		
		
		
		closeProductsBlock: function (callback) {
			var pss 	= qproCatSeries ;

			this.isProductsRevealing = false;
			pss.$productsBlock.slideUp(pss.speed, function() {
				pss.closeBlock();
				if (typeof callback == "function") {
					callback();
				}
			});

		},
		
		
		openLoadingBlock:function (callback){
			var height;
			var pss = qproCatSeries ;
			var currentSelectedSeries;
			var $selectedProducts;

			
			if (!this.isPreloaderRevealing){
				if (this.selectedSeries != ""){
					this.isPreloaderRevealing = true;
					this.$preloaderBlock.slideDown (this.speed, function(){		
																 
						if (typeof callback =="function"){
							callback();
						}													 
																		 
						if (this.selectedSeries != ""){
							pss.processLoadRequest();
							pss.isPreloaderShown = true;
						}
						else {
							pss.collapseProductsDisplay();
							
						}
					});
				}
				else {
					pss.collapseProductsDisplay();	
				}
						
			}
			else{
				if (this.isPreloaderShown){
					if (this.selectedSeries != ""){
						pss.processLoadRequest();
						if (typeof callback =="function"){
							callback();
						}
					}
					else {
						pss.collapseProductsDisplay();	
					}
						
				}	
			}
		},
		
		
		closeLoadingBlock: function (callback){
			var pss = qproCatSeries ;
			var numberOfSelectedProducts;
			if (this.isPreloaderRevealing){
				this.isPreloaderRevealing 	= false;
				this.isPreloaderShown 		= false;
				this.$preloaderBlock.slideUp (this.speed, function(){
										   
					if (this.selectedSeries != "" && !pss.isCorrectSeries()){
						pss.openLoadingBlock();
					}
					else if (typeof callback == "function"){
						callback();
					}								   
				});
			}

		},
		
		
		closeBlock: function () {
			var pss = qproCatSeries ;
			pss.$productsBlock [0].innerHTML != ""
			this.$productsBlock.css("display", "none");
		},
		
		
		highlightProduct: function(clicked){
			var pss 			= qproCatSeries ;
			var clickedIndex 	= pss.$links.index(clicked);
			
			$jq(clicked).parent().addClass('active');
			pss.$scroller.parent().parent().parent().addClass('a' + ((clickedIndex % 6) + 1))
		},
		
		
		
		unHighlightProduct:function (){
			var pss = qproCatSeries ;
			pss.$links.parent().removeClass('active');
			pss.$scroller.parent().parent().parent().removeClass('a1 a2 a3 a4 a5 a6');
		},
		
		
	
		
		collapseProductsDisplay:function (callback){
				var pss = qproCatSeries ; 
				pss.data 				= "";
				pss.selectedSeries		= "";
				pss.isProductsLoaded 	= false;	
				this.unHighlightProduct();
				this.closeLoadingBlock();
				this.closeProductsBlock(function (){
	
					if (typeof callback == "function")
						callback();
				});
				
		},
		
		
		
		isCorrectSeries:function  (){
			var bool;
			var currentSelectedSeries;
			var $selectedProducts;
			var undefinedVar;
			
			$selectedProducts 		=$jq('#qpro_cat_series .series_content ul li.active a');
			currentSelectedSeries 	= $selectedProducts [0];
			if ($selectedProducts.length > 0)
				bool = (currentSelectedSeries.href == this.selectedSeries.href && this.selectedSeries.href != "")
			else
				bool = false;
				
			return bool;
		},
		
		
		
		isSeriesCached:function  (){
			var bool;
			var selectedSeriesHref;
			var tmp;
			var seriesName;
			var undefinedVar;
			
			if (this.getSeriesName() == undefinedVar){
				bool = false;
			}
			else {
				bool = (dataCache [this.getSeriesName()] != undefinedVar);
			}
			return bool;
		},
		
		
		
		getSeriesName :function (){
			var $selectedProducts		= $jq('#qpro_cat_series .series_content ul li.active a')
			var selectedSeriesHref;
			var tmp;
			var seriesName;
			
			if ($selectedProducts.length > 0){
				selectedSeriesHref 	= $selectedProducts [0].href;
				tmp				  	=  selectedSeriesHref.split ("&");
				seriesName 			= tmp[tmp.length-1];
			}
			return seriesName
		}
	}
	qproCatSeries.$productsBlock.hide();
	qproCatSeries.$productsBlock.css("display", "none");
	qproCatSeries.$preloaderBlock.hide();
}