jQuery.fn.loadImgs = function(options) {
	options = $.extend({
		 src : ""
	},options);
	var _self = this;
	_self.hide();
	var img = new Image();
	$(img).load(function(){
		_self.attr("src", options.src);
		//_self.fadeIn("slow");
	}).attr("src", options.src);  //.atte("src",options.src)要放在load后面，
	return _self;
}

// video tracking tags

function trackPlayVideo() {
	dcsMultiTrack('DCS.dcsuri','DeVry.edu: Home  New Degree Tracks - MDD Video Play','WT.ti','DeVry.edu: Home  New Degree Tracks - MDD Video Play');
}
function trackExploreMore() {
	dcsMultiTrack('DCS.dcsuri','DeVry.edu: Home  New Degree Tracks - MDD Explore four degrees','WT.ti','DeVry.edu: Home  New Degree Tracks - MDD Explore four degrees');
}
// leave outside of jquery




$(function() { //document.ready jquery call go in here
		   // button highlight
	var ie = false;
	if(jQuery.browser.msie) {
		ie = true;
	}
	$("#wrap").add("<img src='' id='loadImg' width='0' height='0' />");
	var opencms = ((window.location.href).indexOf("opencms")>-1) ? "/opencms" : "";
	var imgPath = opencms+"/resources/javascript/qw/img-home/";
	$('#loadImg').loadImgs({src:imgPath+"bg-colleges-business.gif"});
	$('#loadImg').loadImgs({src:imgPath+"bg-colleges-engineering.gif"});
	$('#loadImg').loadImgs({src:imgPath+"bg-colleges-health.gif"});
	$('#loadImg').loadImgs({src:imgPath+"bg-colleges-liberal.gif"});
	$('#loadImg').loadImgs({src:imgPath+"bg-colleges-media.gif"});
	$('#loadImg').loadImgs({src:imgPath+"bg-colleges-keller.gif"});
    $("#tooltip").css("display","none");
	$('#search-btn').mouseover(
		function (){
		$('#search-btn').css('background-position','-267px -30px');
		});
	$('#search-btn').mouseout(
		function (){
		$('#search-btn').css('background-position','-267px -2px');
		});
	
	$('.send-btn').mouseover(
		function (){
		$('.send-btn').css('background-position','-321px -33px');
		});
	$('.send-btn').mouseout(
		function (){
		$('.send-btn').css('background-position','-321px -5px');
		});
	$('.go-btn').mouseover(
		function (){
		$('.go-btn').css('background-position','-358px -33px');
		});
	$('.go-btn').mouseout(
		function (){
		$('.go-btn').css('background-position','-358px -5px');
		});
	$('.go-btn1').mouseover(
		function (){
		$('.go-btn1').css('background-position','-358px -33px');
		});
	$('.go-btn1').mouseout(
		function (){
		$('.go-btn1').css('background-position','-358px -5px');
		});		   

	
	var metaCondensedBold = {
		src: opencms + '/resources/flash/metaCondensedBold.swf'
	}
	sIFR.activate(metaCondensedBold);
	sIFR.replace(metaCondensedBold, {
		selector: '#sidebar h3'
		,wmode: 'transparent'
		,css: [ '.sIFR-root { color:#205C82;}' ]
	});
	
	//add slider controls to page
	$('#slide-controls').append('<li id="ctrl-back"><a href="#">Back</a></li><li id="ctrl-play" style="display:none;"><a href="#">Play</a></li><li id="ctrl-pause"><a href="#">Pause</a></li><li id="ctrl-next"><a href="#">Next</a></li>')
	$('#slide-controls').before('<span id="ctrl-slider"></span>')
	//initiate cycle 
	
	$('#slide-holder').cycle({ 
	    fx: 'fade',
		speed: 700,
		timeout: 8000,
	    delay: -700,
		autostop: 1,
		autostopCount: 5,
		end: pauseShow,
	    prev: '#ctrl-back',
		next: '#ctrl-next',
		prevNextClick: pauseShow,
		before: sliderCall
	});
	
	//slider position callback function
	function sliderCall(curr, next, opts) {
		var slideid = next.id
		if (slideid == 'slide-one') {
			$('#ctrl-slider').stop().animate({"right": "540px"}, 600);
        }
		if (slideid == 'slide-two') {
			$('#ctrl-slider').stop().animate({"right": "392px"}, 600);
        }
		if (slideid == 'slide-three') {
			$('#ctrl-slider').stop().animate({"right": "223px"}, 600);
        }
		if (slideid == 'slide-four') {
			$('#ctrl-slider').stop().animate({"right": "55px"}, 600);
        }
		else {
            return;
        }	
	}

	
	// go to first function set to about us callback on end

	function pauseShow() {
	    $('#slide-holder').cycle('pause');
		$('#ctrl-play').show();
		$('#ctrl-pause').hide();
	};
	function resumeShow() {
		$('#slide-holder').cycle('resume', true);
		$('#ctrl-play').hide();
		$('#ctrl-pause').show();
	};

	//add play/pause controls
	$('#ctrl-pause').click(function() { 
	    pauseShow();
		return false; 
	});
	$('#ctrl-play').click(function() {
		resumeShow();
		return false; 
	});
	
	// tile controls
	$('#ctrl-one').click(function() { 
	    $('#slide-holder').cycle(0);
		pauseShow();
	    return false; 
	});
	$('#ctrl-two').click(function() { 
	    $('#slide-holder').cycle(1);
		pauseShow();
	    return false;
	});
	$('#ctrl-three').click(function() { 
	    $('#slide-holder').cycle(2);
		pauseShow();
	    return false; 
	});
	$('#ctrl-four').click(function() { 
	    $('#slide-holder').cycle(3);
		pauseShow();
	    return false; 
	});	
	
	$('#nav').css({'display' : 'none', 'height' : '0'})

// Menu call
	$(function menuCall() {
	    var time = 500;
	    var hideDelay = 250;
	    var hideDelayTimer = null;
	    var beingShown = false;
	    var shown = false;
		var initialHover = false;
		var initial = false;	
	    var trigger = $('#nav-call', this);
	    var info = $('#nav', this).css({ opacity: "0"});
	    $([trigger.get(0), info.get(0)]).mouseover(function () {
	        if (hideDelayTimer) clearTimeout(hideDelayTimer);
	        
			if (beingShown || shown) {
	            // don't trigger the animation again
	            return;
	        } else {
	            // reset position of info box
	            beingShown = true;
				initialHover = true;
	           	info.css({
	                top: 19,
	                display: 'block'
	            }).animate({
	               	height: 140,
	                opacity: 1
	            }, time, 'swing', function() {
	                beingShown = false;
	                shown = true;
	            });
	        }

	        return false;
	    }).mouseout(function () {
	        if (hideDelayTimer) clearTimeout(hideDelayTimer);
	        hideDelayTimer = setTimeout(function () {
	            hideDelayTimer = null;
				info.animate({
					opacity: .5,
					height: 0
				}, time, 'swing', function () {
				    shown = false;
				    info.css('display', 'none');
				});

	        }, hideDelay);

	        return false;
	    });
	
		function hideMenu() {
			if (initialHover || initial) {
	            // don't trigger the animation again
	            return;
	        } else {
	            info.animate({
					opacity: .5,
					height: 0
				}, time, 'swing', function () {
				    shown = false;
				    info.css('display', 'none');
				});
	        }
	        return false;
		};
		$('#nav').animate({height: 140, opacity: 1 }, 1000).animate({opacity: 1.0}, 2000, function () {
			hideMenu();	
		});		
	});


	//better colleges hover over

	$('#colleges li').each(function () {
	    var distance = 10;
	    var time = 500;
	    var hideDelay = 150;

	    var hideDelayTimer = null;

	    var beingShown = false;
	    var shown = false;
	    var trigger = $('h2 a', this);
	    var info = $('.popup', this).css('opacity', 0);

	    $([trigger.get(0), info.get(0)]).mouseover(function () {
	        if (hideDelayTimer) clearTimeout(hideDelayTimer);
	        if (beingShown || shown) {
	            // don't trigger the animation again
	            return;
	        } else {
	            // reset position of info box
	            beingShown = true;				
	           	info.css({
	                bottom: 53,
	                'z-index': 40,
	                display: 'block'
	            }).animate({
	                bottom: '+=' + distance + 'px',
	                opacity: 1
	            }, time, 'swing', function() {
	                beingShown = false;
	                shown = true;
	            });
	        }

	        return false;
	    }).mouseout(function () {
	        if (hideDelayTimer) clearTimeout(hideDelayTimer);
	        hideDelayTimer = setTimeout(function () {
	            hideDelayTimer = null;
					
	            info.animate({
	                bottom: '-=' + distance + 'px',
	                opacity: 0
	            }, time, 'swing', function () {
	                shown = false;
	                info.css('display', 'none');
	            });

	        }, hideDelay);

	        return false;
	    });
	});

			// Location script with XML
			$(function() { 
				// Get a parameter value from a querystring
				function getParam(name) {
					name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
					var regexS = "[\\?&]"+name+"=([^&#]*)";
					var regex = new RegExp( regexS );
					var results = regex.exec( window.location.href );
					if( results == null ) {
					  return false;
					  } else {
					  return results[1];
					  };
				};
				
				var city = getParam('city');  //console.log(city)
				var prog = getParam('program');  //console.log(prog);

				if (prog == "flexible") {
					$(window).load(function () {
						$('#slide-holder').cycle(1);
						pauseShow();
						dcsMultiTrack('DCS.dcsuri','DeVry.edu: Home  Flexible Study','WT.ti','DeVry.edu: Home  Flexible Study');
					});
		        }; // end if prog
		
		
				if (city) {
					$.ajax({
				    	type: "GET",
				    	url: "/resources/javascript/qw/locations.xml",
				    	dataType: "xml",
				    	success: parseXml
				  	});
					$('#location-default').after('<div id="location-data"></div>');

		        }; // end if city

				function parseXml(xml) {
					$(xml).find("location[param='" + city + "']").each(function() {
						formOutput = '';
						formOutput += '<h4>' + $(this).find("name").text() + '</h4>';
						formOutput += '<form action="#" method="get" id="form-locationCity" onSubmit="return selectLink(this);"><select name="iam" id="select-locationCity" class="select" ><option value="-1">Find one nearby</option>';
						$(this).find("campuses").find("campus").each(function() {
							formOutput += '<option value="' + $(this).attr('url') + '">' + $(this).text() + '</option>';
						});
						formOutput += '</select><input type="submit" value="Go" class="go-btn" /></form>';
						$("#location-data").append(formOutput);
						$("#location-default").css('display', 'none');
						// Locations graphic based on position
						pos = $(this).find("name").attr("pos");
						if(pos) {
							$("#location-data h4").css( {
								"text-indent": "-9999px",
								"background-image": "url('/resources/javascript/qw/img-home/sidebar-locations.gif')",
								"background-repeat": "no-repeat",
								"background-position": "0px " + pos + ""		
							});
						}					
					});						
				};
			});
	
	
});






