$(document).ready(function () {

	//IE fix for movie box
	//alert(!jQuery.support.cssFloat);
	
	$("img[src*='time-icon-.png']").remove();
	
	
	if(!jQuery.support.cssFloat && $('div.movie').length > 0){
		
		$('div.movie').css("background", "url("+$('#xHeaderImage img').attr("src")+")");
		
	}
	
	//fix spacing
	if($('#xContent > *:first').is('p')){
		$('#xContent > p:first').css('padding-top','10px');
	}
	
	if($('#xContentHome > *:first').is('p')){
		$('#xContentHome > p:first').css('padding-top','10px');
	}
	
	if($('#xContent > *:first').is('table')){
		$('#xContent > table:first').css('margin-top','10px');
	}
	
	if($('#xContent > *:first').is('h4')){
		$('#xContent > h4:first').css('margin-top','10px');
	}

	
	
	//highlight programs - wayfinder is not enough
	if($('#highlightProgramsInMenu').length > 0){
		$("#xMainMenu div a:contains('Programs')").addClass('active');
	}
	
	if($('table.staffList').length > 0){
	
		$(".staffCateg").click(function(){
            $(this).next().slideToggle("slow");
            $(this).children("h3").toggleClass("staffCategOpen");
        }).next().hide();
		
	}
	
	if($('#searchTablesWrp').length > 0){
		
		
		//zebra
		$('#searchTablesWrp table.searchContent tr:even').addClass('even');
		
		//add doted separator
		$('#searchTablesWrp table.searchHeader tr').after('<tr class="searchHeaderSeaparator"><td colspan="5"></td></tr>');
		$('#searchTablesWrp table.searchHeaderAz tr').after('<tr class="searchHeaderSeaparator"><td colspan="5"></td></tr>');
		
		//open close
		$(".searchHeader").click(function(){
            $(this).next().slideToggle("slow");
            $(this).children().children().children('td.pageTitle').toggleClass('closeTitle');
            
            //history - make url
            pid = $(this).attr("id");
			base = $("base").attr("href");
			//rewrite url
			document.location = makeHistoryUrl(document.location.href,pid, base);
		
		}).next().hide(function(){
        	//$(this).prev().children().children().children('td.pageTitle').addClass('test');
        });    
        
        //history open default
		locP = document.location.href;
		if(locP.split('#')[1]){//open when hash present when loading page
		openT = locP.split('#')[1];
		if(openT.split('|')[1]){//more hash values
			aOpenT = openT.split("|");
			for(var i=0, len=aOpenT.length; i<len; ++i ){
				$('#' + aOpenT[i]).next().slideToggle("slow");
				$('#' + aOpenT[i]).children().children().children('td.pageTitle').toggleClass('closeTitle');
			}
		}else{//only one hash value
			$('#' + openT).next().slideToggle("slow");
			$('#' + openT).children().children().children('td.pageTitle').toggleClass('closeTitle');
		}
		}else{//no hash
		
		};
		
		//tooltips
		tooltip();
        
        //color type column
        $("td.type:contains('Course')").css("color", "#000033");
        $("td.type:contains('Certificate')").css("color", "#006633"); 
        $("td.type:contains('Diploma')").css("color", "#006699");
        $("td.type:contains('Degree')").css("color", "#993300");
        $("td.type:contains('Non-Certificate')").css("color", "#7F7F99");
        $("td.type:contains('Postdiploma/Postdegree Certificate')").css("color", "#996633");
           
	}
	
	
	if($('#newsMenu').length > 0){
		$('#newsMenu').accordion({
			header: 'h2.drawer-handle',
			active: ".opensesami",
			autoHeight: false
		}); 
	}
	
	
	if($('#xHomeHighlightBig').length > 0){
		
		//alert($("#xHomeHighlightBig a").length);
		
		$("#xHomeHighlightBig a").each(function (i) {
			img = $(this).children("img").attr("src");
			$(this).empty();
			$(this).css({'background-image' : 'url('+img+')'});
		});
	}
	
	
	
});



$(window).load(function () {
	
	/*
	//set content height based on left col
	if($('#xWrp7').length > 0){
		var p = $("#xWrp7");
		var position = p.position();
		var boxH = $("#xExtraMenu2").height()
		$('#xWrp4').height(position.top + boxH +100);//100 is the header
	}
	*/
	
	
	//set content and right col height
	var HxWrp6 = $('#xWrp6').height();
	var HxWrp4 = $('#xWrp4').height();
	
	
	
	if(HxWrp6 > HxWrp4){
		$('#xWrp4').height(HxWrp6);
		
		
			
	}else{
		$('#xWrp6').height(HxWrp4 - 20);	
		
		//aligh contact at bottom if right col is smaller then main content
		if($('#xProgramContactInformation').length > 0){
			var HxWrp3 = $('#xWrp3').height();
			
			//alert(HxWrp3);
			
			var HxProgramContactInformation = $('#xProgramContactInformation').height();
			var nTop = HxWrp3 - HxProgramContactInformation + 10;
			
			$('#xProgramContactInformation').css({'position' : 'absolute', 'top' : nTop+'px'});
		}	
	}
	
	


	
});


function playMovie(m){    
	$(".movie").load("movie.php",{movie : m});
	$(".movie").removeAttr("onclick");
}


// in_array function for javascript
Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}

//need to fix IE error
if(!Array.indexOf){
	    Array.prototype.indexOf = function(obj){
	        for(var i=0; i<this.length; i++){
	            if(this[i]==obj){
	                return i;
	            }
	        }
	        return -1;
	    }
}

//make url for open/close history
function makeHistoryUrl(currentUrl,sharpPart,base){
	loc = currentUrl;
	pid = sharpPart;
	partUrl =  currentUrl.split(base)[1];//the part after base but before #
	partUrl = partUrl.split('#')[0] + '#';
	
	if(loc.split('#')[1]){//sharp detected
		
		sSharp = loc.split('#')[1];
		sharp = sSharp.split('|');
		
		if(sharp[1]){ //more then 1 things detected
			if(sharp.in_array(pid)){//value exist remove from array
				//alert("in array");
				indexValue = sharp.indexOf(pid);
				sharp.splice(indexValue,1);
				return base + partUrl + sharp.join("|");
			}else{//value does not exist, add to sharp
				return loc + "|" + pid;
			}
		}else{
			if(pid == sSharp){ //value exist - close detected
				return base + partUrl
			}else{ //value does not exist add new value
				return loc + "|" + pid;
			}
		}
	}else{//no sharp. open first and add sharp
		return base + partUrl + pid;
	}
	
}



this.tooltip = function(){	
	/* CONFIG */		
		xOffset = -20;
		yOffset = -20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("img.tipTrigger").hover(function(e){	
		var position = $(this).position();
		
		$(this).next('div')
			.css("top",(position.top - xOffset) + "px")
			.css("left",(position.left + yOffset) + "px")
			.fadeIn();		
    },
	function(){
		
		$(this).next('div').fadeOut('fast');
    });			
};


// ############################## NOT USED
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};
// ##############################