// JavaScript Document
/*
	Class: CustomPages, 2008 August 05
*/
CustomPages = function()
{
	this.site_url = null;
	this.category_name	= null;
	this.processorUrl = null;
	this.linkTo = null;
	
	/*Start Category Section*/
	this.getCategoryName_success = function(o)
	{
		this.linkTo = YAHOO.lang.JSON.parse(o.responseText); 
		var links = YAHOO.util.Selector.query('p a.dinamicCategoryLink','cpContainer');
		var w = links.length;
		for(j=0;j<w;j++)
		{
			links[j].setAttribute('href', this.linkTo[j]); 
		}

	};
	
	this.getCategoryName_failure = function()
	{
		alert("Error in getCategoryName_failure");	
	};
	
	this.getCategoryName = function(category_name)
	{
		//alert("this.getCategoryName");
		this.category_name = category_name;
		//alert(this.category_name);
		var callBack=
		{
			success: this.getCategoryName_success,
			failure: this.getCategoryName_failure,
			scope: this
		};				
		var postData = "action=getCategoryName&category_name="+this.category_name+"&site_url="+this.site_url;
		YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, callBack, postData);	
	};
	/*End Category Section*/
	
	
	/*
	Start section products
	*/	
	this.getProductName_success = function(o)
	{
		this.linkTo = YAHOO.lang.JSON.parse(o.responseText); 
		var links = YAHOO.util.Selector.query('p a.dinamicProductLink','cpContainer');
		var w = links.length;
		for(j=0;j<w;j++)
		{
			links[j].setAttribute('href', this.linkTo[j]); 
		}
	};
	
	this.getProductName_failure = function()
	{
		alert("Error in getProductName_failure");	
	};
	
	this.getProductName = function(product_name)
	{
		this.product_name = product_name;
		//alert(this.category_name);
		var callBack=
		{
			success: this.getProductName_success,
			failure: this.getProductName_failure,
			scope: this
		};				
		var postData = "action=getProductName&product_name="+this.product_name+"&site_url="+this.site_url;
		YAHOO.util.Connect.asyncRequest('POST', this.processorUrl, callBack, postData);	
	};
	/*
	End section products
	*/
	
	
	this.init = function(site_url)
	{
		this.site_url = site_url;//http://devx.wheretobuyroses.com
		this.processorUrl = this.site_url+"/model/custom_MDL.php";

		var categoryLinks = YAHOO.util.Selector.query('p a.dinamicCategoryLink','cpContainer');
		var productLinks = YAHOO.util.Selector.query('p a.dinamicProductLink','cpContainer');
		
		var k = categoryLinks.length;
		var l = productLinks.length;
		//alert("k:"+k+"; l:"+l);		
		this.categoryTitles = [];
		this.productTitles = [];

		for(i=0; i<k; i++)
		{
			this.categoryTitles[i] = categoryLinks[i].getAttribute("title");
		}
		//alert(this.categoryTitles);
		this.getCategoryName(this.categoryTitles);
		
		for(i=0;i<l;i++)
		{
			this.productTitles[i] = productLinks[i].getAttribute("title");
		}		
		this.getProductName(this.productTitles);
		
	};
};
