$(function () {
	
	var lightbox = $('#lightbox');
	var lightboxInner = $('#lightboxInner');
	var lightboxWrapper = $('#lightboxWrapper');
	var lightboxBackground = $('#lightboxBackground');
	var lightboxClose = $('#lightboxClose');
	
	// Center Lightbox
	//lightbox.css({left:$(document).width()/2 - lightbox.width()/2});
	
	var setLightboxDimensions = function (width, height, padding) {
		lightbox.css({width:width + 'px', height:height + 'px'});
		lightboxInner.css({width:width-(padding*2) + 'px', height:height-(padding*2) + 'px', top:padding + 'px', left:padding + 'px'});
	};
	
	var addTreatment = function () {
		lightbox.addClass('treatment');
	};
	
	var removeTreatment = function () {
		lightbox.removeClass('treatment');
	};
	
	var showLightbox = function () {
		
		lightboxWrapper.show();
		
		if (lightboxBackground.css('zoom') && lightboxBackground.css('zoom') != "normal") {
			lightboxBackground.fadeIn('fast');
			lightboxBackground.fadeTo('fast', 0.5);
		} else {
			lightboxBackground.fadeIn('fast');
		}
		
		lightbox.animate({top:'0'}, 300);
		
	};
	
	var hideLightbox = function () {
		
		lightboxBackground.fadeOut('fast');
		
		lightbox.animate({top:'-550px'}, 300, function () {
			lightboxWrapper.hide();
			lightboxInner.html('');
			removeTreatment();
		});
		
		window.location = '#';
		
	};
	
	var loadLightboxAHAH = function (href) {
		lightboxInner.load(href, null, function () {
			window.location = '#' + href;
			showLightbox();
		});
	};
	
	var loadLightboxImage = function (href) {
		lightboxInner.html('<img src="' + href + '" />');
		window.location = '#' + href;
		showLightbox();
	};
	
	var loadLightboxIFrame = function (href) {
		lightboxInner.html('<iframe src="' + href + '" scrolling="no" frameborder="0"></iframe>');
		showLightbox();
	};
	
	var loadLightboxVideo = function (href) {
		lightboxInner.html('<div id="lightboxReplacement"></div>');
		swfobject.embedSWF("/swf/video-player.swf", "lightboxReplacement", "400", "355", "9.0.0", "/swf/expressInstall.swf", {video:href}, {allowScriptAccess:"sameDomain", wmode:"transparent"});
		showLightbox();
	};
	
	var loadLightboxSWF = function (href) {
		lightboxInner.html('<div id="lightboxReplacement"></div>');
		swfobject.embedSWF(href, "lightboxReplacement", "400", "355", "9.0.0", "/swf/expressInstall.swf", {allowScriptAccess:"sameDomain", wmode:"transparent"});
		showLightbox();
	};
	
	lightboxBackground.click(function () {
		hideLightbox();
		this.blur();
		return false;
	});
	
	lightboxClose.click(function () {
		hideLightbox();
		this.blur();
		return false;
	});
	
	
	// HOOKUP: download, availability, news -----
	
	$('.button-download a, .button-availability a, .button-news a').click(function () {
		
		var parent = $(this.parentNode);
		
		// Width/Height
		if (parent.hasClass('button-download')) {
			addTreatment();
			setLightboxDimensions(412, 482, 25);
		} else if (parent.hasClass('button-availability')) {
			addTreatment();
			setLightboxDimensions(706, 457, 9);
		} else {
			removeTreatment();
			setLightboxDimensions(418, 373, 9);
		}
		
		var href = $(this).attr('href');
		
		// If has "ignore" class, skip it
		if (parent.hasClass('ignore')) {
			return true;
		}
		
		var isInternal = (href.indexOf('http:') === -1);
		
		
		// If link to a pdf or flv, don't capture it
		if (href.indexOf('.pdf') !== -1) {
			return true;
		}
		
		// Set lightbox content
		try {
			
			if (isInternal) {
				loadLightboxIFrame(href + '?layout=none');
				window.location = '#' + href;
			} else {
				loadLightboxIFrame(href);
			}
			
		} catch (e) {
			// error!
			window.location = href;
		}
		
		this.blur();
		return false;
	});
	
	
	// HOOKUP: videos -----
	
	$('.button-videos a, .advisor .videos ul li a').click(function () {
		
		var href = $(this).attr('href');
		var parent = $(this.parentNode);
		
		removeTreatment();
		
		var lightboxWidth = 418;
		var lightboxHeight = 373;
		
		if (parent.metadata().width) {
			lightboxWidth = parent.metadata().width;
		}
		
		if (parent.metadata().height) {
			lightboxHeight = parent.metadata().height;
		} else if (href.indexOf(".swf") !== -1) {
			lightboxHeight = 393;
		}
		
		setLightboxDimensions(lightboxWidth, lightboxHeight, 9);
		
		// If has "ignore" class, skip it
		if (parent.hasClass('ignore')) {
			return true;
		}
		
		if (href.indexOf('http:') === -1 && href.indexOf('.flv') === -1 && href.indexOf('.swf') === -1) {
			href += "?layout=none";
		}

		try {
			
			if (href.indexOf(".flv") !== -1) {
				loadLightboxVideo(href);
			} else if (href.indexOf(".swf") !== -1) {
				loadLightboxSWF(href);
			} else {
				loadLightboxIFrame(href);
			}
			
		} catch (e) {
			// error!
			window.location = href;
		}
		
		this.blur();
		return false;
	});
	
	
	// HOOKUP: generic lightbox -----
	
	$('.lightbox a').click(function () {
		
		var href = $(this).attr('href');
		var parent = $(this.parentNode);
		
		removeTreatment();
		
		// If has "ignore" class, skip it
		if (parent.hasClass('ignore')) {
			return true;
		}
		
		var lightboxWidth = 418;
		var lightboxHeight = 373;
		
		if (parent.metadata().width) {
			lightboxWidth = parent.metadata().width + 18;
		}
		
		if (parent.metadata().height) {
			lightboxHeight = parent.metadata().height + 40;
		}
		
		setLightboxDimensions(lightboxWidth, lightboxHeight, 9);
		
		try {
			loadLightboxImage(href);
		} catch (e) {
			// error!
			window.location = href;
		}
		
		this.blur();
		return false;
	});
	
});