$(document).ready(function(){	
	if($('#categorylevel1').val()!='')
	{
		
		$('#cat_'+$('#categorylevel1').val()).addClass('active');	
		$('#tdcategory').html('<h1>'+$('#cat_'+$('#categorylevel1').val()).text()+'</h1>');
		GetDefaultLevel2();
	}
	else
	{
		ShowProduct('',1,'agallery');
		$('#tdcategory').html('<h1>All Products</h1>');
	}
	
	$("#level-three").css("display", "none");
	
	var srch_options = {
        beforeSubmit:  srch_Request,
        success:       srch_Response,        
        url:           'admin/controller/product_controller.php?CMD=product&level=1&op=search', 
        type:          'post',                  
        timeout:       3000
    };


   /*$('#frm_search').submit(function() {
           $(this).ajaxSubmit(srch_options);           
           return true;
    });*/ 
	   
	function srch_Request(formData, jqForm, f_options) 
	{
		var queryString = $.param(formData);
		return true;
	}
	 
	function srch_Response(responseText, statusText)  
	{ 		
		if(responseText)
		{//alert('in search response');
			$("#viewtype").html(responseText);
		}
		else
		{
			alert('Request Failed!');
		}	
		
	}	
});

function GetDefaultLevel2()
{
	$.get("admin/controller/category_controller.php", {CMD: "level2", tablename: 'category',categoryid:$('#categorylevel1').val(),productid:$('#productid').val()},ShowDefaultLevel2);		
}

function ShowDefaultLevel2(data)
{
	//alert(data);
	$("#level-two").html(data);
	if($('#categorylevel2').val()!='')
	{
		$('#tdcategory').html('<h1>'+$('#level2_'+$('#categorylevel2').val()).text()+': </h1>');
		$("#level-two > ul > li > a").removeClass('active');	
		$('#level2_'+$('#categorylevel2').val()).addClass('active');
		if($('#productid').val()==''){
			ShowProduct($('#categorylevel2').val(),2,'agallery');
		}
		GetDefaultLevel3($('#categorylevel2').val());
	}
	else
	{
		ShowProduct($('#categorylevel1').val(),1,'agallery');	
	}
}

function GetDefaultLevel3(level2link)
{
	//alert(level2link);
	$("#level-two > ul > li > a").removeClass('active');	
	$('#level2_'+level2link).addClass('active');	
	$('#tdcategory').html('<h1>'+$('#level2_'+level2link).text()+'&raquo; </h1><span class="bluetxt" id="spancategory" name="spancategory"></span>');	
	$.get("admin/controller/category_controller.php", {CMD: "level3", tablename: 'category',categoryid:level2link,productid:$('#productid').val()},ShowDefaultLevel3);		
}

function ShowDefaultLevel3(data)
{
	$("#level-three").html(data);
	$('#level3_'+$('#categorylevel3').val()).addClass('active');
	if($('#categorylevel3').val()!='')
	{	
		$("#level-three").css("display", "block");
		if($('#productid').val()==''){
		
		GetProduct($('#categorylevel3').val());
		}
	}
}

function GetLevel3(level2link)
{
	//alert(level2link);
	$("#level-two > ul > li > a").removeClass('active');	
	$('#level2_'+level2link).addClass('active');	
	$('#tdcategory').html('<h1>'+$('#level2_'+level2link).text()+'&raquo; </h1><span class="bluetxt" id="spancategory" name="spancategory"></span>');	
	$.get("admin/controller/category_controller.php", {CMD: "level3", tablename: 'category',categoryid:level2link,productid:$('#productid').val()},ShowLevel3);		
	ShowProduct(level2link,2,'agallery');
}

function ShowLevel3(data)
{
	//alert(data);
	if(data != '<ul></ul>')
	{
		$("#level-three").css("display", "block");
	}
	else
	{
		$("#level-three").css("display", "none");
	}
	
	$("#level-three").html(data);	
}

function GetProduct(level3link)
{
	$("#level-three > ul > li > a").removeClass('active');
	$('#level3_'+level3link).addClass('active');
	$('#spancategory').html($('#level3_'+level3link).text());
	//alert($('#spancategory').html());
	ShowProduct(level3link,3,'agallery');
}

function ShowProduct(id,lvl,vw_typ)
{
	//alert(id);
	$("#load_img").html("<img src='images/loading.gif'>");
	$("#categoryid").val(id);
	$("#level").val(lvl);
	$("#tdview > div > a").removeClass("active");
	$("#"+vw_typ).addClass("active");
	$.get("admin/controller/product_controller.php",{CMD:'product', categoryid:id, level:lvl, view_type:vw_typ},ShowLevelcat);
}

function SwitchView(vw_typ)
{
	ShowProduct($("#categoryid").val(),$("#level").val(),vw_typ);
}

function ShowLevelcat(data)
{
	$("#viewtype").html(data);	
	$("#load_img").html("");
}


function  showprdct(c_id,pageno,count,lvl,vw_typ)
{
	$("#load_img").html("<img src='images/loading.gif'>");
	$.get("admin/controller/product_controller.php", {CMD: "product", categoryid:c_id, level:lvl, pageno :pageno,count:count, view_type:vw_typ},ShowPrdct);	
}

function  showprdct_alt(pageno,count,lvl,vw_typ,is_search,featprd)
{
	$("#load_img").html("<img src='images/loading.gif'>");
	$.get("admin/controller/product_controller.php", {CMD: "product", categoryid:'', level:lvl, pageno :pageno,count:count, view_type:vw_typ, srch_txt:is_search, feat_prd:featprd},ShowPrdct);	
}


function ShowPrdct(data)
{	
	$("#load_img").html("");
	$("#viewtype").html(data);
}

