/*
 * Global Variables
 */
var features = new Object();
var hompageEditState = false;

/*
 * The KhoiPondHomePage object
 */
function KhoiPondHomePage(){
	this.dataCheck = kphp_dataCheck;
	
	this.clickType;
	
	this.features = {
			id: "#features",
			state: "normal",
			max: 7,
			start: false,
			activeEl: false,
			active: false
	};
	this.setFeatures = kphp_setFeatures;
	this.setFeatureItemAction = kphp_setFeatureItemAction;
	this.setFeatureSetAction = kphp_setFeatureSetAction;
	this.setFeaturesMin = kphp_setFeaturesMin;
	
	this.news = {
			id: "#news",
			animate: true
	};
	this.setNews = kphp_setNews;
	this.setNewsMin = kphp_setNewsMin;
	
	this.video = {
			//This depends on the global JS variable youTubeFeed being present
			id: ".video",
			//embedObject: 'object.youtube',
			currentMovie: 0,
			//htmlObject: 'object',
			currentMovieTitle: false,
			currentMovieTitle: false,
			currentMovieUrlRE: new Array()
	};
		
	this.setVideo = kphp_setVideo;
	
	this.slideshows = {
			id: '.slideshow',
			speed: 3000,
			timeout: false,
			firstPlay: false
	}
	this.setSlideshows = kphp_setSlideshows;
	this.resizeImage = kphp_resizeImage;
	
	this.setMediaPlayer = kphp_setMediaPlayer;
	
	this.customizations = {
			id: ".custom",
			contentArea: ".carousel",
			contentElementContainer: ".region",
			contentElements: ".widget",
			controller: ".carouselControl"			
	}		
	this.setCustomizations = kphp_setCustomizations;
	this.setCustomizationsToggleAnimation = kphp_setCustomizationsToggleAnimation;
	this.customizationsChangeCarousel = kphp_customizationsChangeCarousel;
	
	this.ajaxSubmit = kphp_ajaxSubmit;
	this.getNewWidgetHtml = kphp_getNewWidgetHtml;
	this.setWidgetFormDefaults = kphp_setWidgetFormDefaults;
	this.showCustomOptions = kphp_showCustomOptions;
	this.addRegionTransparencies = kphp_addRegionTransparencies;
	this.getMinObject = kphp_getMinObject;
	this.setCookie = kphp_setCookie;
	this.setTouchAnimation = kphp_setTouchAnimation;
	
	this.__construct = function(){
		var obj = this;
		obj.dataCheck();
		$("body").ready(function(){
			obj.setTouchAnimation();
			obj.setFeatures();
			obj.setNews();
			obj.setVideo();
			obj.setCustomizations();
			if($("#featuresMin").length > 0){
				obj.setFeaturesMin();
			}	
			if($("#newsMin").length > 0){
				obj.setNewsMin();
			}
			if($(".slideshow").length > 0){
				obj.setSlideshows();
			}
			if($(".media-player").length > 0){
				obj.setMediaPlayer();
			}
		});
	}	
}

function kphp_dataCheck(){
	var obj = this;
	if(features.constructor != Object){
		return false;
	}
	else{
		setTimeout(function(){
			obj.dataCheck();
		},50);
	}
} 


