$(document).ready(function() {
    
    // Main Image Randomizer
    var media_path = "http://media.davenport.edu/sites/default/files/";
    var image_types = ['b', 't', 'h'];
    var main_image_type = Math.floor(Math.random()*3);
    var main_image_num = Math.floor(Math.random()*3) + 1;
    
    $('#brow_img_general').attr('src', media_path + image_types[main_image_type] + main_image_num + ".jpg");
    $('#brow_img_business').attr('src', media_path + get_main_image("b", main_image_type, main_image_num));
    $('#brow_img_tech').attr('src', media_path + get_main_image("t", main_image_type, main_image_num));
    $('#brow_img_health').attr('src', media_path + get_main_image("h", image_types[main_image_type], main_image_num));

	// News and Events Scroller
	$('#scroll_roll').cycle({
		fx: 'scrollHorz',
		timeout: 10000,
		prev:   '#scroll_arrow_left', 
    	next:   '#scroll_arrow_right'
	});
	
	// Brow Main Image Scroller
	$('#img_holder').cycle({
		timeout: 0,
		fx: 'scrollHorz',
		speed: 950,
		pager:'#nav',
		pagerEvent: 'mouseenter',
		allowPagerClickBubble: true,
		pagerAnchorBuilder: paginate
	});
	$('#title_holder').cycle({
		fx: 'scrollHorz',
		timeout: 0,
		speed: 800,
		pager:'#nav',
		pagerEvent: 'mouseenter',
		allowPagerClickBubble: true,
		cleartype:false,
		pagerAnchorBuilder: dup_paginate
	});
	
	// When user hasn't interacted for 10 seconds reset the brow
	$('#nav').bind("mouseleave", function() {
		waitingForReset = true;
		setTimeout(function() {
			if(waitingForReset) {
				$('#title_holder').cycle(0);
				$('#img_holder').cycle(0);
			}
		}, 15000);
	});
	
	// Cancel reset if user interacts
	$('#nav').bind("mouseenter", function() {
		waitingForReset = false;
	});
	
	// **** Fade page elements in ****
	if ($.cookie("page_fade"))
	{
		var base_delay = 0;
		var long_delay = 0;
		var fadein_time = 0;
	}
	else
	{	
		var base_delay = 750;
		var long_delay = 2000;
		var fadein_time = 750;
	}
	
	//$('div#content_holder > div').css('display', 'none');
	$('div#top_bar > div').css('display', 'none');
	$('div#title_holder').css('display', 'none');
	$('div#nav').css('display', 'none');
	$('div#search_box').css('display', 'none');
	//$('div#brow_bar_bottom').css('display', 'none');
	
	$('div#top_bar > div').delay(base_delay + long_delay).fadeIn(fadein_time);
	$('div#title_holder').delay(base_delay).fadeIn(fadein_time);
	$('div#nav').delay(base_delay + long_delay).fadeIn(fadein_time);
	$('div#search_box').delay(base_delay + long_delay).fadeIn(fadein_time);
	//$('div#brow_bar_bottom').delay(base_delay + long_delay).fadeIn(fadein_time);
	//$('div#content_holder > div').delay(base_delay + long_delay).fadeIn(500);
	
	// Set cookie to disable fade in after first visit
	if (!($.cookie("page_fade")))
	{
		$.cookie("page_fade", "false", {expires: 30} );
	}
	// **** END Fade page elements in ****
	
	
	/* Fat Footer Site Index
	$('#site_index_content').cycle({ 
	    fx:     'fade', 
	    speed:  'fast', 
	    timeout: 0, 
	    cleartype:false,
	    pager:  '#site_index_nav', 
	     
	    // callback fn that creates a thumbnail to use as pager anchor 
	    pagerAnchorBuilder: function(idx, slide) { 
	        return '<li><a href="#">' + $(slide).attr('value') + '</a></li>'; 
	    } 
	});
	
	*/
	
	// Set initial brow height
	var win_height = $(window).height();
	var brow_height = 753;
	var visible_height = 700;
	
	if (win_height < visible_height)
	{
		brow_height = brow_height - (visible_height - win_height);
		
		if (brow_height < 610)
		{
			brow_height = 610;
		}
		
		$('div#brow_holder').css('height', brow_height + 'px');	
	}	
	// Resize brow on window resize
	/*
	$(window).resize(function() {
							  
		win_height = $(window).height();
  		
		if (win_height < visible_height)
		{
				brow_height = brow_height - (visible_height - win_height);
			
				if (brow_height < 610)
				{
					brow_height = 610;
				}
			
			$('div#brow_holder').css('height', brow_height + 'px');	
		}		
	});
	*/
	
	$('#main_search_field').focus(function() {
		$(this).val('');
		$(this).css('color', '#555');
	});
	
	var i;
	for (i in document.images) {
    	if (document.images[i].src) {
        	var imgSrc = document.images[i].src;
        	if (imgSrc.substr(imgSrc.length-4) === '.png' || imgSrc.substr(imgSrc.length-4) === '.PNG') {
            	document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
        	}
    	}
	}

});

