/**
 * This JavaScript file used to load the Nettrekker Home page
 */
var ENGLISHLANG = "102509";
var _21CENTURY = "604060";
var CURRENT = "166074";
var TEACHERTOOLS = "189967";

var NTHomeInterface = function(homepage, al, channel, page){
    this.homepage = homepage;
    this.al = al;
    this.channel = channel;
    this.page = page;  
    this.pageView = SUBJECT_ALL;
    this.nodeArray = new Array();
}

NTHomeInterface.prototype = {

    init: function(){
		if(channel == "all" || channel=="ell" || channel=="21c"){
        	// Load the home page.
			this.loadHomePage.bind(this).call();
		}
    },
    
    loadHomePage: function(){
		$("#id_sub_indicator").show();
        var object = this;
		var maxLevels =2;
		 if (this.channel == "ell" && this.page != 'pref') {
			 maxLevels = 3;
		 }
		 var level = this.al;
		 if(level == '') {
		 	level = 'High';
		 }
        // Make ajax call to goSubject.
		$.ajax({
            url: '/goSubject?nodeID=&al=' + level + '&np=/GoSubject.json&maxSites=0&maxLevels=' + maxLevels,
            type: 'get',
            dataType: 'json',
            timeout: 30000,
            success: function(data){
				var j =0;
				
				$.each(data.nodesMap, function(index, value) {
					object.nodeArray[j] = value;
					j++;
				});
				object.nodeArray.sort(function(a, b){        
		        	if(parseInt(a.order)>parseInt(b.order)){
		                return 1;
		            }
		            else if (parseInt(a.order)==parseInt(b.order)){
		                return 0;
		            }
		            else {
		                return -1;
		            }
	
		        });				
				if(object.page == 'home') {
					// Process the Nodes for Display.
	                object.processCatogoryNodes.bind(object, data.nodesMap).call();
				} else {
					object.processPrefNodes.bind(object, data.nodesMap).call();
				}
            }
        });
    },
    
    processPrefNodes: function(nodesMap) {
    	var displayNodeList = this.pageView.split("\|");    	
        var $this = this;
        var tempArr = [];
        var cssLeft = '';
        var cssRight = '';
        var label = '';
        if(subWidgetOrder != '') {
        	tempArr = subWidgetOrder.split("");
        }
        $.each($this.nodeArray, function(index, node){
            if ($.inArray(node.code, displayNodeList) != -1) {
            	if(node) {
            		$this.addSubjectPreference(node.code, tempArr, node.title);
                }
            }  
        });   
		$this.addSubjectPreference("REFER", tempArr);
		
    	$('.pref_sub_btn_toggle').click(function(){
    		if ($(this).attr("class").indexOf("btn_secondary_slideright") != -1) {
    			$(this).removeClass("btn_secondary_slideright").addClass("btn_tertiary_slideright");
    			$(' >span', this).removeClass("btn_secondary_slideleft").addClass("btn_tertiary_slideleft").html('+ Add');
            } else {
            	$(this).removeClass("btn_tertiary_slideright").addClass("btn_secondary_slideright");
    			$(' >span', this).removeClass("btn_tertiary_slideleft").addClass("btn_secondary_slideleft").html('- Remove');
            }
    		var suborder = $('#id_preferece_frm input:hidden[name=pref_value_3]').val().replace("Z", "").replace("X", "");
    		// ponnu
    		// var suborder = '';
    		var subWidgetId;;
    		$('.pref_sub_btn_toggle').each(function() {
    			if($(this).hasClass('btn_secondary_slideright')) {
    				// on clicking add button
    				// suborder += subjectPref1Map[$(this).attr('nodecode')];
    				subWidgetId = subjectPref1Map[$(this).attr('nodecode')];
    				if(suborder.indexOf(subWidgetId) == -1) {
    					suborder += subWidgetId; 
    				}
    			} else if($(this).hasClass('btn_tertiary_slideright')) {
    				// on clicking remove button
    				subWidgetId = subjectPref1Map[$(this).attr('nodecode')];
    				if(suborder.indexOf(subWidgetId) != -1) {
						suborder = suborder.substr(0, suborder.indexOf(subWidgetId)) + suborder.substr(suborder.indexOf(subWidgetId) + 1);
    				}
    			}
    		})
    		if(suborder == '') {
    			suborder = 'Z';
    		}
    		$('#id_preferece_frm input:hidden[name=pref_value_3]').val(suborder);
    	});
    	
    	$('.pref_nav_btn_toggle').click(function(){
    		if ($(this).attr("class").indexOf("btn_secondary_slideright") != -1) {
    			$(this).removeClass("btn_secondary_slideright").addClass("btn_tertiary_slideright");
    			$(' >span', this).removeClass("btn_secondary_slideleft").addClass("btn_tertiary_slideleft").html('+ Add');
            } else {
            	$(this).removeClass("btn_tertiary_slideright").addClass("btn_secondary_slideright");
    			$(' >span', this).removeClass("btn_tertiary_slideleft").addClass("btn_secondary_slideleft").html('- Remove');
            }
    		var navorder = $('#id_preferece_frm input:hidden[name=pref_value_4]').val().replace("Z", "").replace("X", "");
    		$('.pref_nav_btn_toggle').each(function() {
    			if ($(this).hasClass('btn_secondary_slideright')) {
					var navId = $(this).attr('navorder') + '2';					
					if(navorder.indexOf(navId) == -1) {
    					navorder += navId; 
    				} 
				} else if ($(this).hasClass('btn_tertiary_slideright')) {
					var navId = $(this).attr('navorder') + '2';
					// code addded for the condition --when the widget is minimized
					var navIdMin = $(this).attr('navorder') + '1';
					if (navorder.indexOf(navId) != -1) {						
						navorder = navorder.substr(0, navorder.indexOf(navId)) + navorder.substr(navorder.indexOf(navId) + 2);
					} 
					if (navorder.indexOf(navIdMin) != -1) {
						navorder = navorder.substr(0, navorder.indexOf(navIdMin)) + navorder.substr(navorder.indexOf(navIdMin) + 2);
					}
				}
    		})
    		if(navorder == '') {
    			navorder = 'Z';
    		}
    		$('#id_preferece_frm input:hidden[name=pref_value_4]').val(navorder);  
    	});    	    
    },
    
    addSubjectPreference: function(nodeCode, tempArr, title) {
    	if($.inArray('X', tempArr) > -1 || ($.inArray('Z', tempArr) == -1 && ($.inArray(subjectPref1Map[nodeCode], tempArr) > -1 || tempArr.length == 0))) {
			cssRight = 'btn_secondary_slideright';
			cssLeft = 'btn_secondary_slideleft';
			label = '- Remove';            			
		} else {
			cssRight = 'btn_tertiary_slideright';
			cssLeft = 'btn_tertiary_slideleft';
			label = '+ Add';
		}
     	var subjectIndex = subjectMap[nodeCode];
		if(!title ){
			title = subjectArray[subjectIndex][2];
		}      	
     	var nodesHtml = "<li><div><a href='javascript:;' class='"+cssRight+" nt_gen_noborders pref_sub_btn_toggle' nodecode='"+nodeCode+"'><span class='"+cssLeft+" nt_gen_fontMed'>"+label+"</span></a></div><div class='md_icon "+
     	subjectArray[subjectIndex][4]+"' alt='"+subjectArray[subjectIndex][1] +"...' title='"+subjectArray[subjectIndex][1]+"...'></div>"+title+"</li><div style='clear: both;'></div>";
     	$('#pref_sub_list').append(nodesHtml);
    },
    
    processCatogoryNodes: function(nodesMap){
    	var displayNodeList = [];
    	var $this = this;
    	if (subWidgetOrder != 'Z') {
			if (subWidgetOrder != '' && subWidgetOrder != 'X') {
				var tempArr = subWidgetOrder.split("");
				for (var i = 0; i < tempArr.length; i++) {
					if (this.pageView.indexOf(subjectPrefMap[tempArr[i]]) != -1) {
						displayNodeList[i] = subjectPrefMap[tempArr[i]];
					}
				}
				for (var i = 0; i < displayNodeList.length; i++) {
					if (displayNodeList[i] == 'REFER') {
						$('#hm_sub_widgets').append(referenceTools);
					}
					var node = nodesMap[displayNodeList[i]];
					if (node) {
						this.processNode.bind(this, node, '#hm_sub_widgets').call();
					}
				}
			} else {
				displayNodeList = this.pageView.split("\|");
				$.each($this.nodeArray, function(index, node){
					if ($.inArray(node.code, displayNodeList) > -1) {
						if (node) {
							$this.processNode.bind($this, node, '#hm_sub_widgets').call();
						}
					}
				});
				$('#hm_sub_widgets').append(referenceTools);
			}
		}
        if (channel == "ell") {
            var englangNode = nodesMap[ENGLISHLANG];
            if (englangNode) {
                var ellNode, multiNode;
                for (_obj in englangNode.childNodes) {
                    curChildNode = englangNode.childNodes[_obj];
                    if (curChildNode.title == "ELL Skill Development") {
                        ellNode = curChildNode;
                        var ell_link = "/goSubject?np=/subjectresults.ftl&nodeID="+ curChildNode.nodeId +"&al=" + this.al; 
                    }
                    else if (curChildNode.title == "Multicultural Pavilion") {
                        multiNode = curChildNode;
                        var multi_link = "/goSubject?np=/subjectresults.ftl&nodeID="+ curChildNode.nodeId +"&al=" + this.al; 
                    }
                    if (ellNode && multiNode) {
                        break;
                    }
                }
                $("#ell_link").attr('href', ell_link);
                $("#ell_div_link").attr('onClick', "document.location.href='" + ell_link + "'");
                $("#multi_link").attr('href', multi_link);
                $("#multi_div_link").attr('onClick', "document.location.href='" + multi_link + "'");
                
                $("#ell_skill").append(getSubjectChildren(ellNode, this.al, this.channel));
                $("#multi_Pavilion").append(getSubjectChildren(multiNode, this.al, this.channel));
            }
        }
        $("#id_sub_indicator").hide();
        if(this.channel == "21c"){
        	var twentyOneC = nodesMap[_21CENTURY];      
        	if (twentyOneC){ 			
        		set21CId(twentyOneC);	        			
        	}
        }
              
		var teacherTools = nodesMap[TEACHERTOOLS];  
		//set the Teacher Tools link in the right menu bar
		if (teacherTools){ 
			buildTeacherToolsLink(al,teacherTools.nodeId);
			setTeacherToolsId(teacherTools.nodeId);	
			
		}
		var revert = true;
		if($.browser.msie && $.browser.version == '7.0'){
			revert = false;
		}
		if (!userIsLocked) {
			$("#hm_sub_widgets").sortable({
				opacity: 0.7,
				revert: revert,
				scroll: true,
				forcePlaceholderSize: true,
				forceHelperSize: true,
				tolerance: 'pointer',
				handle: $(".hm_lrg_widget_hdr"),
				helper: 'clone',
				update: function(){
					updateSubjectPref();
				}

			});
			$('.hm_widget_close').click(function(){
				$(this).parent().parent().hide(200, function(){
					$(this).remove();
					updateSubjectPref();
				});
			});
		} else {
			$('.hm_widget_close, .hm_lrg_widget_hdr').each(function() {
				$(this).removeClass('nt_gen_cursor_hand').removeClass('nt_gen_cursor').removeClass('nt_gen_cursor_move').addClass('cs_cursortext');
			});
		}
    },
	
	processNode:function(node, container, al){
    	var subjectIndex = subjectMap[node.code];
    	var subjectBody = getSubjectChildren(node, this.al, this.channel);
    	var title = (node.title.toString().length > 22) ? (node.title.toString().substring(0, 22)+'.') :  (node.title);
    	subjectdiv = "<li class='hm_widget' nodeCode='" + node.code + "'>" +
    					"<div class='hm_lrg_widget_box floatLT'>" + 
    						"<div class='hm_widget_close nt_gen_cursor_hand'></div>" + 
    						"<h1 class='hm_lrg_widget_hdr floatLT nt_gen_cursor_move'>" + 
    						"<div class='nt_gen_cursor lg_icon pos_abs " + subjectArray[subjectIndex][0]+"' alt='"+subjectArray[subjectIndex][1] + "...' title='"+subjectArray[subjectIndex][1] + "...'" +
    								"onClick=\"document.location.href='/goSubject?np=/subjectresults.ftl&nodeID=" + node.nodeId + "&al=" + this.al + "&allWords=" + subjectArray[subjectIndex][2] + "'\"></div>" +
    						"<span class='icon_mrglt'>" + 
    						"<a class='white_label' href='/goSubject?np=/subjectresults.ftl&nodeID=" + node.nodeId + "&al=" + this.al + "&allWords=" + subjectArray[subjectIndex][2] + "'>"+
    						title + 
    						"</a>" + 
    						"</span>" +
    						"</h1>" + subjectBody +"</div></li>"
    	$(container).append(subjectdiv);
	}
}