function kphp_setFeatures(){
	var obj = this;	
	
	if(typeof(siteSet) != 'undefined'){ 
		$.ajax({
			url: 'http://'+window.location.hostname+'/_common_/actions/get/data.php?file=features_'+siteSet,
			type: "GET",
			dataType: 'xml',
			success: function(data,status){
				features = data;
			},
			cache: false,
			error: function(features,status,errorThrown){
				return false;
			}
		});
	}
	
	//if using the .withLogo class, set features.max to 5
	if($(obj.features.id+" .withLogo").length > 0){
		obj.features.max = 5;
	}	
	
	
	//Show the correct amount of features
	$(obj.features.id+" .menu li a").removeAttr("href"); 	
	$(obj.features.id+" .menu li").each(function(){
		$(this).hide();
		if($(this).attr("class") == 'selected'){
			obj.features.start = 1;
		}
		if((obj.features.start != false)&&(obj.features.start <= obj.features.max)) {
			$(this).show();
			obj.features.start++;
		}
	});
	if($(obj.features.id+" .menu li:visible").length < obj.features.max){
		var addNum = obj.features.max - $(obj.features.id+" .menu li:visible").length;
		if(addNum < $(obj.features.id+" .menu li").length){			
			var add = $(obj.features.id+" .menu li").slice(0,addNum);
			add.insertAfter($(obj.features.id+" .menu li:visible:last")).show();
		}
		else{
			$(obj.features.id+" .menu li").show();
		}
	}
	
	//Add the control buttons
	$(obj.features.id+" .menu").before('<input type="button" class="alignLeft alignBottom arrow_white_large_left button" name="left" />');
	if($(obj.features.id+" .withLogo").html() != null){
		$(obj.features.id+" .button:first").css("left","208px");
	}
	$(obj.features.id+" .menu").after('<input type="button" class="alignRight alignBottom arrow_white_large_right button" name="right" />');

	obj.setFeatureItemAction();
	obj.setFeatureSetAction();
}

function kphp_setFeatureItemAction(){
	var obj = this;
	
	$(obj.features.id+" .menu li").click(function(){
		obj.features.activeEl = $(this);
		
		siteTracker._trackEvent('FeaturesItemToggle', obj.features.activeEl.attr("name"));
		$(obj.features.id+" .left img").fadeOut("fast");
		
		$(obj.features.id+" .right").fadeOut("fast", function(){
			$(obj.features.id+" .menu li").attr("class", "");
			obj.features.activeEl.attr("class", "selected");
			
			obj.features.active = $(features).find('item > articleid').filter(function(){
				if( $(this).text() == obj.features.activeEl.attr('uniqueid') ){
					return true;
				}	
				else{
					return false;
				}	
			}).parent();
			//var newImg = articleState+'/images/'+obj.features.active.children('imageLarge').text();
			var newImg = obj.features.active.children('imageLarge').text();
			var newImgAlt = obj.features.active.children('title').text();
			//var newUrl = newsState+'/features/article/'+siteSet+'/'+obj.features.active.children('articleid').text()+'/'+obj.features.active.children('uri').text()+'.html';
			var newUrl = obj.features.active.children('uri').text();
			
			$(obj.features.id+" .left img").attr("src",newImg);
			$(obj.features.id+" .left img").attr("alt",newImgAlt);
			$(obj.features.id+" .right .title").html(obj.features.active.children('title').text());
			$(obj.features.id+" .right .previewText").html(obj.features.active.children('preview').text());
			$(obj.features.id+" .right a").attr("href", newUrl);
			$(obj.features.id+" .left img, "+obj.features.id+" .right").fadeIn("slow");
		});	
		return false;
	});
}

function kphp_setFeatureSetAction(){
	var obj = this;
	
	$(obj.features.id+" .button").click(function(){
		
		if(typeof(siteTracker) != 'undefined'){
			siteTracker._trackEvent('FeaturesSetToggle', $(this).attr("name"));
		}
		$(obj.features.id+" menu li.selected").attr("class","");
		var first = $(obj.features.id+" .menu li:visible:first");
		var last = $(obj.features.id+" .menu li:visible:last");
		
		if($(this).attr("name") == "right"){		
			var nextSet = last.nextAll().slice(0,obj.features.max);
			if(nextSet.length < obj.features.max){
				var addSet = $(obj.features.id+" .menu li").slice(0, obj.features.max - nextSet.length);
			}
			$(obj.features.id+" .menu li:visible").hide();
		}
		else{
			var nextSet = first.prevAll().slice(0,obj.features.max);
			if(nextSet.length < obj.features.max){
				var start = $(obj.features.id+" .menu li").length - obj.features.max;
				var end = $(obj.features.id+" .menu li").length - nextSet.length; 
				var addSet = $(obj.features.id+" .menu li").slice(start, end);
			}
			$(obj.features.id+" .menu li:visible").hide();
		}
		nextSet.fadeIn();
		if(addSet){
			addSet.fadeIn();
		}	
	
		
	});
}

