// check if mobile
if ((navigator.userAgent.indexOf("iPhone") != -1) || (navigator.userAgent.indexOf("iPod") != -1) || (navigator.userAgent.indexOf("iPad") != -1) || (navigator.userAgent.indexOf("Android") != -1) || (navigator.userAgent.indexOf("BlackBerry") != -1)){
	mobile = true;
} else {
	mobile = false;
}

function setTextSize() {
	var w = $(window).width();
	if (w < 1400) {
		// don't resize the text if lower than 800
		$('body').css('font-size','14px');
	} else if(w > 1400) {
		var newSize = ((w/textSize)/100);
		fontSize = textSize*newSize
		$('body').css('font-size',fontSize+'px');
	}
	$('#content,#fader').delay(200).fadeTo(500, 1.0);
}

$(function() {
	if (home == false) {
		$('#content').css({'opacity':'0.0'});
	}
	setTextSize();

	if(!$.browser.webkit && !$.browser.mozilla){
		// setup menu scrollpane
		var menu = $('#nav');
		menu.jScrollPane({showArrows: false, autoReinitialise: true, autoReinitialiseDelay: 50});
		mapi = menu.data('jsp');
	}
});

function showMain() {
	$('.showpage').hide();
	$('.sub:visible').slideUp(function() {
		$('.main').parent('li').show();
	});
	menu = 1;
}

function hideMain() {
	$('.main').parent('li').hide();
	$('.showpage').show();
	$('#navPhotos').show();
	resizeNavPhotos();
}

function backToPage() {
	showMain();
	$('#navPhotos img').css({'display':'none'}); // hide all photos
	$('ul.sub li a').removeClass('sel'); // remove sel class
	$('#navPhotos').hide();
	menu = 0;
}

function resizeNavPhotos() {
	var navPhotoHeight = $('.n_photos').height();
	var navPhotoWidth = $('.n_photos').width()
	$('#navPhotos table, #navPhotos table td').css({'height':navPhotoHeight,'width':navPhotoWidth});
	$('#navPhotos table td img').css({'max-height':navPhotoHeight, 'max-width':navPhotoWidth});
}

function resizeSlideshow() {
	var sWidth = $('#portfolio').width();
	var sHeight = $('#portfolio').height();
	$('.p_slideshow, .p_slideshow ul, .p_slideshow ul li, .p_slideshow ul li table, .p_slideshow ul li table td, .p_slideshow ul li.video>div').css({'width':sWidth,'height':sHeight});
	$('.p_slideshow ul li table td img').css({'max-width':sWidth,'max-height':sHeight});
	if (mobile == true) {
		$('.p_slideshow ul li.video>div>div').css({'width':sWidth, 'height':sHeight});
	}
}

var hover_item_id = '';
var hover_item_index = 0;
var hover_timeout = false;
var in_next_hover_item = false;

function nextHoverItem() {
	if (!in_next_hover_item) {
		in_next_hover_item = true;
		clearTimeout(hover_timeout);
		var prev_hover_index = hover_item_index;
		hover_item_index++;
		var $hover_item = $(hover_item_id + '-' + hover_item_index);

		if ($hover_item.length < 1) {
			if (prev_hover_index > 0) hover_item_index = 0;
			$hover_item = $(hover_item_id + '-' + hover_item_index);
		}

		if ($hover_item.length > 0 && (prev_hover_index != hover_item_index)) {
			var $prev_item = $(hover_item_id + '-' + prev_hover_index);
			$prev_item.fadeTo(500, 0.0, function() {
				$prev_item.hide();
				$hover_item.fadeTo(500, 1.0, function() {
					$hover_item.css({'display' : 'inline'});
					in_next_hover_item = false;
					hover_timeout = setTimeout('nextHoverItem()', next_hover_delay);
				});
			});
		} else {
			in_next_hover_item = false;
		}
	}
}