//Subjects to display on the homepage.
//var CURRENT = "166074";
var SUBJECT_ALL = "100172|102458|102462|102461|102465|102466|102460|102464|102463|102459|166074|REFER";

var Elementary_All = "100172|102458|102462|102461|102463|102464|102460|102466|166074|102465|THEME";
var Elementary_Ell = "100172|102458|102462|102461|102463|102464|102460|102466";
var Elementary_21C = "";
var Middle_All = "100172|102458|102462|102461|102465|102466|102460|102464|102463|102459|166074|REFER";
var Middle_Ell = "100172|102458|102462|102461|102465|102466|102460|102464|102463|102459";
var Middle_21C = "";
var High_All = "100172|102458|102462|102461|102465|102466|102460|102464|102463|102459|166074|REFER";
var High_Ell = "100172|102458|102462|102461|102465|102466|102460|102464|102463";
var High_21C = "";

//Used to look-up subject index for the homepage
var subjectMap = {"102464" : "0",
				"102465" : "1",
				"102466" : "2",
				"102462" : "3",
				"102461" : "4",
				"102460" : "5",
				"100172" : "6",
				"102458" : "7",
				"189967" : "8",
				"102463" : "9",				
				"102509" : "10",
				"37841" : "11",
				"102509" : "12",
				"102459" : "13",
				"166074" : "14",
				"604060" : "15",
				"THEME"  : "16",
				"REFER"  : "17",
				"UNSPECIFIED" : "100"}