var unbound_func;

function anim_before()
{
	unbound_func = $("#business_box_link").data("events");
	//unbound_func = $("#business_box_link,#tech_box_link,#health_box_link").unbind("mouseover");
	alert(unbound_func);
	//$("#business_box_link,#tech_box_link,#health_box_link").attr("disabled","disabled");
}

function anim_after()
{
	//$("#business_box_link,#tech_box_link,#health_box_link").bind("mouseover", unbound_func);
	//alert(unbound_func.data);
	//$("#business_box_link,#tech_box_link,#health_box_link").removeAttr("disabled");
}

function paginate(ind, el)
{
	if (ind == 0) return '<div id="home_option" class="option"></div>';
	
	if (ind == 1) return '<div id="business_option" class="option"><div class="title"><a href="/college-business" style="color: #fff;">COLLEGE OF BUSINESS</a></div><div class="content"><a href="/programs/business">Degrees and Programs in Business</a><br /><a href="/programs/business/bachelors-degree/marketing-bba">Marketing</a><br /><a href="/programs/business/bachelors-degree/sport-management-bba">Sports Management</a><br /><a href="/programs/business/bachelors-degree/paralegal-studies-bs">Paralegal Studies</a><br /><a href="/programs/business/bachelors-degree/human-resource-management-bba">Human Resource Management</a><br /><strong><a href="/programs/business">MORE &gt;</a></strong></div></div>';
	
	if (ind == 2) return '<div id="tech_option" class="option"><div class="title"><a href="/college-technology" style="color: #fff;">COLLEGE OF TECHNOLOGY</a></div><div class="content"><a href="/programs/technology">Degrees and Programs in Technology</a><br /><a href="/programs/technology/bachelors-degree/computer-information-systems-major-computer-gaming-and-simulation-bs">Computer Gaming and Simulation</a><br /><a href="/programs/technology/bachelors-degree/networking-technology-major-network-security-bs">Network Security</a><br /><a href="/programs/technology/bachelors-degree/security-and-information-assurance-bs">Security and Information Assurance</a><br /><a href="/programs/technology/bachelors-degree/technology-management-bs">Technology Management</a><br /><strong><a href="/programs/technology">MORE &gt;</a></strong></div></div>';
	
	if (ind == 3) return '<div id="health_option" class="option"><div class="title"><a href="/college-health-professions" style="color: #fff;">COLLEGE OF HEALTH PROFESSIONS</a></div><div class="content"><a href="/programs/health-professions">Degrees and Programs in Healthcare</a><br /><a href="/programs/health-professions/bachelors-degree/bachelor-of-science-in-nursing-completion-program-bs">BSN - Completion Program</a><br /><a href="/programs/health-professions/bachelors-degree/health-services-administration-bs">Health Services Administration</a><br /><a href="/programs/health-professions/bachelors-degree/health-information-management-bs">Health Information Management</a><br /><a href="/programs/health-professions/bachelors-degree/bachelor-of-science-in-nursing-pre-licensure-bs">BSN - Pre-Licensure</a><br /><strong><a href="/programs/health-professions">MORE &gt;</a></strong></div></div>';
	
	if (ind == 4) return '<div id="aas_option_holder"><div id="aas_option" class="option"><div class="title"><a href="/college-arts-and-sciences" style="color: #fff;">COLLEGE OF ARTS AND SCIENCES</a></div></div></div>';
}

function dup_paginate(idx, slide) 
{
	// return selector string for existing anchor
    return '#nav div.option:eq(' + idx + ')'; 
}

// Get main image for frontpage top
var get_main_image = function (img_name, selected_type, selected_num)
{
	
	var img_num = Math.floor(Math.random()*3) + 1;
	
	if (selected_type === img_name)
	{		
		// alert ("hit");
		
		while (img_num === selected_num)
		{
			img_num = Math.floor(Math.random()*3) + 1;
		}
	}
	
	return img_name + img_num + ".jpg";
}