// Explore the University Form Action
function selectLink(oForm) {
	document.location.href = oForm.iam.value
	return false;
};

function popUp(url) {
	newwindow=window.open(url,'name','height=600,width=650');
	if (window.focus) {newwindow.focus()}
	return false;
}

// Live Chat Functionality
var needValidate = false;

var defaultVal2 = "Type your question here and click below...";
function  openchatwindow(cbForm,windowName) {
	var res = true;

	var msg = document.getElementById("chat-field");

	if(needValidate){
		var validateMessage = "Before you start your chat, type in the following fields:\r\n ";

		if(msg.value.length==0 || msg.value==defaultVal2 ){
			validateMessage += "Your Question\r\n";
			res=false;
		}
		if(res==false){
			alert(validateMessage);
		}
	}
    var formurl = cbForm.action;
    if(formurl.indexOf('?')==-1)
        formurl += '?';
    var elemCol = cbForm.elements;
    for(var ie=0; ie<elemCol.length; ie++){
        var et = elemCol[ie].type;
        if(et=="text" || et=="hidden" || et=="radio" || et=="textarea"){
            formurl += elemCol[ie].name + "=" + escape(elemCol[ie].value) + "&" ;
        }
    }
	if(res==true)
		dcsMultiTrack('DCS.dcsuri','DeVry.edu: Home Chat Live Enter','WT.ti', 'DeVry.edu: Home Chat Live Enter');
		window.open(formurl, windowName,'width=475,height=400,resizable=yes');
	return res;
};

