function getGalleryCoordinates() {
	var viewport = {
		width: jQuery(window).width(),
		height: jQuery(window).height()
	}
	
	return {
		top: $('ul.global_media').height() + 100,
		left: $('div#sidebar_toggle').width(),
		width: viewport.width - $('div#sidebar_toggle').width() - $('div#right_thumbnails_wrapper').width(),
		height: viewport.height - 200 - $('ul.global_media').height()
	};
}

function showLinkColumn() {
	document.getElementById('lhb_movie').showLinkColumn();
}

function hideLinkColumn() {
	document.getElementById('lhb_movie').hideLinkColumn();
}

var play_frames = 4;

$(function() {
            $('#small_cubes').hover(
                function() {
                    $('#small_cubes').sprite({
                        fps: 9,
                        no_of_frames: 4,
						play_frames:play_frames 
                    });
					play_frames = 3;
                },function(){
					$('#small_cubes').sprite({
            			fps: 50,
            			no_of_frames: 4,
            			play_frames: 3,
            			rewind: true
        });	
				}
            );
        });

$(function() { 
	/* global navigation */
	$('div.dropdown').hide();
	$('div.dropdown').siblings().mouseover(showMenu);
	$('div.dropdown').siblings().mouseout(leaveMenu);
	$('div.dropdown').mouseover(showMenu);
	$('div.dropdown').mouseout(leaveMenu);
	$('div.homepage_open').show();

	$( "#toggle_section" ).accordion({
		collapsible: true,
		active:false,
		autoHeight:false
	});
	
	$('#toggle_section').bind('accordionchange', function(event, ui) {
		$('#main_area').animate({
			scrollTop: ui.options.active * 69 + 25
		}, 550);
	});
	
	$( "#toggle_section2" ).accordion({
		collapsible: true,
		active:false,
		autoHeight:false
	});
	
	$( ".toggle_category" ).click(function(){
		$(this).next().slideToggle();
	});
	
	// hide the third menu on the homepage due to modx being unable to style that one differently
	
	if(swfobject.hasFlashPlayerVersion('10.2') && $('div.homepage_open').length) {
		$('div.dropdown').detach();
		
		$('a.wws_nav').eq(2).mouseover(function() {
			showLinkColumn();
		});
		$('a.wws_nav').eq(2).mouseout(function() {
			hideLinkColumn();
		});
	}
	else {
		var dropdown = $('div.homepage_open').eq(2);
		dropdown.removeAttr('style');
		dropdown.css('display', 'none');
	}

	// converting span tags to anchors due to modx forcing anchor tags to be outside of the accordion anchors
	var jobPositions = $('span.job-category-title');
	jobPositions.each(function() {
		$(this).replaceWith('<a href="' + $(this).prev('span.job-category-uri').html() + '" class="accordion_link">' + $(this).html() + '</a>');
	});

	var viewport = {
		width: jQuery(window).width(),
		height: jQuery(window).height()
	}
	
	var gallery = getGalleryCoordinates();

	// for testing
	//$('body').append('<div style="background: red; position: absolute; top: ' + gallery.top + 'px; left: ' + gallery.left + 'px; width: ' + gallery.width + 'px; height: ' + gallery.height + 'px;">&nbsp;</div>');


	function random(min, max) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}
	
	// randomize image tips
	var imageTipLocations = [
		{top: '250px', left: '800px'},
		{top: '200px', left: '500px'},
		{top: '450px', left: '1000px'},
		{top: '300px', left: '300px'}
		
	];
	
	$('div.thumbnail_section').each(function() {
		$(this).find('div.image_tip').each(function(index) {
			var tipLocation = imageTipLocations[index];
			
			if(tipLocation !== undefined) {			
				$(this).css({
					top: tipLocation.top,
					left: tipLocation.left
				});
			}
			
			$(this).data('top', $(this).position().top);
			$(this).data('left', $(this).position().left);
		});
	});

 	var sidebarOpen = true;
 	
 	$('a.accordion_link').click(function(){
		$('#toggle_section').accordion('disable');
		window.open($(this).attr('href'));
		
		setTimeout(function() {
		  $('#toggle_section').accordion('enable');
		}, 250);
	});
 	
 	$('#toggle_button').click(function() {
 		if(sidebarOpen) {
 			$('#sidebar_toggle').animate({left: -256}, function() {
 				$('#toggle_button').addClass('toggle_button_closed');
 				sidebarOpen = false;
 			});
 		}
 		else {
 			$('#sidebar_toggle').animate({left: 0}, function() {
 				$('#toggle_button').removeClass('toggle_button_closed');
 				sidebarOpen = true;
 			});
 		}
 	});
	
 	var thumbs = $('div#right_thumbnails_wrapper div.thumbnail a');
 	
	thumbs.click(function() {
		var fullsize = $(this).siblings('span').html();
		$('div#image_bg img').attr('src', fullsize);
		thumbs.removeClass('active');
		
		thumbs.each(function() {
			$(this).find('img').attr('src', ($(this).find('img').attr('src').replace('-bw', '')).replace('.', '-bw.'));
		});
		
		$(this).find('img').attr('src', $(this).find('img').attr('src').replace('-bw', ''));
		$(this).addClass('active');
		
		$('div.thumbnail_section').hide();
		$('div.thumbnail_section').eq(thumbs.index($(this))).show();
	});
 	
 	var thumbHover = function() {
 		var image = $(this).find('img');
 		var src = image.attr('src');
 		if(!$(this).hasClass('active'))
 			image.attr('src', src.indexOf('-bw.') == -1 ? src.replace('.', '-bw.') : src.replace('-bw.', '.'));
 	}
 	
 	thumbs.mouseover(thumbHover);
 	thumbs.mouseout(thumbHover);
 	
 	thumbs.eq(0).click();
 	
	$('div.thumbnail').eq($('div.thumbnail').length-1).addClass('bottom');
	
 	$('div#image_tips_wrapper a.toggle').click(function() {
 		if($(this).hasClass('active')) {
 			$(this).siblings('div.image_tip_content_wrapper').hide();
 			$(this).removeClass('active');
 		}
 		else {
 			$(this).siblings('div.image_tip_content_wrapper').show();
 			$(this).addClass('active');
 		}
 	});
});