var subjectPrefMap = {	"A" : "102464",
					"B" : "102465",
					"C" : "102466",
					"D" : "102462",
					"E" : "102461",
					"F" : "102460",
					"G" : "100172",
					"H" : "102458",
					"I" : "189967",
					"J" : "102463",				
					"K" : "102509",
					"L" : "37841",
					"M" : "102509",
					"N" : "102459",
					"O" : "166074",
					"P" : "604060",
					"Q"	: "THEME",
					"S"	: "REFER"
				};

var subjectPref1Map = {	"102464" : "A",
						"102465" : "B",
						"102466" : "C",
						"102462" : "D",
						"102461" : "E",
						"102460" : "F",
						"100172" : "G",
						"102458" : "H",
						"189967" : "I",
						"102463" : "J",				
						"102509" : "K",
						"37841" : "L",
						"102509" : "M",
						"102459" : "N",
						"166074" : "O",
						"604060" : "P",
						"THEME"  : "Q",
						"REFER"  : "S"	
	};

//look-up for the down images on the subject drill-down page (using supref_value_4bject name)
var subjectDrillDownMap = {"Arts" : "0",
		"Foreign Language" : "1",
		"World Languages" : "1",
		"Health and Physical Education" : "2",
		"Language Arts" : "3",
		"Mathematics" : "4",
		"Religion" : "5",
		"Philosophy and Religion" : "5",
		"Science" : "6",
		"Social Studies" : "7",
		"Teacher Tools" : "8",	
		"Technology" : "9",		
		"ELL Skill Development" : "10",
		"Multicultural Pavilion" : "11",
		"English Language Learners" : "12",
		"Career and Technical Education" : "13",
		"Current Events" : "14",
		"21st Century Skills" : "15",
		"" : "100"}