function kphp_setVideo(){
	var obj = this;
	
	if(typeof(youTubeFeed) != 'undefined'){
		$(obj.video.id+' .subtitle').after('<input type="button" class="next" /><input type="button" class="previous" />');
	}
	$(obj.video.id).find('input[type="button"]').live('click', function(){
		if(typeof(siteTracker) != 'undefined'){
			siteTracker._trackEvent('Video', 'Toggle');
		}
		
		var domEl = $(obj.video.id).find('.player');
	
		var prevMovie = obj.video.currentMovie;
		
		if($(this).is(".next")){
			obj.video.currentMovie++;	
		}	
		else{
			obj.video.currentMovie--;
		}
		
		if(obj.video.currentMovie < 0){
			obj.video.currentMovie = youTubeFeed.length - 1;
		}	
		if(obj.video.currentMovie >= youTubeFeed.length){
			obj.video.currentMovie = 0;
		}				
		
		var html = domEl.html();
		obj.video.currentMovieUrlRE = new Array(new RegExp(youTubeFeed[prevMovie].url,"g"), youTubeFeed[obj.video.currentMovie].url);
		html = html.replace(obj.video.currentMovieUrlRE[0],obj.video.currentMovieUrlRE[1]);
		domEl.html(html);
		
		obj.video.currentMovieTitle = youTubeFeed[obj.video.currentMovie].title;
		$(obj.video.id).find(".subtitle span").html(obj.video.currentMovieTitle);
	});

}

function kphp_resizeImage(viewer, images){
	var obj = this;
	
	images.each(function(){
			var yfactor = viewer.height()/$(this).height();
			var xfactor = viewer.width()/$(this).width();
			
			if( (xfactor < 1) || (yfactor < 1) ){
				if(xfactor <= yfactor){
					var newX = $(this).width()*xfactor;
					var newY = $(this).height()*xfactor;
				}
				else{
					var newX = $(this).width()*yfactor;
					var newY = $(this).height()*yfactor;
				}
				$(this).width(newX);
				$(this).height(newY);
			}
			
			var h = $(this).height();
			
			if(h == 0){
				h = $(this).attr('height');
			}	
			
			if( (viewer.height() > h) && (h > 0) ){
				var pad = (viewer.height() - h ) / 2;
				$(this).css('border-top', pad+'px solid black');
			}	
			else{
				$(this).css('border-top', 'none');
			}	
			$(this).show();
	});	
}	

