$(document).ready(function() {
	loading = '<center><img src="/images/navigation/loading.gif"><br></center>';
	//Parse query string
	url = location.search;
//	qsid = '#' + $.query.get('sid');
	qmid = $.query.get('mid');
	qtid = $.query.get('tid');
	
	//Added this for vert
	buildResourceList(qmid,qtid);
	
//	if(qsid != '#'){
//		selectSize(qsid);
//	}
		
	//Click to select function for sizes
//	$(".dlsize").click(function() {
//		qsid = '';
//		qmid = '';
//		qtid = '';
//		selectSize(this);
//	});

	function selectSize(thisid){
		//Dont need this function
		$("#biz-sm").css("background-image","url(/images/resources/biz-sm.gif)");
		$("#biz-md").css("background-image","url(/images/resources/biz-md.gif)");
		$("#biz-lg").css("background-image","url(/images/resources/biz-lg.gif)");
		$(".dlsize").removeClass("selectedsize");
		var imgidon = 'url(/images/resources/' + $(thisid).attr('id') + '-on.gif)';
		$(thisid).css("background-image",imgidon);
		$(thisid).addClass("selectedsize");
		switchList(thisid);
		getModules(thisid);
	}
	
	function getModules(thisid,tid){
		//Dont need this function
		resetPage(0);
		$(".dltopic").css("background-image","");
		$(".dltopic").removeClass("selected");
		getResourceList(qmid,qtid);
	}

	function getResourceList(thisid,tid){
		//Dont need this function
		//Hover function for topics
		$(".dltopic").hover(function() {
			$(this).css("background-image","url(/images/resources/topicbg.gif)");
		},function(){
			if(!$(this).hasClass("selected")){
				$(this).css("background-image","");
			}
		});

		//Toggle module, concepts, and verticals on and off fuction
		$(".dltopic").click(function() {
			resetPage(1);
			buildResourceList(this,'')
		});
		if(qmid){
			resetPage(1);
			buildResourceList(thisid,tid)
		}
	}
	
	function buildResourceList(thisid,tid){
//		$(".dltopic").css("background-image","");
//		$(".dltopic").removeClass("selected");
//		$(thisid).css("background-image","url(/images/resources/topicbg.gif)");
//		$(thisid).addClass("selected");
				
		//Get size tag id and topic tag id
//		var sizeid = $('.selectedsize > .biztxt').attr('id');
//		var tagid = $(thisid).attr('id');

		//Added for verticals
		var tagid = thisid;
				
		//Send tagid and sizeid and return resources
		$.post("/includes/dlresults-vert.php", {tagid: tagid}, function(xml) {
			var count = 0;
			doct=0,audiot=0,videot=0,eventt=0;
			resetPage();
			$("element", xml).each(function(){
				ecount = $("count", this).text();
				ecount = parseInt(ecount);
			});
			$("resource", xml).each(function(){
				var resourcename = $("resourcetype", this).text();
				var typeid = $("typeid", this).text();
				var listing = $("listing", this).text();
				var listing = htmlDecode(listing);
				if(resourcename === "noresult"){
					$("#noresult").html("");
					$("#noresult").append(listing);
					$("#noresult").css("display","block");
				} else {
					$("#resultstabs").css("display","block");
					count++;
					createResultSet(resourcename,typeid,listing,count,ecount,tid);
				}
			});	
		});
	}
	
	function createResultSet(resourcename,typeid,listing,count,ecount,tid){
		if(typeid == 1){
			spanclass = 'pdfdoc';
			linktitle = 'SuccessFactors Document Download';
			resourceType = 'doc';
			doct = 1;
		} else if(typeid == 2){
			spanclass = 'audiofile';
			linktitle = 'SuccessFactors Audio Content';
			resourceType = 'audio';
			audiot = 1;
		} else if(typeid == 3){
			spanclass = 'vidclip';
			linktitle = 'SuccessFactors Video Content';
			resourceType = 'video';
			videot = 1;
		} else if(typeid == 4){
			spanclass = 'eventlisting';
			linktitle = 'SuccessFactors Events';
			resourceType = 'event';
			eventt = 1;
		}
		var result = "#" + resourceType + "types";
		$(result).append(listing);
		setOpenTab(resourceType,tid);
		if(count == ecount){
			setOpenTab('final',tid);
			//Initialize thickbox
			tb_init('a.thickbox');	
		}
	}
	
	function setOpenTab(resourceType,tid){		
		if(resourceType != 'final'){
			var resultshow = "#" + resourceType + ".resultset";
			var tabstyle = "#" + resourceType + "tab";
			var tab = "#" + resourceType + "tab.tab";
			$(tab).css("display","block");
		} else {
			if(tid > 0){
				if(tid === 1){
					resourceType = 'doc';
				} else if(tid === 2){
					resourceType = 'audio';
				} else if(tid === 3){
					resourceType = 'video';
				} else if(tid === 4){
					resourceType = 'event';
				}
			} else {
				if(doct == 1){
					resourceType = 'doc';
				} else if(audiot == 1){
					resourceType = 'audio';
				} else if(videot == 1){
					resourceType = 'video';
				} else if(eventt == 1){
					resourceType = 'event';
				}
			}
			var resultshow = "#" + resourceType + ".resultset";
			var tabstyle = "#" + resourceType + "tab";
			var tab = "#" + resourceType + "tab.tab";
			$("#linker").css("display","block");
			$("#noresult").css("display","none");
			$(".resultset").css("display","none");
			$(resultshow).css("display","block");
			$(tabstyle).removeClass("off");
			$(tabstyle).addClass("on");
			$(tab).css("display","block");
		}
	}
	
	function htmlDecode(nstring) {
		var dstring = nstring.replace(/\&gt\;/g, '\>');
		dstring = dstring.replace(/\&lt\;/g, '\<');
		return(dstring);
	}
	
	function resetPage(type){
		//Clear current results
		$("#doctypes").html("");
		$("#audiotypes").html("");
		$("#videotypes").html("");
		$("#eventtypes").html("");
		$("#noresult").html("");
					
		//Hide results windows
		$(".resultset").css("display","none");
					
		//Hide window tabs
		$("#resultstabs .tab").css("display","none");
		
		if(type == 1){
			$("#noresult").html("");
			$("#noresult").css("display","block");
			$("#noresult").append(loading);
		}
						
		//Set all tabs to off state
		if($(".tab").hasClass("on")){
			$(".tab").removeClass("on");
			$(".tab").addClass("off");
		}
		
		$("#linker").css("display","none");				
	}
	
	function switchList(thisid){
		var modlist = '#list' + $(thisid).attr('name') + '.modlist';
		$(".listbox").css("display","block");
		$(".modlist").css("display","none");
		$(modlist).css("display","block");
	}
	
	//Select result tab function
	$(".tab").click(function() {
		if(!$(this).hasClass("on")){
			//Switch Tabs
			$(".tab").removeClass("on");
			$(".tab").addClass("off");
			$(this).removeClass("off");
			$(this).addClass("on");

			//Switch Results
			var result = "#" + $(this).attr("title");
			$(".resultset").css("display","none");
			$(result).css("display","block");
		}
	});
					
	//Hover function for sizes
	$(".dlsize").hover(function() {
		var imgidon = 'url(/images/resources/' + $(this).attr('id') + '-on.gif)';
		$(this).css("background-image",imgidon);
	},function(){
		var imgid = 'url(/images/resources/' + $(this).attr('id') + '.gif)';
		if(!$(this).hasClass("selectedsize")){
			$(this).css("background-image",imgid);
		}
	});
	
	//Get link code
	$(".getlink").click(function() {
//		var size = $(".dlsize.selectedsize").attr('id');
//		if(size == undefined){
//			var querysize = '';
//		} else {
//			var querysize = "?sid=" + size;
//		}
//		var topic = $(".dltopic.selected").attr('id');
		var topic = $.query.get('mid');
		if(topic == undefined){
			var querytopic  = '';
		} else {
			var querytopic = "?mid=" + topic;
		}
		var tabs = $(".on").attr('id');
		if(tabs == undefined){
			var querytabs = '';
		} else {
			if(tabs == 'doctab'){
				id2 = 1;
			} else if(tabs == 'audiotab'){
				id2 = 2;
			} else if(tabs == 'videotab'){
				id2 = 3;
			} else if(tabs == 'eventtab'){
				id2 = 4;
			}
			var querytabs = "&tid=" + id2;
		}
		var strHref = new String(window.location);
   		var strHref = strHref.split('?');
		$("#getlinkwin").css("display","block");
		$("#link").val(strHref[0] + querytopic + querytabs);
		$("#link").focus();
		$("#link").select();
	});	
});
function closewin(){
	document.getElementById("getlinkwin").style.display = "none";
}