var str = "test.html";
var videoIndex = 0;
var menuTitle = '';

$(function() {
	//loads the flash movie SWFObject
	var flashvars = {};
	var params = {};
	params.menu = "false";
	params.quality = "best";
	params.scale = "noscale";
	params.allowfullscreen = "true";
	params.wmode = "opaque";
	var attributes = {};
	attributes.id = "indexFlash";
	attributes.name = "HomeFlash";
	attributes.align = "middle";
	swfobject.embedSWF("flash/index.flash.swf", "homeContent", "100%", "100%", "9.0.0", "flash/expressInstall.swf", flashvars, params, attributes);
		
	
	//Creates and Sets up the initial Overlay
	$("#portfolio").overlay({
		// some mask tweaks suitable for facebox-looking dialogs
		mask: {
		// you might also consider a "transparent" color for the mask
		color: '#231f20',
		// load mask a little faster
		loadSpeed: 100,
		// very transparent
		opacity: 0.76,
		
		fixed: false,
		
		closeOnEsc: true,
		
		onBeforeLoad: function() {
			// grab wrapper element inside content
			if(window.console){
				console.log("called onBeforeLoad");
			}
			//Loads the new content into the content wrapper with var str = number of the portfolio
			loadPage("#contentWrap", str, videoIndex);
		},
		onLoad: function(){
			//centers the overlay in the browser window - top to bottom, left to right
			centerOverlay();
		},
		onClose: function(){
			$('#contentWrap').html('');
			$('#homeContent').show();
		}
	},
	// disable this for modal dialog-type of overlays
	closeOnClick: false,
	load: false
	});
	
	//activates the overlay menu dropdown
	$('#menu').hover(  
		function () {  
		//show its submenu  
			$('.dropdown', this).stop(true, true).slideDown(50);
			$(this).addClass('active');
			$('div.description').children().css('opacity', '.3');
		},   
		function () {  
			//hide its submenu  
			$('.dropdown', this).stop(true, true).slideUp(50, function() {
				// Animation complete.
				$('#menu').removeClass('active');
				$('div.description').children().css('opacity', '1');
			});
		}  
	);
	
	menuTitle = $('#menu_title').text();
	
	//
	//Controls for the Menu Links
	//
	$('#menu .dropdown div li').click(function(e){
		var target = $(this).attr('rel');
		videoIndex = $(this).attr('loc');
		loadPage("#contentWrap", $(this).attr('rel'));
		$('#menu_title').text(menuTitle + " : " + $(this).attr('alt'));
		return false;
	});
});

///
///Called by the Flash to launch the Portfolio Lightbox jQuery Tools Overlay
///
launchPortfolio = function(str2, name, indx){
	str = str2;
	
	if(indx != null || indx != undefined){
		videoIndex = indx;
	} else {
		videoIndex = 0;
	};
	
	$('#menu_title').text(menuTitle + " : " + name);
	
	var api = $("#portfolio").data("overlay");
	api.load();
	$("#portfolio").css('position', 'absolute');
	$('#homeContent').hide();

};

///
/// Load a page via AJAX.
///
function loadPage(obj, portfolio, successCallback, errorCallback) {
    centerOverlay();
	$('#contentWrap').html(''); //Loading text or image
	$('#content-loader').show('');
	
    if (portfolio != null || portfolio != undefined) {
        var contentPath = 'gallery/' + portfolio;

        try {
           	//Create AJAX request to get content.
            $.ajax({
                url: contentPath +'/index.html',
                cache: false,
                success: function (data) {
					$('#content-loader').hide('');
                    $('#content-loader-error').hide();
                    $('#contentWrap').html(data); //Load ajax result as content

                    //load the corresponding script for this page
                    try {
                        $.ajax({
                            url: contentPath + '/init.js',
                            cache: false,
                            dataType: 'script', //no need to add to the DOM, 'script' data type executes the script
                            success: function (data, status) { if (window.console) console.log('successfully loaded /portfolio' + portfolio + '/init.js'); },
                            error: function (xhtr, status, err) { if (window.console) console.error(err); }
                        });
                    } catch (err) { console.error(err); }
                },
                error: function (xhtr, status, err) {
                    $('#content-loader').hide('');
                    $('#content-loader-error').show();
                    location.hash = "error";

                    if (errorCallback != null && errorCallback != undefined)
                        errorCallback(obj);
                }
            });
        } catch (err) { if (window.console) console.log(err); }
    }
    else {
        $('#content-loader').html('');
        $('#content-loader-error').show();
        location.hash = "error";
        errorCallback(obj);
    }
}

///
///Centers the Lightbox on Window Resize
///			
$(window).resize(function() {
	centerOverlay();
});

centerOverlay = function() {
	var screenHeight = $(window).height();
	var screenWidth = $(window).width();
	var lbTop = screenHeight/2 - 315;
	var lbLeft = screenWidth/2 - 600;
	if (lbTop < 0){
		lbTop = 0;
	}
	if (lbLeft < 0){
		lbLeft = 0;
	}
	
	//if(window.console){
	//	console.log("screenHeight = "+screenHeight+", screenWidth = "+screenWidth);
	//}
			
	if (screenHeight < $('#portfolio').height()){
		$("#exposeMask").height($('#portfolio').height()+2);
	} else {
		$("#exposeMask").height(screenHeight);
	}
	
	if (screenWidth < $('#portfolio').width()){
		$("#exposeMask").width($('#portfolio').width()+2);
	} else {
		$("#exposeMask").width(screenWidth);
	}
	
	$("#portfolio").css({'top' : lbTop, 'left' : lbLeft});

};

