var content = null;

function init()
{		  
	//if url # value is set
	if (location.hash.substring(1) != "")
	{
		getPage(location.hash.substring(1) + ".asp");
	}
	else
	{
		getPage(location.pathname.substring(location.pathname.lastIndexOf('/') + 1));	
	}
}

function getPage(option)
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer 6+
		try
    	{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
  		catch (e)
    	{
			//Internet Explorer 5.5
			try
      		{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
      		{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	
	content = document.getElementById('pgcontent');

	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
	  	{
			content.innerHTML=xmlHttp.responseText;
			//doDelay();
			applysIFR();
			doShadowbox();
			doZoom();
	  	}
	}
	
	//Page content
	if(option != "")
	{
			tempLoc = option.substring(option.length-4);
			if(tempLoc == ".asp"){
				location.hash = option.substring(0, option.length-4);
			}
			else{
				location.hash = option;
				option = option+'.asp'
			}
			xmlHttp.open("GET","pgcontent/"+option,true);
			xmlHttp.send(null);
	}
	else
	{
			location.hash = 'index';
			xmlHttp.open("GET","pgcontent/index.asp",true);
			xmlHttp.send(null);
	}
	
	if (option != "index.asp"){
		$("#longinesBanner").hide();
		//alert(option);
		console.log(option);
	}
}

function doDelay()
{
	$(document).ready(function(){ 
        $("ul.sf-menu").superfish({ 
            animation: {opacity:'show'},   // slide-down effect without fade-in 
			speed:     'fast',
            delay:     500            // 1.2 second delay on mouseout 
        }); 
    });
}

function doShadowbox()
{
	Shadowbox.clearCache();
	Shadowbox.setup();
}

//sFIR
function applysIFR()
{
	//apply sIFR after page content is loaded
	if(typeof sIFR == "function")
	{
		// This is the preferred "named argument" syntax
		sIFR.replaceElement(named({sSelector:"body h1", sFlashSrc:"corporateS.swf", sColor:"#715d23", sLinkColor:"#000000", sBgColor:"opaque", sHoverColor:"#CCCCCC", sWmode:"transparent", nPaddingTop:20, nPaddingBottom:10, sFlashVars:"textalign=left&offsetTop=0"}));
		sIFR.replaceElement(named({sSelector:"body h2", sFlashSrc:"corporateS.swf", sColor:"#0069aa", sLinkColor:"#000000", sBgColor:"opaque", sHoverColor:"#CCCCCC", sWmode:"transparent", nPaddingTop:10, nPaddingBottom:7, sFlashVars:"textalign=left&offsetTop=0"}));
	};
}
	
//zoom tab
function doZoom()
{
	$.fn.zoomtabs = function (zoomPercent, easing) {
        if (!zoomPercent) zoomPercent = 10;
        
        return this.each(function () {
            var $zoomtab = $(this);
            var $tabs = $zoomtab.find('.tabs');
            var height = $tabs.height();
            
            var panelIds = $tabs.find('a').map(function () {
                return this.hash;
            }).get().join(',');
            
            $zoomtab.find('> div').scrollTop(0);
            
            var $panels = $(panelIds);
            var images = [];
            
            $panels.each(function () {
                var $panel = $(this),
                    bg = ($panel.css('backgroundImage') || "").match(/url\s*\(["']*(.*?)['"]*\)/),
                    img = null;
                
                if (bg !== null && bg.length && bg.length > 0) {
                    bg = bg[1];
                    img = new Image();
                    
                    $panel.find('*').wrap('<div style="position: relative; z-index: 2;" />');                    
                    $panel.css('backgroundImage', 'none');
                    
                    $(img).load(function () {
                        var w = this.width / 10;
                        var wIn = w / 100 * zoomPercent;
                        var h = this.height / 10;
                        var hIn = h / 100 * zoomPercent;
                        var top = 0;
                        
                        var fullView = {
                            height: h + 'em',
                            width: w + 'em',
                            top: top,
                            left: 0
                        };
                                                
                        var zoomView = {
                            height: (h + hIn) + 'em',
                            width: (w + wIn) + 'em',
                            top: top,
                            left: '-' + (wIn / 2) + 'em'
                        };
                        
                        $(this).data('fullView', fullView).data('zoomView', zoomView).css(zoomView);

                    }).prependTo($panel).css({'position' : 'absolute', 'top' : 0, 'left' : 0 }).attr('src', bg);
                    
                    images.push(img);
                }
            });
            
            function zoomImages(zoomType, speed) {
                $(images).each(function () {
                    var $image = $(this);
                    if ($image.is(':visible')) {
                        $image.stop().animate($image.data(zoomType), speed, easing);
                    } else {
                        $image.css($image.data(zoomType), speed);
                    }
                });
            }
                        
            $tabs.height(0).hide(); // have to manually set the initial state to get it animate properly.
            
            // this causes opear to render the images with zero height and width for the hidden image
            // $panels.hide().filter(':first').show();
            var speed = 200;
            
            $zoomtab.hover(function () {
                // show and zoom out
                zoomImages('fullView', speed);
                $tabs.stop().animate({ height : height }, speed, easing);
            }, function () {
                // hide and zoom in
                zoomImages('zoomView', speed);
                $tabs.stop().animate({ height : 0 }, speed, easing, function () {
                  $tabs.hide();
                });
            });
            
            var hoverIntent = null;
            $tabs.find('a').hover(function () {
                clearTimeout(hoverIntent);
                var el = this;
                hoverIntent = setTimeout(function () {
                    $panels.hide().filter(el.hash).show();
                }, 100);
            }, function () {
                clearTimeout(hoverIntent);
            }).click(function () {
                return false;
            });
        });
    };

    $(function () {
        $('.zoomoutmenu').zoomtabs(15);
    });
}

function getFlashMovieObject(movieName){
	if (window.document[movieName]){
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1){
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];
	}
	else{
		return document.getElementById(movieName);
	}
}

function moveFlash(gotoPage){
	getFlashMovieObject("index_godolphin").JSCalled(gotoPage);
}