function kphp_setSlideshows(){
	var obj = this;
	/*
	 * This code replaces shadowbox.js and all of its subsidiaries
	 * 
	 * it takes anchor tags in the slideshow object, fetches the images, creates the img tags,
	 * and loops their show/hide.
	 */	
	
	$(obj.slideshows.id).bind(obj.clickType.enter, function(){
		$(obj.slideshows.id+ ' .pause').fadeIn(200, function(){
			setTimeout(function(){
				$(obj.slideshows.id+ ' .pause').fadeOut();
			},1000);
		});
	});
	
	$(obj.slideshows.id+ ' .player').attr('index', '0');
	
	//set all of the start images:
	obj.resizeImage($(obj.slideshows.id+ ' .player'), $(obj.slideshows.id+ ' .player img.start'));
		
	//add the progress meter
	$(obj.slideshows.id+' .player').append('<div class="meter"></div>');
	$(obj.slideshows.id+ ' .player .meter').css({
		height: '3px',
		background: '#666',
		position: 'absolute',
		bottom: '0px',
		left: '0px'
	});	
		
	$(obj.slideshows.id+ ' .controlButton').bind(obj.clickType.start, function(){
		var player = $(this).parents('.player');
		var viewer = player.children('.viewer');
		var urls = player.parent().find('.linkset a.slide');
		
		//preload images on first play:
		if(obj.slideshows.firstPlay == false){
			if($(this).is('.play')){
				$(obj.slideshows.id).append('<div class="loader"/></div>');
			}	
			urls.each(function(){
				var u = $(this).attr('href');
				new Image().src = u;
			});
			obj.slideshows.firstPlay = true;
		}	
		
		if($(this).is('.play')){
			if(typeof(siteTracker) != 'undefined'){
				siteTracker._trackEvent('Slideshow', 'Play');
			}
			
			$(this).removeClass('play').addClass('pause');		
			
			function changeImage(){
				var n = parseInt(player.attr('index'));
	
				viewer.children('img').remove();
				
				viewer.append('<img />');
				var image = viewer.children('img');
				image.hide();
				image.bind('load', function(){
					obj.resizeImage(viewer, image);
					image.fadeIn(200, function(){
						$(obj.slideshows.id).find('.loader').remove();
						obj.slideshows.timeout = setTimeout(function(){
							changeImage();
						}, obj.slideshows.speed);
						
					});
				});	
				image.attr('src', urls.eq(n).attr('href'));
				
				
				
				//update the progress meter:
				var meter = player.children('.meter');
				var mWidth = (player.width() / (urls.length-1)) * n;
					
				meter.animate({'width': mWidth}, 300);
				
				//keep looping:
				n++;
				if(n == urls.length){
					player.attr('index', 0);
				}	
				else{
					player.attr('index', n);
				}	
		
			}
			player.children('.start').hide();	
			viewer.show();
			changeImage();
		}
		else{
			clearTimeout(obj.slideshows.timeout);
			$(this).removeClass('pause').addClass('play');
			viewer.hide();
			player.children('img.start').attr('src', viewer.children('img').attr('src'));
			player.children('img.start').attr('style', viewer.children('img').attr('style'));
			
			player.children('.start').fadeIn();
		}	
	
	});
	
	
	//if there is more than one slideshow available, allow toggling:
	if($(obj.slideshows.id).length > 1){
		//assumes contained with a media-player object. see alumni.r.e
		$(obj.slideshows.id+' .subtitle').after('<input type="button" class="next" /><input type="button" class="previous" />');
	
		$(obj.slideshows.id).find('input[type="button"]').bind(obj.clickType.start, function(){
			if(typeof(siteTracker) != 'undefined'){
				siteTracker._trackEvent('Slideshow', 'Toggle');
			}
			
			var domEl = $(obj.slideshows.id).find('.player');
			
			var slideshows = $(this).parents('.media-player').find('.media-element[type="slideshow"]');
			var currentShow = $(this).parents('.media-element[type="slideshow"]');
			slideshows.hide();
			
			obj.slideshows.firstPlay = false;
			
			if($(this).is(".next")){
				if(currentShow.html() == slideshows.filter(':last').html()){
					var node = slideshows.filter(':first');
				}
				else{
					var node = currentShow.next('.media-element[type="slideshow"]');
				}	
				node.show();
			}
			else{
				if(currentShow.html() == slideshows.filter(':first').html()){
					var node = slideshows.filter(':last');
				}
				else{
					var node = currentShow.prev('.media-element[type="slideshow"]');
				}	
				node.show();
			}
			$(obj.slideshows.id+ ' .controlButton').removeClass('play').addClass('pause');
			$(obj.slideshows.id+ ' .controlButton').trigger(obj.clickType.start);
		
		});
	}
}

function kphp_setMediaPlayer(){
	var obj = this;
	
	$(".media-player").each(function(){
		$(this).children('.media-element:not(:first)').hide();
	});
	
	//controls:
	$('.media-menu .item').bind('click', function(){
		var player = $(this).parents('.media-player');
		player.find('.media-menu .item').removeClass('on');
		$(this).addClass('on');
		player.children('.media-element').hide();
		player.children('.media-element[type="'+$(this).attr('target')+'"]:first').fadeIn();
		
		//reset slideshows
		$(obj.slideshows.id+ ' .controlButton').removeClass('play').addClass('pause');
		$(obj.slideshows.id+ ' .controlButton').trigger(obj.clickType.start);
	});
	
}