$(document).ready(function() {
	// navigation toggle
	$('#nav a.division').live('click', function() {
		if (home == 1) {
			$('#header').hide()
			$('#logo').show();
			$('#content').css({'top':'1.4em'}).removeClass('top-bordered');
			$('#nav').css({'top':'3.2em'});
			menu = 1;
		}
		hideMain();
		$(this).parent('li').show();
		$(this).siblings('ul').slideDown(500);
		return false;
	});

	// back to page
	$('#nav').delegate('.showpage', 'click', function() {
		if (home == 1) {
			$('#header').show()
			$('#logo').hide();
			$('#content').css({'top':contentTop}).addClass('top-bordered');
			$('#nav').css({'top':'1.4em'});
			menu = 0;
		}
		backToPage();
	});

	$('ul.sub li').delegate('a', 'hover', function(e) {
		var image = $(this).attr('rel'),
				min, max;
		if (hover_item_id != '#' + image) {
			in_next_hover_item = true;
			clearTimeout(hover_timeout);
			$('#navPhotos img').clearQueue().stop().css({'display':'none'}); // hide all photos
			$('ul.sub li a').removeClass('sel'); // remove class on href

			hover_item_id = '#' + image;
			min = 0;
			max = $('.'+image).length;
			hover_item_index = Math.floor(Math.random() * max);//since we are using a zero based index instead of the usual max - min + 1
			$(this).addClass('sel');
			var $hover_item = $(hover_item_id + '-' + hover_item_index);
			$hover_item.fadeTo(500, 1.0, function() {
				$hover_item.css({'display' : 'inline'});
				in_next_hover_item = false;
				hover_timeout = setTimeout('nextHoverItem()', next_hover_delay);
			});
		} else {
			if (e.type == 'mouseenter') {
				nextHoverItem();
			}
		}
	});

	$('ul.parent li').delegate('a:not(.division)', 'hover', function(e) {
		$('ul.parent li a:not(.original_sel)').removeClass('sel'); // remove class on href
		if (e.type == 'mouseenter') {
			$(this).addClass('sel');
		}
	});
	$('ul.parent li').delegate('a.division', 'hover', function(e) {
		var image = $(this).attr('id'),
				min, max;
		$('ul.parent li a:not(.original_sel)').removeClass('sel'); // remove class on href
		if (e.type == 'mouseenter') {
			$(this).addClass('sel');
		}

		if (hover_item_id != '#' + 'division-' + image) {
			in_next_hover_item = true;
			clearTimeout(hover_timeout);
			$('#navPhotos img').clearQueue().stop().css({'display':'none'}); // hide all photos

			hover_item_id = '#' + 'division-' + image;
			min = 0;
			max = $('.division-' + image).length;
			hover_item_index = Math.floor(Math.random() * max);//since we are using a zero based index instead of the usual max - min + 1
			var $hover_item = $(hover_item_id + '-' + hover_item_index);
			$hover_item.fadeTo(500, 1.0, function() {
				$hover_item.css({'display' : 'inline'});
				in_next_hover_item = false;
				hover_timeout = setTimeout('nextHoverItem()', next_hover_delay);
			});
		} else {
			if (e.type == 'mouseenter') {
				nextHoverItem();
			}
		}
	});

	// modal click to launch overlay
	$('a.modal').live('click', function() {
		var target = $(this).attr('rel');
		if (target == 'shareOverlay'){
			var title = $(this).parent().parent().find('.news_title a').text() + ' from Management Artists';
			var slug = $(this).parent().parent().find('.news_title a').attr('href');
			slug = slug.replace('/news/', '');
			$('#nSubject').val(title);
			$('#nSlug').val(slug);
		}
		showOverlay(target);
	});

	// clear the overlay form and close
	$('a.fCancel, a.lCancel').live('click', function() {
		var $form = $('.overlay_con form');
		if ($form.length > 0) {
			$form.each(function() {
				this.reset();
			});
		}
		$('.overlay').hide();
	});

	// toggle the add/remove to lightbox tooltip
	$('a.lightbox').live('mouseover mouseout', function(e) {
		if (e.type == 'mouseover') {
			$(this).children('.lightbox_label').show();
		} else {
			$(this).children('.lightbox_label').hide();
		}
	});

	$('.biolink').bind('click', function() {
		if ($('#bio').is(':visible')) {
			hideBio();
		} else {
			showBio();
		}
	});
	$('.clientslink').bind('click', function() {
		if ($('#clientList').is(':visible')) {
			hideClientList();
		} else {
			showClientList();
		}
	});
});

$(window).bind('resize', function() {
	var throttleTimeout;
	if ($.browser.msie) {
		if (!throttleTimeout) {
			throttleTimeout = setTimeout(function(){
				reinitJScrollPane();
				throttleTimeout = null;
			},50);
		}
	} else {
		reinitJScrollPane();
	}
	setTextSize();
	resizeNavPhotos();
});

function showOverlay(target) {
	$('#'+target).show();
	var overlay = $('#'+target+' .overlay_con');
	overlay.css({'opacity':0.0});
	_height = $(overlay).height() + 40;
	overlay.css({'marginTop':'-'+(_height/2)+'px'});
	overlay.css({'opacity':1.0});
}

function showBio() {
	var toHide = '#portfolio';
	if ($('#clientList').is(':visible')) { $('.clientslink').removeClass('sel'); toHide = '#clientList'; }

	$(toHide).fadeOut(300, function() {
		$('#bio').fadeIn(300);
	});
	$('.biolink').addClass('sel');

	// setup menu scrollpane
	var bio = $('#bio');
	bio.jScrollPane({showArrows: false, autoReinitialise: true, autoReinitialiseDelay: 50});
	bapi = bio.data('jsp');
}

function hideBio(){
	$('#bio').fadeOut(300, function() {
		$('#portfolio').fadeIn(300);
	});
	$('.biolink, .clientslink').removeClass('sel');
	bapi.destroy();
}

function showClientList() {
	var toHide = '#portfolio';
	if ($('#bio').is(':visible')) { $('.biolink').removeClass('sel'); toHide = '#bio'; }

	$(toHide).fadeOut(300, function() {
		$('#clientList').fadeIn(300);
	});
	$('.clientslink').addClass('sel');

	// setup menu scrollpane
	var cl = $('#clientList');
	cl.jScrollPane({showArrows: false, autoReinitialise: true, autoReinitialiseDelay: 50});
	clapi = cl.data('jsp');
}

function hideClientList(){
	$('#clientList').fadeOut(300, function() {
		$('#portfolio').fadeIn(300);
	});
	$('.clientslink, .biolink').removeClass('sel');
	clapi.destroy();
}

function reinitJScrollPane(){
	if(!$.browser.webkit && !$.browser.mozilla){
		mapi.reinitialise();
	}else{
		$('#news');
	}
}

