if(typeof NICK == "undefined" || !NICK) var NICK = {};

NICK.namespace("listings");

// Sort List Utility

$(document).ready(function() {
	$("div.seo-filter-list").hide();
	$("select").each(function (i){
		this.selectedIndex = 0;
	})
});	

NICK.listings.sortShow = "";
NICK.listings.sortType = "";
NICK.listings.overrideURI = "";
NICK.listings.selectedOption = "";
NICK.listings.selectedDropdown = -1;

NICK.listings.getData = function(urlAlias, sortVar, start, page){
	sortVar = sortVar == null || sortVar == "" ? "date desc" : sortVar;

	//NICK.utils.doLog('urlAlias: '+urlAlias);
	//NICK.listings.urlAlias = urlAlias;

	/*$(".filter-target")
		.height(($(".filter-target").height() || "auto"))
		.empty()
		.append("<div class='tabAjaxLoader' />");*/

	var params = {
	 	'sort':unescape(sortVar),
	 	'start': start,
		'page': page == null ? 1 : page
	 }; 
	NICK.request.doRequest({
		dataType:"html",
		url: "/ajax" + urlAlias,
		data: params,
		onSuccess: function(response) {
			var filterTarget = $("div.filter-target");
			filterTarget.height("auto").html(response);
						
			if(filterTarget.children('.carousel-container').children('.carousel').find("li").size() < 4){
				filterTarget.addClass('single');
			}			
			
			$('.turnintodropdown').each(function(){
				this.selectedIndex = NICK.listings.selectedIndex;
			});
			
			NICK.utils.initTooltip();
			if(NICK.video!=undefined) NICK.video.initVideoPlayIcon();
		},
		onFail: function(errors) {
			for(var error in errors) {
				NICK.utils.doLog("Sort Response: Error: "+error+" - "+errors[error]);
			}
		}				
	});
}

NICK.listings.selectDropdownFilter = function( dropdown ){
	var uri = NICK.get("uri");
	uri = (uri.lastIndexOf("/") == uri.length)? uri.substring(0,uri.length-1) : uri;
	var selectedOption = $(dropdown).val();
	var typeFilter = NICK.listings.overrideURI = uri +"/"+ selectedOption;
	/*If there are more than one dropdown, and they are selected, concat their result */
	if($("#dropdown-by-show").length != 0 && $("#dropdown-by-type").length != 0){
		var showDropVal = $("#dropdown-by-show option:selected").val();
		var typeDropVal = $("#dropdown-by-type option:selected").val();
		if(showDropVal != "" && typeDropVal != ""){
			selectedOption = showDropVal + typeDropVal;
			typeFilter = NICK.listings.overrideURI = "/"+NICK.get("type")+"s/"+ selectedOption; 
		}
	}
	NICK.listings.getData( NICK.listings.overrideURI ,"", "0")
	/*$("select").each(function (i){
		if(this.id != dropdown.id){
			this.selectedIndex = 0;
		}
	})*/
	NICK.listings.selectedIndex = dropdown.selectedIndex;
	NICK.listings.selectedOption = dropdown.options[dropdown.selectedIndex].value;
	NICK.listings.selectedDropdown = $('.turnintodropdown').index(dropdown);
}


//This maybe global drop down function in the future. need to be tested on games before merging
NICK.listings.videoDropdownFilter = function( dropdown ,params){
	var uri = NICK.get("uri");
	uri = (uri.lastIndexOf("/") == uri.length)? uri.substring(0,uri.length-1) : uri;
	NICK.listings.selectedOption = "/"+$(dropdown).val();
	var counter =0;
	var strParams ="";
	for(j in params){
		switch(counter){
			case 0:
				strParams+="?"+j+"="+params[j];
				break;
			default:
				strParams+="&"+j+"="+params[j];
				break;
		}
		counter++;
	}
	//var typeFilter = (NICK.listings.filterType == "undefined")?"" : "&type="+NICK.listings.filterType;
	NICK.listings.overrideURI = uri + NICK.listings.selectedOption+strParams;
	NICK.listings.getData( NICK.listings.overrideURI ,"", "0")
	$("select").each(function (i){
		if(this.id != dropdown.id){
			this.selectedIndex = 0;
		}
	})
	NICK.listings.selectedIndex = dropdown.selectedIndex;
	NICK.listings.selectedOption = dropdown.options[dropdown.selectedIndex].value;
	NICK.listings.selectedDropdown = $('.turnintodropdown').index(dropdown);
}