function kphp_setNews(){
	var obj = this;
	
	if(obj.news.animate == true){
		var dt_count = $(obj.news.id+" dl.collapsed dt").length;
		
		$(obj.news.id+" dl.collapsed dd").height(288-(36*dt_count));
		
		//Set the accordion's ul height based on the parent height
		if( (dt_count < 4) && (jQuery.support.cssFloat == true) ){
			$(obj.news.id+" dl.collapsed dd ul").height(252-(36*dt_count));
		}	
	}	
	
	$(obj.news.id+" dl dt").click(function(){
		var dt_el = this;
		
		if(typeof(siteTracker) != 'undefined'){
			siteTracker._trackEvent('NewsEventWidget', $(dt_el).find('span').text());	
		}
		if($(this).attr("class").indexOf("selected") == -1){
			$(dt_el).parent().css("overflow","hidden");
			if(obj.news.animate == true){
				$(dt_el).parent().find("dd:visible").slideUp(300);
				$(dt_el).next().slideDown(500);	
			}
			else{
				$(dt_el).parent().find("dd:visible").hide();
				$(dt_el).next().show();
			}
			$(dt_el).parent().find("dt").attr("class", "");
			$(dt_el).parent().find("dt:first").attr("class", "first");
			$(dt_el).attr("class", $(dt_el).attr("class")+" selected");
			$(dt_el).parent().css("overflow","auto");
			
			return false;
		}	
	});
}

function kphp_setFeaturesMin(){
	$("#featuresMin ul").after('<span class="tiny">Next article</span>');
	$("#featuresMin li:not(.selected)").hide();
	$('#featuresMin .tiny').click(function(){
		if(typeof(siteTracker) != 'undefined'){
			siteTracker._trackEvent('FeaturesMin', 'toggle');
		}
		var next = $("#featuresMin li:visible").next();
		if(next.size() == 0) next = $("#featuresMin li:first");
		$("#featuresMin li:visible").hide();
		next.fadeIn();
	});
}

function kphp_setNewsMin(){
	var obj = this;
	obj.news.id = "#newsMin"
	obj.news.animate = false;
	obj.setNews();
}

function kphp_setCustomizations(){
	var obj = this;
	
	$(obj.customizations.id).attr("class", $(obj.customizations.id).attr("class")+' customJS');
	$(obj.customizations.contentArea).attr("class", $(obj.customizations.contentArea).attr("class")+' carouselJS');
	
	$(obj.customizations.contentArea+" "+obj.customizations.contentElementContainer+":not(:first)").hide();
	
	$(obj.customizations.contentArea+" "+obj.customizations.contentElementContainer).each(function(){
		$(obj.customizations.controller+' .control').append('<input type="button" value="'+($(this).index()+1)+'" />');
		$(this).attr('count',($(this).index()+1));
	});
	
	if($(obj.customizations.contentArea+" "+obj.customizations.contentElementContainer).length > 1){
		$(obj.customizations.controller+" input:first").attr("class","active");
		$(obj.customizations.controller+" input:first").before('<input class="arrow arrow_white_med_left" name="previous" type="button" />');
		$(obj.customizations.controller+" input:last").after('<input class="arrow arrow_white_med_right" name="next" type="button" />');
	}
	else{
		//this means there is only one region on the page
		$(obj.customizations.controller+' .control').remove();
	}
	
	//Set up option forms and carousel slide animation
	obj.setWidgetFormDefaults();
	$(".optionsToggle").click(function(){
		$(this).parent().parent().find(".options:first").slideToggle();
	});
	
	obj.setCustomizationsToggleAnimation();
}

function kphp_setCustomizationsToggleAnimation(){
	var obj = this;
	
	$(obj.customizations.contentArea).height("100%");
	
	var inputs = $(obj.customizations.controller+" .control input");
	
	inputs.filter(":not(.arrow)").click(function(){
		obj.customizationsChangeCarousel($(this));
	});
	
	inputs.filter("[name='next']").click(function(){
		var a = inputs.filter(".active").attr('value');
		a++;
		if(a <= $(obj.customizations.contentArea+" "+obj.customizations.contentElementContainer).length){
			obj.customizationsChangeCarousel(inputs.filter("[value='"+a+"']"));
		}
		else return false;
	});
	//The previous button animation
	inputs.filter("[name='previous']").click(function(){
		var a = inputs.filter(".active").attr('value');
		a--;
		if(a != 0){
			obj.customizationsChangeCarousel(inputs.filter("[value='"+a+"']"));	
		}
		else return false;
	});	
}