//Array used to look-up image name and alt values
//0:Homepage image, 1:Title, 2:Alt, 3:Drill-down image
var subjectArray = new Array();
//Arts
subjectArray[0] = new Array(4);
subjectArray[0][0] = "lg_icon_art";
subjectArray[0][1] = "Arts: Music, Visual Arts, Dance...";
subjectArray[0][2] = "Arts";
subjectArray[0][3] = "arts.png";
subjectArray[0][4] = "md_icon_art";
//Foreign Language
subjectArray[1] = new Array(4);
subjectArray[1][0] = "lg_icon_worldlanguage";
subjectArray[1][1] = "World Languages: Spanish, French, German, Language...";
subjectArray[1][2] = "World Languages";
subjectArray[1][3] = "worldlang.png";
subjectArray[1][4] = "md_icon_worldlanguage";
//Health and Physical Education
subjectArray[2] = new Array(4);
subjectArray[2][0] = "lg_icon_health";
subjectArray[2][1] = "Health: Food, Disease, Movement, Sports...";
subjectArray[2][2] = "Health";
subjectArray[2][3] = "health.png";
subjectArray[2][4] = "md_icon_health";
//Language Arts
subjectArray[3] = new Array(4);
subjectArray[3][0] = "lg_icon_languagearts";
subjectArray[3][1] = "Language Arts: Reading, Writing, Spelling...";
subjectArray[3][2] = "Language Arts";
subjectArray[3][3] = "languagearts.png";
subjectArray[3][4] = "md_icon_languagearts";
//Mathematics
subjectArray[4] = new Array(4);
subjectArray[4][0] = "lg_icon_mathematics";
subjectArray[4][1] = "Mathematics: Problem-Solving, Measurement, Computation...";
subjectArray[4][2] = "Mathematics";
subjectArray[4][3] = "mathematics.png";
subjectArray[4][4] = "md_icon_mathematics";
//Religion
subjectArray[5] = new Array(4);
subjectArray[5][0] = "lg_icon_philosophy";
subjectArray[5][1] = "Religion: Christianity, Islam, Judaism...";
subjectArray[5][2] = "Religion";
subjectArray[5][3] = "religion.png";
subjectArray[5][4] = "md_icon_philosophy";
//Science
subjectArray[6] = new Array(4);
subjectArray[6][0] = "lg_icon_science";
subjectArray[6][1] = "Science: Animals, Earth Science, Space Science...";
subjectArray[6][2] = "Science";
subjectArray[6][3] = "science.png";
subjectArray[6][4] = "md_icon_science";
//Social Studies
subjectArray[7] = new Array(4);
subjectArray[7][0] = "lg_icon_socialstudies";
subjectArray[7][1] = "Social Studies: American Government, Geography, History, Canadian Social Studies...";
subjectArray[7][2] = "Social Studies";
subjectArray[7][3] = "socialstudies.png";
subjectArray[7][4] = "md_icon_socialstudies";
//Teacher Tools
subjectArray[8] = new Array(4);
subjectArray[8][0] = "lg_icon_teachertools";
subjectArray[8][1] = "Teacher Tools";
subjectArray[8][2] = "Teacher Tools";
subjectArray[8][3] = "teachertools.png";
subjectArray[8][4] = "md_icon_teachertools";
//Technology
subjectArray[9] = new Array(4);
subjectArray[9][0] = "lg_icon_technology";
subjectArray[9][1] = "Technology: Computers, Internet...";
subjectArray[9][2] = "Technology";
subjectArray[9][3] = "technology.png";
subjectArray[9][4] = "md_icon_technology";
//ELL Skill Development
subjectArray[10] = new Array(4);
subjectArray[10][0] = "lg_icon_ell";
subjectArray[10][1] = "ELL Skill Development";
subjectArray[10][2] = "ELL Skill Development";
subjectArray[10][3] = "ell.png";
subjectArray[10][4] = "md_icon_ell";
//Multicultural Pavilion
subjectArray[11] = new Array(4);
subjectArray[11][0] = "multipav.gif";
subjectArray[11][1] = "Multicultural Pavilion";
subjectArray[11][2] = "Multicultural Pavilion";
subjectArray[11][3] = "ell.png";
subjectArray[11][4] = "multipav.gif";
//English Language Learners
subjectArray[12] = new Array(4);
subjectArray[12][0] = "lg_icon_ell";
subjectArray[12][1] = "English Language Learners";
subjectArray[12][2] = "English Language Learners";
subjectArray[12][3] = "ell.png";
subjectArray[12][4] = "md_icon_ell";
//Career and Technical Education
subjectArray[13] = new Array(4);
subjectArray[13][0] = "lg_icon_career";
subjectArray[13][1] = "Career and Technical Education";
subjectArray[13][2] = "Career and Technical Education";
subjectArray[13][3] = "voced.png";
subjectArray[13][4] = "md_icon_career";
//Current Events
subjectArray[14] = new Array(4);
subjectArray[14][0] = "lg_icon_currentevents";
subjectArray[14][1] = "Current Events: News Event Analysis, Breaking News...";
subjectArray[14][2] = "Current Events";
subjectArray[14][3] = "currentevents.png";
subjectArray[14][4] = "md_icon_currentevents";
//21
subjectArray[15] = new Array(4);
subjectArray[15][0] = "lg_icon_21c";
subjectArray[15][1] = "21st Century Skills";
subjectArray[15][2] = "21st Century Skills";
subjectArray[15][3] = "21c.png";
subjectArray[15][4] = "md_icon_21c";

subjectArray[16] = new Array(4);
subjectArray[16][0] = "lg_icon_themes";
subjectArray[16][1] = "Themes";
subjectArray[16][2] = "Themes";
subjectArray[16][4] = "md_icon_themes";

subjectArray[17] = new Array(4);
subjectArray[17][0] = "lg_icon_referencetools";
subjectArray[17][1] = "Reference Tools";
subjectArray[17][2] = "Reference Tools";
subjectArray[17][4] = "md_icon_referencetools";
//NA
subjectArray[100] = new Array(4);
subjectArray[100][0] = "";
subjectArray[100][1] = "";
subjectArray[100][2] = "";
subjectArray[100][3] = "";
subjectArray[100][4] = "";
