$(document).ready(function () {
    /* Home Page */
    $('html').addClass('browser-is-' + $.client.browser);
    $('html').addClass('platform-is-' + $.client.os);
    if ($('#page-header ul.image-slideshow li').length > 0) {
        var eles = $('#page-header ul.image-slideshow li').length;
        var current_ele = 0;
        $(document).everyTime(5000, function (i) {
            $('#page-header ul.image-slideshow li:nth-child(' + current_ele + ')').fadeOut();
            current_ele++;
            if (current_ele > eles) current_ele = 1;
            $('#page-header ul.image-slideshow li:nth-child(' + current_ele + ')').fadeIn();
        });
    }

    /* Sports & Entertainment Page */
    $('.trans-box:nth-child(3)').addClass('third');
    $('.sidebar-gray').enable_sidebar_menu();
    $('.sidebar-gray > li:nth-child(odd)').addClass('odd');
    $('section.archive-list-box li').click(function () {
        $(this).toggleClass('open');
        return false;
    });
    $('.list-box ul li:even').addClass('even');
    $('section.case-study-gallery-box').enable_gallery();
    $('.restaurants .list-box ul').three_column_nav();

    / * Careers Page */
    $('.gray-box .img-container').add('.trans-box .img-container').add('.inner-gray-box-list .img-container').hover(function () {
        $(this).css({ cursor: 'pointer' });
        $('a', this).css({ 'text-decoration': 'underline' });
    }, function () {
        $(this).css({ cursor: 'default' });
        $('a', this).css({ 'text-decoration': 'none' });
    });
    $('.menu-box img').click(function () {
        window.location = $('a:first', $(this).parent()).attr('href');
    });
    $('.menu-box img').hover(function () {
        $('a:first', $(this).parent()).css({ 'text-decoration': 'underline' });
        $(this).css({ 'cursor': 'pointer' });
    }, function () {
        $('a:first', $(this).parent()).css({ 'text-decoration': 'none' });
        $(this).css({ 'cursor': 'default' });
    });
    $('.gray-box .img-container').add('.trans-box .img-container').add('.inner-gray-box-list .img-container').click(function () {
        $(this).css({ 'cursor': 'pointer' });
        window.location = $('a', this).attr('href');
    });
    $('.case-study-box img').click(function () {
        $(this).css({ 'cursor': 'pointer' });
        window.location = $('a', $(this).parent()).attr('href');
    });

    $('.case-study-box img').hover(function () {
        $('a:first', $(this).parent()).css({ 'text-decoration': 'underline' });
        $(this).css({ 'cursor': 'pointer' });
    }, function () {
        $('a:first', $(this).parent()).css({ 'text-decoration': 'none' });
        $(this).css({ 'cursor': 'default' });
    });

    if ($('#partners ul').length > 0) {
        var end_game = $('#partners ul li:last').offset();
        var adjust = 0;
        var adjust_amount = 1;
        $('#partners ul').everyTime(1, function () {
            adjust += adjust_amount;
            if (adjust > end_game.left - 2500) adjust_amount = -1;
            if (adjust == 0) adjust_amount = 1;
            $('#partners ul li').animate({ left: '-=' + adjust_amount }, 0);
        });
    }
});

$.fn.three_column_nav = function(){
	return this.each(function(){
		var ele = this;
		$("ul", ele).css({ 'width' : "100%"});
		var colWrap = $("ul.column").width(); //Get the width of row
		var colNum = Math.floor(colWrap / 200); //Find how many columns of 200px can fit per row / then round it down to a whole number
		var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column
		$("ul", ele).css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
		$("ul li", ele).css({ 'width' : colFixed}); //Set exact width of the re-adjusted column
	});
};

$.fn.enable_sidebar_menu = function () {
	return this.each(function(){
		$('.sidebar-gray > li:not(:first)').addClass('closed');	
		$('.sidebar-gray > li > h1 > a').click(function(){
			var menu_link = this;
			var menu_item = $(menu_link).parent().parent();
			var menu = $(menu_link).parent().parent().parent();
			if (!$(menu_item).hasClass('closed'))
			{
				$('li:not(.closed) ul', menu).slideUp('fast', function(){
					$(menu_item).addClass('closed');
				});
				return false;
			}
			if ($('li:not(.closed) ul').length < 1) 
			{
				$(menu_item).siblings().addClass('closed');
				$('ul', menu_item).slideDown('fast', function(){
					$(menu_item).removeClass('closed');
				});
			}
			$('li:not(.closed) ul', menu).slideUp('fast', function(){
				$(menu_item).siblings().addClass('closed');
				$('ul', menu_item).slideDown('fast', function(){
					$(menu_item).removeClass('closed');
				});
			});
			return false;
		});
	});
};

$.fn.enable_gallery = function () {
	return this.each(function(){
		var gallery = this;
		gallery.photo_count = $('li img', gallery).length;
		gallery.current_photo = 0;
		$('li img', gallery).click(function(){
			var new_img = $(this).attr('data-high-res');
			$('img.primary', gallery).attr('src', new_img);
			return false;
		});
		$('li.next a', gallery).click(function(){
			gallery.current_photo++;
			if (gallery.current_photo >= gallery.photo_count) gallery.current_photo = 0;
			var new_img = $($('li img', gallery).get(gallery.current_photo)).attr('data-high-res');
			$('img.primary', gallery).attr('src', new_img);
			return false;
		});
		$('li.previous a', gallery).click(function(){
			gallery.current_photo--;
			if (gallery.current_photo < 0) gallery.current_photo = gallery.photo_count-1;
			var new_img = $($('li img', gallery).get(gallery.current_photo)).attr('data-high-res');
			$('img.primary', gallery).attr('src', new_img);
			return false;
		});		
	});
};

(function() {
	
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{
				string: navigator.userAgent,
				subString: "Chrome",
				identity: "Chrome"
			},
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari",
				versionSearch: "Version"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.userAgent,
				subString: "iPhone",
				identity: "iPhone/iPod"
		    },
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
	
	};
	
	BrowserDetect.init();
	
	window.$.client = { os : BrowserDetect.OS, browser : BrowserDetect.browser };
	
    $('html').addClass('browser-is-'+$.client.browser);
	$('html').addClass('platform-is-'+$.client.os);
})();

function homepageSlideshow()
{
    if ($('#page-header ul.image-slideshow li').length > 0) 
	{
		var eles = $('#page-header ul.image-slideshow li').length;
		var current_ele = 0;
		$(document).everyTime(5000, function(i){
			$('#page-header ul.image-slideshow li:nth-child('+current_ele+')').fadeOut();
			current_ele++;
			if (current_ele > eles) current_ele = 1;
			$('#page-header ul.image-slideshow li:nth-child('+current_ele+')').fadeIn();
		});
	}
}