function kphp_customizationsChangeCarousel(domEl){
	var obj = this;
	var inputs = $(obj.customizations.controller+" .control input");
	
	inputs.filter(":not(.arrow)").attr("class",'');
	domEl.attr("class","active");
	var current = domEl.attr("value");
	$(obj.customizations.contentArea).css("overflow", "hidden");
	$(obj.customizations.contentArea+" "+obj.customizations.contentElementContainer+":visible").animate({"left": "+=1026px","opacity" : "hide"},1000);
	$(obj.customizations.contentArea+" "+obj.customizations.contentElementContainer+"[count='"+current+"']").animate({"opacity" : "show"},1100, 
			function(){
				//refresh the video content
				var html = $(obj.video.id).html();
				$(obj.video.id).html(html);
				
				//set all regions to have the correct starting coordinates
				$(obj.customizations.contentArea+" "+obj.customizations.contentElementContainer).css("left","10px");
				$(obj.customizations.contentArea).css("overflow", "visible");
			}
	);
}


/*
 * The code below is from KP 2.0 and has not been objectified all the way
 * This code would only apply to the home page
 */

function kphp_ajaxSubmit(domObjId){
	var obj = this;
	
	if (siteTracker) siteTracker._trackEvent('WidgetsPersonalize',domObjId);
	var formUrl = $("#"+domObjId).attr("action");
	$.ajax({
		url: formUrl,
		data: $("#"+domObjId).serialize(),
		type: "POST",
		success: function(data,status){
			$("#"+domObjId+' .options').slideToggle();
			obj.getNewWidgetHtml(domObjId, data,'name='+domObjId);
		},
		cache: false,
		error: function(){
			return false;
		}
	});
}
function kphp_getNewWidgetHtml(objId, formUrl, query){
	var obj = this;
	
	if(formUrl != 'refresh'){
		$.ajax({
			url: formUrl+'?'+query,
			type: "GET",
			success: function(data,status){
				var domObj = $("#"+objId).parent();
				var old = domObj.children().not(':first');
				
				old.fadeOut("slow", function(){
					$(this).remove();
				});
				domObj.append(data);
				domObj.children().not(':first').hide();
				domObj.children().not(':first, .options').fadeIn("slow");
				obj.setFormDefaults();
			},
			cache: false,
			error: function(xhr,status,errorThrown){
				return false;
			}
		});
	}
	else window.location.reload();
}
function kphp_setWidgetFormDefaults(){
	var obj = this;
	
	$(".customJS .options").submit(function(){
		obj.ajaxSubmit($(this).attr('id'));
		return false;
	});
}
function kphp_showCustomOptions(){
	var obj = this;
	
	if($("#personalize").attr("action") == "edit"){
		obj.addRegionTransparencies();
		$("#CustomRegion").slideDown();
		$("#personalize span").html("Exit Editing Mode");
		$("#personalize").attr("action", 'normal');
	}
	else{
		$(".transparency").remove();
		$("#CustomRegion").slideUp();
		$("#personalize span").html("Personalize the home page");
		$("#personalize").attr("action", 'edit');
	}
}
function kphp_addRegionTransparencies(){
	var obj = this;
	
	if($("#features").html() != null){
		$("#news").after('<div id="features_transparency" class="transparency"><div class="transparency_bkgd"></div><div class="transparency_content"><span class="minimize">Minimize feature stories, news and events.</span></div></div>');
		var position = $("#features").position();
		$("#features_transparency").css("top", position.top).css("left",position.left);
		$("#features_transparency .minimize").click(function(){
			siteTracker._trackEvent('WidgetsPersonalize','FeaturesMin');
			obj.getMinObject("#features",'../_common_/actions/get/minimized.php','object=Features&state=min');
			obj.getMinObject("#news",'../_common_/actions/get/minimized.php','object=NewsEventWidget&state=min');
			obj.setCookie('name=urcustom&method=minimize','minFeatures=on');
			$("#newsMin dl.collapsed dt").click(function(){
				obj.setNewsMin();
				return false;
			});
			obj.showCustomOptions();
		});
	}	
	else if($("#featuresMin").html() != null){
		$("#newsMin").after('<div id="featuresMin_transparency" class="transparency"><div class="transparency_bkgd"></div><div class="transparency_content"><span class="maximize">Maximize feature stories, news and events.</span></div></div>');
		var position = $("#featuresMin").position();
		$("#featuresMin_transparency").css("top", position.top).css("left",position.left);
		$("#featuresMin_transparency .maximize").click(function(){
			siteTracker._trackEvent('WidgetsPersonalize','FeaturesMax');
			obj.getMinObject("#featuresMin",'../_common_/actions/get/minimized.php','object=Features&state=max');
			obj.getMinObject("#newsMin",'../_common_/actions/get/minimized.php','object=NewsEventWidget&state=max');
			obj.setCookie('name=urcustom&method=minimize','minFeatures=off');
			obj.showCustomOptions();
		});
	}
	if($("#promosMin").html() == null){
		$(".promos:last").after('<div id="promos_transparency" class="transparency"><div class="transparency_bkgd"></div><div class="transparency_content"><span class="minimize">Minimize University highlights.</span></div></div>');
		var position = $(".promos:first").position();
		$("#promos_transparency").css("top", position.top).css("left",position.left);
		$("#promos_transparency .minimize").click(function(){
			siteTracker._trackEvent('WidgetsPersonalize','PromosMin');
			obj.getMinObject(".promos",'../_common_/actions/get/minimized.php','object=Promos&state=min');
			obj.setCookie('name=urcustom&method=minimize','minPromos=on');
			obj.showCustomOptions();
		});
	}
	else{
		$("#promosMin").after('<div id="promosMin_transparency" class="transparency"><div class="transparency_bkgd"></div><div class="transparency_content"><span class="maximize">Maximize University highlights.</span></div></div>');
		var position = $("#promosMin").position();
		$("#promosMin_transparency").css("top", position.top).css("left",position.left);
		$("#promosMin_transparency .maximize").click(function(){
			siteTracker._trackEvent('WidgetsPersonalize','PromosMax');
			obj.getMinObject("#promosMin",'../_common_/actions/get/minimized.php','object=Promos&state=max');
			obj.setCookie('name=urcustom&method=refresh','minPromos=off');
			obj.showCustomOptions();
		});
	}
	$(".transparency").fadeIn();
}

