var Listing = {
	
	init:function(){
		
	},
	
	filter:function(titlename,filterid,filtername,obj,url){
		//alert('here'+titlename+'|'+filterid+'|'+filtername+'|'+url);
		//console.log(obj)
		//remove the on class from any previous selection
		var tmpType = $(obj).attr('typ');
		Listing.removeOnClass(tmpType);
		//update the class of the current selection
		Listing.updateOnClass(obj,'on','add');
		switch(titlename){
			case 'Sub-Categories':
				sztmpObj = $('a[typ="Sizes"]');
				srtmpObj = $('a[typ="Sort by"]');
				subcategoryid = filterid;
				sizeid = $(sztmpObj[0]).attr('sid');
				sortid = $(srtmpObj[0]).attr('rid');
				url = url;
			  	break;
			case 'Sizes':
				tmpObj = $('a[typ="'+tmpType+'"]');
				subcategoryid = $(tmpObj[0]).attr('cid');
				sizeid = filterid;
				sortid = $(tmpObj[0]).attr('rid');
				url = url;
			  	break;
			case 'Sort by':
				tmpObj = $('a[typ="'+tmpType+'"]');
				subcategoryid = $(tmpObj[0]).attr('cid');
				sizeid = $(tmpObj[0]).attr('sid');
				sortid = filterid;
				url = url;
				break;
			default:
			  //code to be executed if n is different from case 1 and 2
		}
		/*console.log(url);
		console.log(subcategoryid);
		console.log(sizeid);
		console.log(typeof(sizeid))
		console.log(sortid);*/
		if(typeof(sizeid) === 'undefined'){
			sizeid = '';
		}
		Listing.sendToLocation(url,subcategoryid,sizeid,sortid);
	},
	
	removeOnClass:function(attrType){
		//get all the a tags with type attrType and class="on"
		var attrArray = Listing.getAllAttrOnClass(attrType);
		//loop the array and remove the class="on"
		for(i=0; i<attrArray.length; i++){
			Listing.updateOnClass(attrArray[i],'on','remove')
		}
	},
	
	updateOnClass:function(obj,status,action){
		if(action == 'add'){
			$(obj).addClass(status);	
		}else{
			$(obj).removeClass(status);
		}
	},
	
	sendToLocation:function(url,subfilter,sizeid,sortid){
		var ajax = $.ajax({
			type: "GET",
			url: "/includes/ajax/ajx_gateway.cfm?comp=ajx_listing&method=getFilterData",
			dataType: 'json',
			data: {
				url: url,
				categoryid: subfilter,
				sizeid: sizeid,
				sortid: sortid
			},
			success: function(r, status){
				//console.log(r);
				var locationURL = r.rURL;
				location.href = locationURL;
			}
		});
	},
	
	getAllAttrOnClass:function(attrType){
		return $('a[typ="'+attrType+'"][class="on"]');
	},
	
	toggleHover:function(obj){
		$(obj).hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		)
	}
}

