var Product = {
	active_product_id: 0,
	active_view_mode: 0,
	active_sort_mode: 0,
	active_category_id: 0,
	active_external_product_id: 0,
	
	active_tab_id: 2,
	history_tab_id: 2,
	active_filter: new Object(),
	
	//---------------------------------------------------------------------------------------------------------------
	page_offset: 12,
	page_limit: 12,
	current_page_offset: 0,
	
	//---------------------------------------------------------------------------------------------------------------
	filter_status: new Object(),
	tabs: new Array(),
	
	//---------------------------------------------------------------------------------------------------------------
	showProductsTab: function(id)
	{
		if(isNaN(id))
			return;
		
		var active_tab = $("tab_" + this.active_tab_id);
		active_tab.className = "";
		var active_products = $("products_" + this.active_tab_id);
		active_products.style.display = "none";
		
		var tab = $("tab_" + id);
		tab.className = "tab-selected";
		var products = $("products_" + id);
		products.style.display = "block";
		this.active_tab_id = id;
				/**/
		SWFAddress.setValue(tab.title + "?c=" + id);
		return false;
	},
	
	//---------------------------------------------------------------------------------------------------------------
	handleProductsHistory: function(event) 
	{	
		for(var i = 0; i < event.parameterNames.length; i++)
		{
			if(event.parameterNames[i] == "c")
			{
				if(event.parameters["c"] != null && event.parameters["c"] != Product.active_tab_id)
				{
					Product.showProductsTab(event.parameters["c"]);
					return;
				}
			}
			
			if(event.parameterNames[i] == "o")
			{
				if(event.parameters["o"] != null && event.parameters["o"] != Product.current_page_offset)
				{
					Product.current_page_offset = parseInt(event.parameters["o"]);
					Product.getMoreProductsByFilter(Product.active_filter, Product.page_limit, Product.current_page_offset);	
					return;
				}
			}
		}
		
		if(event.path == "/" && Product.active_tab_id != Product.history_tab_id)
		{
			Product.showProductsTab(Product.history_tab_id);
		}
	},
	
	//---------------------------------------------------------------------------------------------------------------
	showDetailTab: function(id, site_anchor, play_video)
	{
		if(isNaN(id))
			return;
			
		var active_tab = $("tab_" + this.active_tab_id);
		active_tab.className = "";
		var active_products = $("product_" + this.active_tab_id);
		var product_downloads_hidden = $("product-downloads-hidden");
		var teaser_container = $("teaser_container");
		
		if (id==2){
			product_downloads_hidden.hide();
			teaser_container.show();
		}else{
			product_downloads_hidden.show();
			teaser_container.hide();
		}
	
		active_products.hide();
		
		var tab = $("tab_" + id);
		tab.className = "tab-selected";
		var products = $("product_" + id);
		products.show();
		this.active_tab_id = id;
		
		SWFAddress.setValue(tab.title + "?t=" + id);
		
		if(typeof(play_video) != 'undefined' && play_video == true && typeof(contentVideo_trailer) != 'undefined' && typeof(contentVideo_trailer.embedVideo) != 'undefined'){
			contentVideo_trailer.embedVideo(true);
		} else {
			if(typeof(contentVideo_trailer) != 'undefined' && typeof(contentVideo_trailer.embedVideo) != 'undefined'){
				contentVideo_trailer.embedVideo();
			}
		}
		
		if(id == 2)
			this.loadProductTOC(this.active_product_id);
		if(id == 6)
			this.loadServiceDownloads(this.active_product_id);
			
		if(String(site_anchor).length > 0)
			if($(site_anchor) != null)
				$(site_anchor).scrollIntoView(true);
		
		return false;
	},

	videoAutoPlay: false,
	
	//---------------------------------------------------------------------------------------------------------------
	handleProductDetailHistory: function(event) 
	{	
		var tab_id = 0;
		var site_anchor = "";
		var play_video = false;
	
		for(var i = 0; i < event.parameterNames.length; i++)
		{		
			if(event.parameterNames[i] == "t")
			{
				if(event.parameters["t"] != null && event.parameters["t"] != Product.active_tab_id)
				{
					tab_id = event.parameters["t"];
				}
			}	
			if(event.parameterNames[i] == "a")
				if(event.parameters["a"] != null)
					site_anchor = event.parameters["a"];
		}
		if(tab_id > 0)
		{
			Product.showDetailTab(tab_id, site_anchor);
			return;
		}
		
		if(event.path == "/" && Product.active_tab_id != Product.history_tab_id)
		{
			Product.showDetailTab(Product.history_tab_id);
		}
	},
	
	//---------------------------------------------------------------------------------------------------------------
	initProductDetails: function(product_id, toc_visible) 
	{			
		if(isNaN(product_id))
			return;
		
		if(this.active_tab_id == 2)
			this.loadProductTOC(product_id);
			
		this.active_product_id = product_id;
	},
	
	//---------------------------------------------------------------------------------------------------------------
	loadProductTOC: function(product_id)
	{
		if(isNaN(product_id))
			return;
		
		var product_toc = $("product_toc_" + product_id);
		if(product_toc.className.length == 0)
			return;
			
		var params = new Object();
		params.product_id = product_id;
		new Ajax.Request("custom/modules/product/product_ajax.cfc?method=renderProductDetailTOC", {
			method: 'post',
			parameters : params,
			onSuccess: function(transport) {
				var obj = transport.responseText.evalJSON();
				
				var product_toc = $("product_toc_" + obj.ID);
				product_toc.className = "";
			
				if(obj.UPDATE == true)
					product_toc.innerHTML = obj.HTML;
			}
		});
	},
	
	//---------------------------------------------------------------------------------------------------------------
	loadServiceDownloads: function(product_id)
	{
		if(isNaN(product_id))
			return;
		
		var service_downloads = $("product_service_downloads");
		if(service_downloads.className.length == 0)
			return;
			
		var params = new Object();
		params.product_id = product_id;
		new Ajax.Request("custom/modules/product/product_ajax.cfc?method=renderProductServiceDownloads", {
			method: 'post',
			parameters : params,
			onSuccess: function(transport) {
				var obj = transport.responseText.evalJSON();
				
				service_downloads.className = "";
			
				if(obj.UPDATE == true)
					service_downloads.innerHTML = obj.HTML;
			}
		});
	},
		
	//---------------------------------------------------------------------------------------------------------------
	productFilterChanged: function()
	{	
		this.getProductsByFilter(this.active_filter);
	},
	
	//---------------------------------------------------------------------------------------------------------------
	changeSortMode: function(mode)
	{	
		if(isNaN(mode))
			return;
			
		if(this.active_sort_mode == mode)
			return;
					
		this.active_sort_mode = mode;
		this.getProductsByFilter(this.active_filter);
	},
	
	//---------------------------------------------------------------------------------------------------------------
	changeViewMode: function(mode)
	{	
		if(isNaN(mode))
			return;
			
		if(this.active_view_mode == mode)
			return;
			
		if(mode)
		{
			$("btn_list").className = "btn-list-active";
			$("btn_raster").className = "btn-raster-inactive";
		}
		else
		{
			$("btn_list").className = "btn-list-inactive";
			$("btn_raster").className = "btn-raster-active";
		}
			
		this.active_view_mode = mode;
		this.getProductsByFilter(this.active_filter);
		
		return false;
	},
	
	//---------------------------------------------------------------------------------------------------------------
	changeExternalProductFilter: function(external_product_id)
	{
		if(external_product_id == this.active_external_product_id)
			return;
			
		this.active_external_product_id = external_product_id;
		Videos.active_external_product_id = external_product_id;
		
		for(var i = 0; i < this.tabs.length; i++)
		{
			this.filter_status[this.tabs[i]] = false;	
		}
		
		this.categoryFilterChanged();
	},
	
	//---------------------------------------------------------------------------------------------------------------
	categoryFilterChanged: function()
	{	
		if(this.active_tab_id == 1)
			this.productFilterChanged();
		else if(this.active_tab_id == 2)
			Videos.videoFilterChanged();
			
		this.filter_status[this.active_tab_id] = true;	
	},
	
	//---------------------------------------------------------------------------------------------------------------
	getProductsByFilter: function(filter)
	{
		$("products_content").className = "ajax-category-loading";
				
		var params = new Object();
		params.view_mode = this.active_view_mode == 0 ? "0" : this.active_view_mode;
		params.sort_mode = this.active_sort_mode == 0 ? "0" : this.active_sort_mode;
		params.product_category_id = this.active_category_id;
		params.external_product_id = this.active_external_product_id;
		
		new Ajax.Request("custom/modules/product/product_ajax.cfc?method=getProductsByFilter", {
			method: 'post',
			parameters : params,
			onSuccess: function(transport) {
				var obj = transport.responseText.evalJSON();
				
				$("products_content").className = "";
			
				if(obj.records_left == "NO")
					$("show_more").style.display = "none";
				else
					$("show_more").style.display = "block";
			
				if(obj.update == true)
				{
					$("products_content").innerHTML = obj.html;
					$("info_count").innerHTML = obj.info_count_text;
				}
				
				Product.current_page_offset = 0;
				Favourites.prepareLinks();
				
				InfoWindow.hideInfoWindow();
				Product.prepareOverlay();
			}
		});
	},

	//---------------------------------------------------------------------------------------------------------------
	showMoreProducts: function()
	{	
		this.current_page_offset += this.page_limit;
		this.getMoreProductsByFilter(this.active_filter);	
	},
	
	//---------------------------------------------------------------------------------------------------------------
	getMoreProductsByFilter: function(filter, override_offset, override_limit)
	{
		$("load_more_ajax").style.display = "block";
		
		var params = new Object();
		params.view_mode = this.active_view_mode == 0 ? "0" : this.active_view_mode;
		params.sort_mode = this.active_sort_mode == 0 ? "0" : this.active_sort_mode;
		params.offset = this.current_page_offset == 0 ? "0" : this.current_page_offset;
		params.limit = this.page_limit;
		params.product_category_id = this.active_category_id;
		params.external_product_id = this.active_external_product_id;
		
		if(!isNaN(override_offset))
			params.offset = override_offset;
		if(!isNaN(override_limit))
			params.limit = override_limit;
		
		new Ajax.Request("custom/modules/product/product_ajax.cfc?method=getProductsByFilter", {
			method: 'post',
			parameters : params,
			onSuccess: function(transport) {
				var obj = transport.responseText.evalJSON();
	
				$("load_more_ajax").style.display = "none";
			
				if(obj.records_left == "NO")
					$("show_more").style.display = "none";
				else
					$("show_more").style.display = "block";
					
			
				if(obj.update == true)
				{
					$("products_content").innerHTML += obj.html;
					$("info_count").innerHTML = obj.info_count_text;
				}
				
				Favourites.prepareLinks();
				InfoWindow.hideInfoWindow();		
				Product.prepareOverlay();
				
				SWFAddress.setValue("?o=" + Product.current_page_offset);
			}
		});
	},

	//---------------------------------------------------------------------------------------------------------------
	searchProductByISBN: function(isbn)
	{
		if(!String(isbn).length)
			return;
			
		$("search-product-result").style.display = "block";	
		$("search-product-result-list").innerHTML = "<div class='ajax-loading-no-padding'></div";	
			
		var params = new Object();
		params.isbn = isbn;
		
		new Ajax.Request("custom/modules/product/product_ajax.cfc?method=getProductsByISBN", {
			method: 'post',
			parameters : params,
			onSuccess: function(transport) {
				var obj = transport.responseText.evalJSON();
				
				if(obj.LOGGED_IN)
					$("search-product-result-list").innerHTML = obj.HTML;
				else
					window.location = "user-login.htm";
			}
		});
	},
	
	//---------------------------------------------------------------------------------------------------------------
	registerISBNProduct: function(product_id)
	{
		if(isNaN(product_id))
			return;
			
		$("product-register-isbn-" + product_id).innerHTML = "<div class='ajax-loading-button'></div";	
			
		var params = new Object();
		params.product_id = product_id;
		
		new Ajax.Request("custom/modules/product/product_ajax.cfc?method=registerISBNProduct", {
			method: 'post',
			parameters : params,
			onSuccess: function(transport) {
				var obj = transport.responseText.evalJSON();
				
				if(obj.LOGGED_IN)
				{
					$("product-register-isbn-" + obj.product_id).innerHTML = obj.HTML;
					$("register-product-progress-area").show();
					$("register-product-progress-list").innerHTML = obj.PROGRESSHTML;
				}
				else
					window.location = "user-login.htm";
			}
		});
	},
	
	//---------------------------------------------------------------------------------------------------------------
	categoryCheckForUpdate: function(id)
	{
		if(!this.filter_status[id])
		{
			this.categoryFilterChanged();
		}
	},
	
	//---------------------------------------------------------------------------------------------------------------
	prepareOverlay: function()
	{		
		$$("a.overlay-buy-html-2").each(function(element) { 		
			new FancyZoom(element, {width: 526, height: 550, finish_function:Product.showBuyingOptions, finish_function_arguments: String(element.parentNode.id).substr(String(element.parentNode.id).lastIndexOf("-") + 1)});
		});
		$$("a.overlay-buy-html-3").each(function(element) { 		
			new FancyZoom(element, {width: 790, height: 550, finish_function:Product.showBuyingOptions, finish_function_arguments: String(element.parentNode.id).substr(String(element.parentNode.id).lastIndexOf("-") + 1)});
		});
	},
		
	//---------------------------------------------------------------------------------------------------------------
	showBuyingOptions: function(product_id)
	{
		 Product.getBuyingOptions(product_id);	
	},
	
	//---------------------------------------------------------------------------------------------------------------	
	getBuyingOptions: function(product_id)
	{					
		
		var params = new Object();
		params.product_id = product_id;
								
		new Ajax.Request("custom/modules/product/product_ajax.cfc?method=renderProductBuyingOptions", {
			parameters: params,
			onSuccess: function(transport) {
				var json = transport.responseText.evalJSON();
				jQuery("#zoom_content > .buying-options-overlay").html(json.window_content);
			}
			
		});
	},
	
	//---------------------------------------------------------------------------------------------------------------	
	getProductDownloads: function(product_ids, show_lightbox, show_toc)
	{					
		var params = new Object();
		params.product_ids = product_ids;
		params.toc_visible = show_toc;
								
		new Ajax.Request("custom/modules/product/product_ajax.cfc?method=renderProductDetailDownloads", {
			parameters: params,
			onSuccess: function(transport) {	
				var json = transport.responseText.evalJSON();

				if(show_lightbox)
				{
					jQuery(".product-downloads").html(json.html);

					var box = new FancyZoom("downloads-available", {width: 535, height: 100});						
					box.showAuto($("downloads-available"), 0);	
				}
				else
				{
					jQuery("#zoom_content > .product-downloads").html(json.html);
				}
			}
		});
	},
	
	//---------------------------------------------------------------------------------------------------------------	
	showProductDownloads:function(product_id)
	{
		Product.getProductDownloads(product_id, false, false); 
	}	
}


