var closeTimer;

function showMenu(){
	clearTimeout(closeTimer);
	var target = $(this).hasClass('dropdown') ? $(this) : $(this).siblings('div.dropdown');
	hideAllMenus();
	target.show();
	target.siblings('a').addClass('nav-on');
}

function leaveMenu(menu){
	var target = $(this).hasClass('dropdown') ? $(this) : $(this).siblings('div.dropdown');
	closeTimer=setTimeout('hideAllMenus()',750);
}

function hideAllMenus(){
	var menus = $('div.dropdown');
	menus.hide();
	menus.siblings('a').removeClass('nav-on');
	$('div.homepage_open').show();
	$('div.homepage_open').eq(2).hide();
}

function setFlashWidth(movieId, newW){
	var object = document.getElementById(movieId);
	if(object)
		object.style.width = newW+"px";
}

function setFlashHeight(movieId, newH){
	var object = document.getElementById(movieId);
	if(object)
		document.getElementById(movieId).style.height = newH+"px";		
}

function setFlashSize(movieId, newW, newH){
	setFlashWidth(movieId, newW);
	setFlashHeight(movieId, newH);
}

var _viewportcache;

jQuery(window).resize(function() {
	var viewport = {
		width: jQuery(window).width(),
		height: jQuery(window).height()
	}

	if(_viewportcache) {
		var differenceX = viewport.width - _viewportcache.width;
		var differenceY = viewport.height - _viewportcache.height;
		
		var gallery = getGalleryCoordinates();		
		
		$('div.thumbnail_section').each(function() {
			$(this).find('div.image_tip').each(function() {
			
				var imageTip = $(this);
			
				imageTip.data('top', imageTip.data('top') + differenceY);
				imageTip.data('left', imageTip.data('left') + differenceX);
				
				imageTip.css({
					top: Math.max(imageTip.data('top'), gallery.top) + 'px',
					left: Math.max(imageTip.data('left'), gallery.left) + 'px'
				});
			});
		});
	}

	var mainArea = jQuery('div#main_area');
	
	if(mainArea.length) {
		mainArea.css({
			width: (viewport.width - jQuery('div#sidebar').width()) - parseInt(mainArea.css('padding-left').replace('px', '')) - parseInt(mainArea.css('padding-right').replace('px')) + 'px',
			height: (viewport.height - $('ul.global_media').height() - 128 - parseInt(mainArea.css('padding-bottom').replace('px', '')) - parseInt(mainArea.css('padding-top').replace('px', '')) + 57) + 'px'
		});
	}
	
	$('#image_bg').backgroundScale({
      imageSelector: "#image_bg img",
      centerAlign: true,
      containerPadding: 0
  });
  
  $('#image_bg').css('height', (viewport.height - $('div#header').height() - 72) + 'px');
	
	$('#image_bg img').mousedown(function(event) {
		if(event.button == 2) return true;
		return false;
	});

	$('#sidebar, div.sidebar_toggle_info').css('height', (viewport.height - 71 - 15 - $('div#header').height() - 56) + 'px');

	$('#right_thumbnails_wrapper').css('top', viewport.height * 0.1);

	$('div.thumbnail').eq($('div.thumbnail').length-1).addClass('bottom');

	$('#lhb').css('position', 'absolute');
	$('#lhb').css('top', '37px');

	setFlashSize('lhb_movie', viewport.width, viewport.height-109);
	
	
	
	_viewportcache = viewport;
});

window.onload = function() {
	jQuery(window).resize();
}