function kphp_getMinObject(domObjId, formUrl, query){
	var obj = this;
	
	$.ajax({
		url: formUrl+'?'+query,
		type: "GET",
		success: function(data,status){
			var domObj = $(domObjId);			
			$(domObjId+':first').before(data);
			domObj.slideUp("slow", function(){
				$(this).remove();
			});
			if(domObjId == "#features") obj.setFeaturesMin();
			if(domObjId == '#featuresMin') obj.setFeatures();
			if(domObjId == "#news"){
				$("#newsMin").ready(function() {
					$("#newsMin dl dt").click(function(){
						obj.setNewsMin();
						return false;
					});
				});	
			}
			if(domObjId == "#newsMin"){
				$("#news").ready(function() {
					$("#news dl.collapsed dt").click(function(){
						obj.setNews();
						return false;
					});
				});	
			}
		},
		cache: false,
		error: function(xhr,status,errorThrown){
			return false;
		}
	});
}
function kphp_setCookie(query,dataSet){
	var obj = this;
	
	$.ajax({
		url: '../_common_/actions/set/cookie.php?'+query,
		type: "POST",
		data: dataSet,
		success: function(data,status){
			if(data == 'refresh') window.location.reload();
		},
		cache: false,
		error: function(xhr,status,errorThrown){
			return false;
		}
	}); 
}

/* ##########################################
 * ############# Touch Controls #############
 * ##########################################
 */
function kphp_setTouchAnimation(){     
	var obj = this;
	
	//pick touch or click
	document.body.setAttribute('ontouchmove','return;');
	if(typeof document.body.ontouchmove == 'function'){
		obj.clickType = {
				start: 'touchstart',
				move: 'touchmove',
				end: 'touchend',
				x: 'event.changedTouches[0].clientX',
				y: 'event.changedTouches[0].clientY',
				enter: 'touchend',
				exit: 'touchend'
		}
	}	
	else{
		obj.clickType = {
				start: 'mousedown',
				move: 'mousemove',
				end: 'mouseup',
				x: 'e.clientX',
				y: 'e.clientY',
				enter: 'mouseenter',
				exit: 'mouseexit'
		}		
	}	
}


/*
 * This code calls the KhoiPondHomePage object. Ideally, this would be in the config block of each page, not here.
 */
var kphp = new KhoiPondHomePage();
kphp.__construct();

