//Check what browser is being used.
var browser = navigator.appName;
var articleID = '';

// Set variables for Vimeo
var vimeoAlbum = 'album/219867';
var videoCallback = 'latestVideo';
var videosUrl = 'http://www.vimeo.com/api/v2/' + vimeoAlbum + '/videos.json?callback=' + videoCallback;
var videoID = '';
var videoThumbnail = '';
var moogaloop = false;

//no conflict jquery 
jQuery.noConflict();

jQuery(document).ready(function(){  
	
	//Set the initial heights for content windows and positions of containers
	var pageContentHeight = jQuery("#main_page_content").height();
	var pageWidth = jQuery(window).width();
	var bkgndWidth = '';
	
	if(pageContentHeight != null && pageContentHeight > 575) {
		var divFooterPosition = pageContentHeight + 125;
	} else {
		var divContainerHeight = jQuery("#page-wrap").height();
		var divFooterPosition = divContainerHeight - 33;
	}
		
	if (divContainerHeight < 575) {
		jQuery("#page-wrap").css("height", 575);
		jQuery("#footer-box").css("top", 545);
	} else {
		jQuery("#footer-box").css("top", divFooterPosition + 20);
	}
	
	//Adjust the width of the background header images
	if (pageWidth > 1280) {
		bkgndWidth = pageWidth - 1285;
		if(browser != 'Microsoft Internet Explorer' && divContainerHeight < 575) {
			bkgndWidth = bkgndWidth - 18;
		}
		jQuery("#grey_bkgnd").css("width", bkgndWidth);
		jQuery("#subnav_bkgnd").css("width", bkgndWidth);
	} else {
		bkgndWidth = 0;
		jQuery("#grey_bkgnd").css("width", bkgndWidth);
		jQuery("#subnav_bkgnd").css("width", bkgndWidth);
	}
	
	
	//If the User resizes the window, adjust the #container height
	jQuery(window).bind("resize", resizeWindow);
	function resizeWindow( e ) {
		pageContentHeight = jQuery("#main_page_content").height();
		pageWidth = jQuery(window).width();
		if(pageContentHeight != null && pageContentHeight > 575) {
			divFooterPosition = pageContentHeight + 105;
		} else {
			divContainerHeight = jQuery("#page-wrap").height();
			divFooterPosition = divContainerHeight - 30;
		}
		
		if (divContainerHeight < 575) {
			jQuery("#page-wrap").css("height", 575);
			jQuery("#footer-box").css("top", 545);
		} else {
			jQuery("#footer-box").css("top", divFooterPosition);
		}
		
		if (pageWidth > 1280) {
			bkgndWidth = pageWidth - 1285;
			if(browser != 'Microsoft Internet Explorer' && divContainerHeight < 575) {
				bkgndWidth = bkgndWidth - 18;
			}
			jQuery("#grey_bkgnd").css("width", bkgndWidth);
			jQuery("#subnav_bkgnd").css("width", bkgndWidth);
		} else {
			bkgndWidth = 0;
			jQuery("#grey_bkgnd").css("width", bkgndWidth);
			jQuery("#subnav_bkgnd").css("width", bkgndWidth);
		}
	} 
	
	//If not IE, use JQuery Blend
	if(browser != 'Microsoft Internet Explorer') {
	
		//Sponsor Logos
		jQuery(".sm-facebook a").blend();
		jQuery(".sm-twitter a").blend();
		jQuery(".sm-myspace a").blend();
		
		jQuery(".kk-signature a").blend();
		jQuery(".kkr-logo a").blend();
		jQuery(".kkf-logo a").blend();
		jQuery(".kks-logo a").blend();
		jQuery(".klub-btn a").blend();
		jQuery(".fk-btn a").blend();
		
		jQuery(".menu-news a").blend();
		jQuery(".menu-profile a").blend();
		jQuery(".menu-team a").blend();
		jQuery(".menu-schedule a").blend();
		jQuery(".menu-gallery a").blend();
		jQuery(".menu-partners a").blend();
		jQuery(".menu-about a").blend();
		jQuery(".menu-donate a").blend();
		jQuery(".menu-events a").blend();
					
	}
	
	/**
	* Highlight the submenu item that is active 
	* on the page with it's rollover effect
	*/
	var currentPage = window.location.pathname;
	var activePageArray = currentPage.split("/");
	var activePage = activePageArray[2];
	
	if(activePage == "news") { 
		jQuery("#menu-news-link").css("background-position", "0 -9px");
	}
	if(activePage == "schedules") { 
		jQuery("#menu-schedule-link").css("background-position", "0 -9px");
	}
	if(activePage == "team") { 
		jQuery("#menu-team-link").css("background-position", "0 -9px");
	}
	if(activePage == "gallery") { 
		jQuery("#menu-gallery-link").css("background-position", "0 -9px");
	}
	if(activePageArray[1] == "photos") { 
		jQuery("#menu-gallery-link").css("background-position", "0 -9px");
	}
	if(activePage == "partners") { 
		jQuery("#menu-partners-link").css("background-position", "0 -9px");
	}
	
	/**
	* KK menu Effect (News, Profile, Team, Gallery, Partners)
	* Hovering over main menu items (KK, KKF, KKR, KKS)
	* will change background background of menu
	*/
	jQuery(".kk-signature").hover(function() {
	    jQuery("#menu_bkgnd_1").addClass("grey");
		jQuery("#menu_bkgnd_2").addClass("grey");
		jQuery("#menu_bkgnd_3").addClass("grey");
		jQuery("#menu_bkgnd_4").addClass("grey");
		jQuery("#menu_bkgnd_5").addClass("grey");
	}, function() {
    	jQuery("#menu_bkgnd_1").removeClass("grey");
		jQuery("#menu_bkgnd_2").removeClass("grey");
		jQuery("#menu_bkgnd_3").removeClass("grey");
		jQuery("#menu_bkgnd_4").removeClass("grey");
		jQuery("#menu_bkgnd_5").removeClass("grey");
    });
	
	jQuery(".kkf-logo").hover(function() {
	    jQuery("#menu_bkgnd_1").addClass("blue");
		jQuery("#menu_bkgnd_2").addClass("blue");
		jQuery("#menu_bkgnd_3").addClass("blue");
		jQuery("#menu_bkgnd_4").addClass("blue");
		jQuery("#menu_bkgnd_5").addClass("blue");
	}, function() {
    	jQuery("#menu_bkgnd_1").removeClass("blue");
		jQuery("#menu_bkgnd_2").removeClass("blue");
		jQuery("#menu_bkgnd_3").removeClass("blue");
		jQuery("#menu_bkgnd_4").removeClass("blue");
		jQuery("#menu_bkgnd_5").removeClass("blue");
    });
	
	jQuery(".kks-logo").hover(function() {
	    jQuery("#menu_bkgnd_1").addClass("green");
		jQuery("#menu_bkgnd_2").addClass("green");
		jQuery("#menu_bkgnd_3").addClass("green");
		jQuery("#menu_bkgnd_4").addClass("green");
		jQuery("#menu_bkgnd_5").addClass("green");
	}, function() {
    	jQuery("#menu_bkgnd_1").removeClass("green");
		jQuery("#menu_bkgnd_2").removeClass("green");
		jQuery("#menu_bkgnd_3").removeClass("green");
		jQuery("#menu_bkgnd_4").removeClass("green");
		jQuery("#menu_bkgnd_5").removeClass("green");
    });
	
	/**
	 * Tooltips for KK Sites
	 */
	jQuery("#video_control").tooltip({
		tip: '#vc_tooltip',
		effect: "fade",
		offset: [0, 125],
		onShow: function() {
			this.getTip().delay(5000).fadeTo("slow", 0);
		}
	});
	jQuery("#news_submenu").tooltip({
		tip: '#news_tooltip',
		effect: "fade",
		offset: [0, 0],
		onShow: function() {
			this.getTip().delay(5000).fadeTo("slow", 0);
		}
	});
	jQuery("#archive_submenu").tooltip({
		tip: '#news_tooltip',
		effect: "fade",
		offset: [0, 0],
		onShow: function() {
			this.getTip().delay(5000).fadeTo("slow", 0);
		}
	});
	jQuery("#schedules_submenu").tooltip({
		tip: '#schedules_tooltip',
		effect: "fade",
		offset: [0, 0],
		onShow: function() {
			this.getTip().delay(5000).fadeTo("slow", 0);
		}
	});
	jQuery("#highlights_submenu").tooltip({
		tip: '#highlights_tooltip',
		effect: "fade",
		offset: [0, 0],
		onShow: function() {
			this.getTip().delay(5000).fadeTo("slow", 0);
		}
	});
	jQuery("#team_submenu").tooltip({
		tip: '#team_tooltip',
		effect: "fade",
		offset: [0, 0],
		onShow: function() {
			this.getTip().delay(5000).fadeTo("slow", 0);
		}
	});
	jQuery("#gallery_submenu").tooltip({
		tip: '#gallery_tooltip',
		effect: "fade",
		offset: [0, 0],
		onShow: function() {
			this.getTip().delay(5000).fadeTo("slow", 0);
		}
	});
	jQuery("#video_submenu").tooltip({
		tip: '#video_tooltip',
		effect: "fade",
		offset: [0, 0],
		onShow: function() {
			this.getTip().delay(5000).fadeTo("slow", 0);
		}
	});
	jQuery("#partners_submenu").tooltip({
		tip: '#partners_tooltip',
		effect: "fade",
		offset: [0, 0],
		onShow: function() {
			this.getTip().delay(5000).fadeTo("slow", 0);
		}
	});
	
	/**
	 * Rotates sponsors logos in bottom right corner of page
	 * Loops through sponsors for continuous flow
	 */
	jQuery(".sponsor-rotator").jCarouselLite({
		visible: 3,
		vertical: true,
		auto: 2500,
		speed: 1500,
		easing: "backinout"
	});
	
	jQuery('#kk_banner_ads').cycle({ 
		timeout: 5000,
    	speed: 1000
	});
	
	//Hide video controls initially
	if(browser != 'Microsoft Internet Explorer') {
		jQuery("#controls-box").fadeTo(0, 0);
	} else {
		jQuery("#controls-box").css("display", 'none');
	}

	//Fade Background image on home page
	jQuery("#video_thumb").click(function () {
		jQuery("#posterwall").fadeTo(2500, 0, embedVideo(videoID));
	});
		
	//Initialize the Background Video
	jQuery.getScript(videosUrl);
	
	//Show the tooltip with instructions on using the video controls
	jQuery("#news_submenu").tooltip().show();
	jQuery("#archive_submenu").tooltip().show();
	jQuery("#profile_submenu").tooltip().show();
	jQuery("#team_submenu").tooltip().show();
	jQuery("#schedules_submenu").tooltip().show();
	jQuery("#gallery_submenu").tooltip().show();
	jQuery("#video_submenu").tooltip().show();
	jQuery("#partners_submenu").tooltip().show();
	jQuery("#highlights_submenu").tooltip().show();
	
    /**
	* KK Submenu Effect (Visual Menu)
	* Hovering over menu bring the menu to the front
	* Above all other content on the page
	*/
	jQuery("#news_submenu").hover(function() {
	    jQuery("#main_page_content").css("z-index", 85);
		jQuery("#news_submenu").css("z-index", 86);
	}, function() {
    	jQuery("#main_page_content").css("z-index", 86);
		jQuery("#news_submenu").css("z-index", 85);
    });
	
	jQuery("#archive_submenu").hover(function() {
	    jQuery("#main_page_content").css("z-index", 85);
		jQuery("#archive_submenu").css("z-index", 86);
	}, function() {
    	jQuery("#main_page_content").delay(5000).css("z-index", 86);
		jQuery("#archive_submenu").delay(5000).css("z-index", 85);
    });
	
	jQuery("#profile_submenu").hover(function() {
	    jQuery("#main_page_content").css("z-index", 85);
		jQuery("#profile_submenu").css("z-index", 86);
	}, function() {
    	jQuery("#main_page_content").css("z-index", 86);
		jQuery("#profile_submenu").css("z-index", 85);
    });
	
	jQuery("#team_submenu").hover(function() {
	    jQuery("#main_page_content").css("z-index", 85);
		jQuery("#team_submenu").css("z-index", 86);
	}, function() {
    	jQuery("#main_page_content").css("z-index", 86);
		jQuery("#team_submenu").css("z-index", 85);
    });
	jQuery("#schedules_submenu").hover(function() {
	    jQuery("#main_page_content").css("z-index", 85);
		jQuery("#schedules_submenu").css("z-index", 86);
	}, function() {
    	jQuery("#main_page_content").css("z-index", 86);
		jQuery("#schedules_submenu").css("z-index", 85);
    });
	jQuery("#partners_submenu").hover(function() {
	    jQuery("#main_page_content").css("z-index", 85);
		jQuery("#partners_submenu").css("z-index", 86);
	}, function() {
    	jQuery("#main_page_content").css("z-index", 86);
		jQuery("#partners_submenu").css("z-index", 85);
    });
	
	
	//News Archive Menu Rotator
	jQuery("#archive_items").jCarouselLite({
		btnNext: ".arrow_rt",
        btnPrev: ".arrow_lt",
		visible: 3,
		speed: 1500,
		easing: "backinout",
		scroll: 3
	});
	
	//Load the news article selected
	jQuery(".thumbnail").click(function() {	
	   articleID = jQuery(this).attr('href').match(/#([0-9]+)/)[1]; //Get the Article ID
	   jQuery("#posterwall_wrapper").fadeOut("slow"); 	//Fade out the current background image
	   jQuery("#page_content_middle_skinny").load("/Application/loadarticle.php?article=" + articleID);	//Load the new article text
	   jQuery("#posterwall_wrapper").load("/Application/loadbkgnd.php?article=" + articleID);	//Load the new background image
	   pageContentHeight = jQuery("#page_content_middle_skinny").height();
	   divFooterPosition = pageContentHeight + 185;
	   jQuery("#footer-box").css("top", divFooterPosition);
	   jQuery("#posterwall_wrapper").fadeIn("slow");	//Fade in the new background image
	});
	
	//Load the partner selected
	jQuery(".partner_thumbnail").click(function() {	
	   partnerID = jQuery(this).attr('href').match(/#([0-9]+)/)[1]; //Get the Article ID
	   jQuery("#page_content_middle_skinny").load("/Application/loadpartner.php?partner=" + partnerID);	//Load the new article text
	});
	
	//Load the career highlight selected
	jQuery(".highlight_thumbnail").click(function() {	
	   highlightID = jQuery(this).attr('href').match(/#([0-9]+)/)[1]; //Get the Article ID
	   jQuery("#posterwall_wrapper").fadeOut("slow"); 	//Fade out the current background image
	   jQuery("#posterwall_wrapper").load("/Application/loadhighlight.php?highlight=" + highlightID);	//Load the new background image
	   jQuery("#posterwall_wrapper").fadeIn("slow");	//Fade in the new background image
	});
	
	//Schedule Toggle (Show/Hide extra content)
	jQuery(".schedule_extra").hide();

	jQuery("#item_more").toggle(function(){
		jQuery(this).text("[-]");
		}, function () {
		jQuery(this).text("[+]"); 
	});
	
	jQuery(".schedule_container").click(function(){
		jQuery(this).next(".schedule_extra").slideToggle("slow");
	});
	
	//Results Toggle (Show/Hide extra content)
	jQuery(".results_extra").hide();

	jQuery("#results_more").toggle(function(){
		jQuery(this).text("[-]"); 
		}, function () {
		jQuery(this).text("[+]"); 
	});
	
	jQuery(".results_container").click(function(){
		jQuery(this).next(".results_extra").slideToggle("slow");
	});
		
});

/**
* Home Page Video Player Embed Code
* Embeds latest video from Vimeo
* Background image fade out, then video appears
*/
function latestVideo(videos) {
	videoID = videos[0].id;
	videoThumbnail = videos[0].thumbnail_large;
	jQuery('#video_thumb').prepend('<img src="' + videoThumbnail + '" width="248" height="153" alt="" border="0" />');
}

//Show the Video Controls
function show_controls() {
	Swiff.remote(moogaloop, 'api_addEventListener', 'onFinish', 'video_on_stop');
	if(browser != 'Microsoft Internet Explorer') {
		jQuery("#controls-box").fadeTo('slow', 100);
		jQuery("#controls-box").css("z-index", 51);
		jQuery("#footer-box").fadeTo('slow', 0);
		jQuery("#footer-box").css("z-index", 50);
		jQuery("#sponsors-box").fadeTo('slow', 0);
		jQuery("#news-box").fadeTo('slow', 0);
		jQuery("#feature-box").fadeTo('slow', 0);
		jQuery("#video_submenu").fadeTo('slow', 0);
		jQuery(".gallery-back-btn").fadeTo('slow', 0);
			
	} else {
		jQuery("#controls-box").css("display", 'block');
		jQuery("#footer-box").css("display", 'none');
		jQuery("#sponsors-box").css("display", 'none');
		jQuery("#news-box").css("display", 'none');
		jQuery("#feature-box").css("display", 'none');
		jQuery("#video_submenu").css("display", 'none');
		jQuery(".gallery-back-btn").css("display", 'none');
	}
	
	//Show the tooltip with instructions on using the video controls
	jQuery("#video_control").tooltip().show();
	
}

function video_on_play() {
	Swiff.remote(moogaloop, 'api_play');
}

function video_on_pause() {
	Swiff.remote(moogaloop, 'api_pause');
}
function video_on_stop() {
	Swiff.remote(moogaloop, 'api_pause');
	Swiff.remote(moogaloop, 'api_seekTo', 0);
	if(browser != 'Microsoft Internet Explorer') {
		jQuery("#posterwall").fadeTo(2500, 100);
		jQuery("#controls-box").fadeTo('slow', 0);	
		jQuery("#controls-box").css("z-index", 50);
		jQuery("#footer-box").fadeTo('slow', 100);
		jQuery("#footer-box").css("z-index", 51);
		jQuery("#sponsors-box").fadeTo('slow', 100);
		jQuery("#news-box").fadeTo('slow', 100);
		jQuery("#feature-box").fadeTo('slow', 100);
		jQuery("#video_submenu").fadeTo('slow', 100);
		jQuery(".gallery-back-btn").fadeTo('slow', 100);
	} else {
		jQuery("#posterwall").fadeTo(2500, 100);
		jQuery("#controls-box").css("display", 'none');
		jQuery("#footer-box").css("display", 'block');
		jQuery("#sponsors-box").css("display", 'block');
		jQuery("#news-box").css("display", 'block');
		jQuery("#feature-box").css("display", 'block');
		jQuery("#video_submenu").css("display", 'block');
		jQuery(".gallery-back-btn").css("display", 'block');
	}
}

function embedVideo(video_id) {
	var swf_id = 'video';
	moogaloop = new Swiff('http://vimeo.com/moogaloop.swf', {
		id: swf_id,
		container: 'video',	
		width: 1280,
		height: 720,
		vars: {
			clip_id: video_id,
			show_portrait: 0,
			show_byline: 0,
			show_title: 0,
			autoplay: 1,
			js_api: 1, // required in order to use the Javascript API
			js_onLoad: 'show_controls', // moogaloop will call this JS function when it's done loading (optional)
			js_swf_id: 'video' // this will be passed into all event methods so you can keep track of multiple moogaloops (optional)
		},
		params: {
			allowscriptaccess: 'always',
			allowfullscreen: 'true',
			wmode: 'transparent'
		}
	}).toElement();
	
}

function showArchiveVideo(video_id) {

	jQuery("#posterwall").fadeTo(2500, 0, embedVideo(video_id));
	//jQuery("#posterwall").fadeTo(2500, 0);
	
}

function switchGalleryPhoto(image_url) {
	jQuery("#posterwall_wrapper").fadeTo('slow', 0);
	jQuery("#posterwall").attr("src", image_url);
	jQuery("#posterwall_wrapper").fadeTo('slow', 100);
}