NICK.listings.initSortTab = function(tabsBrowser, i, urlAlias, params){
	var tab = $("#"+tabsBrowser +" table a").eq(i);
	var counter =0;
	for(j in params){
		switch(counter){
			case 0:
				urlAlias+="?"+j+"="+params[j];
				break;
			default:
				urlAlias+="&"+j+"="+params[j];
				break;
		}
		counter++;
	}
	
	$(tab).bind("click", function(){
		NICK.listings.selectedIndex=0;
		NICK.listings.filterType = null;
		NICK.listings.tabIndex = i;
		NICK.listings.dropDownActive = i;
		NICK.listings.selectedOption = "";
		NICK.listings.getData(urlAlias, "", 0 );
		NICK.listings.selectedDropdown = -1;
		//NICK.listings.getData(urlAlias, "featured", '0');
		return false;
	})
	
}

NICK.listings.initTabRedirect = function(tabsBrowser, i, uri){
	var tab = $("#"+tabsBrowser +" table a").eq(i);
	$(tab).bind("click", function(){
		window.location = uri;
	})
	
}

NICK.listings.getUriOverride = function() {
	var tmpURI = "";
	if(NICK.listings.overrideURI == "") {
		tmpURI = NICK.get("uri");
	} else {
		tmpURI = NICK.listings.overrideURI;
	}
	return tmpURI;
}
NICK.listings.clearUriOverride = function() {
	NICK.listings.overrideURI = "";
}

NICK.listings.getDataPages = function(sortVar, page, max, params) {
	params = params == null ? {} : params;
	if(params["view"] === undefined) params["view"] = "";
	if(params["viewType"] === undefined) params["viewType"] = "";

	var overrideUrl = NICK.listings.getUriOverride();
	overrideUrl = overrideUrl == null ? "" : overrideUrl;

	if(overrideUrl.indexOf("?") >= 0) {
		overrideUrl = overrideUrl.substring(0, overrideUrl.indexOf("?"));
	}

	var strParams = '';
	if(params != null && params != "undefined"){
		for(var j in params){
			//sc
			strParams += strParams == '' ? "?" : "&";
			strParams += j +"="+ unescape(params[j]);

			/* switch(counter){
				case 0:
					strParams+="?"+j+"="+params[j];
					break;
				default:
					strParams+="&"+j+"="+params[j];
					break;
			} */
		}
	}
	//NICK.utils.doLog(">> GDP:1: params: "+overrideUrl + strParams);
	NICK.listings.getData(overrideUrl + strParams, sortVar, page * max - max, page); //convert pages to item offset
}

NICK.listings.sortLinks = function( sortVar, params){
	params = params == null || params === 'undefined' ? {} : params;

	var strParams = '';
	for(j in params){
		strParams += strParams == '' ? "?" : "&";
		strParams += j +"="+ unescape(params[j]);
	}
	
	var uriPrefix = NICK.get('uri');// + 's';
	var dropdown = $('.turnintodropdown').eq(NICK.listings.selectedDropdown);
	
	// Get URI of active drop down
	var itemURI = NICK.listings.selectedDropdown != -1 ? uriPrefix + "/" + dropdown.val() : NICK.get("uri") + '/';
	
	NICK.listings.getData(itemURI + NICK.listings.selectedOption+strParams,sortVar, '0');
	NICK.pagination.setSortOverride(sortVar);
}

