var GB_DONE = false;
var GB_HEIGHT = 400;
var GB_WIDTH = 400;
var GB_WRAP=200;
var GB_TOP;
var GB_HIDDEN_SELECTS;

// TODO: implement commented out changes to allow for overlay style purchase popup

function ShowOverlayFrame(url, height, width) {
    /*ShowOverlayFrame_Internal(url, height, width, false);*/
    GB_show("<iframe id='GB_frame' src='" + url + "' frameborder='0'></iframe>", height, width);
}

/*function ShowOverlayFrame_Internal(url, height, width, hideCloseButton) 
{ 
    GB_show("<iframe id='GB_frame' src='"+url+"' frameborder='0'></iframe>", height, width, hideCloseButton);
}

// Shows the page ContentPopup.aspx as an overlay - first checks is it is being shown within an overlay
function ShowContentPopupOverlayFrame(headContentID, bodyContentID, height, width) {
    /*ShowContentPopupOverlayFrame_Internal(headContentID, bodyContentID, height, width, false);
}

function ShowContentPopupOverlayFrameNoClose(headContentID, bodyContentID, height, width) {
    ShowContentPopupOverlayFrame_Internal(headContentID, bodyContentID, height, width, true);
}*/

/*function ShowContentPopupOverlayFrame_Internal(headContentID, bodyContentID, height, width, hideCloseButton) {*/
function ShowContentPopupOverlayFrame(headContentID, bodyContentID, height, width) {
    var url = '/contentpopup.aspx?headingcontentid=' + headContentID +
        '&bodycontentid=' + bodyContentID;
    if (location.href.toLowerCase().indexOf('/contentpopup.aspx') > 0) {
        url += '&showback=true';
        document.location.href = url;
    }
    else {
        /*ShowOverlayFrame_Internal(url, height, width, hideCloseButton);*/
        ShowOverlayFrame(url, height, width);
    }
}    

function ShowShowElementPopupOverlayFrame(showCode, headContentID, type, name, attribute, useParent, height, width)
{
    var url = '/shows/showelementpopup.aspx?sh=' + showCode + '&headingcontentid=' + headContentID + 
        '&type=' + type + '&name=' + name + '&attribute=' + attribute + '&useparent=' + useParent;
    height = height || 500;
    width = width || 640;
    ShowOverlayFrame(url, height, width);
}

function ShowVenueVenueOverlay(venueCode, showID, height, width)
{
    var url = '/Venues/ViewSeatingMap.aspx?v=' + venueCode + '&s=' + showID;
    if (location.href.toLowerCase().indexOf('/venuedetailspopup.aspx') > 0)
    {
        url += '&showback=true';
        document.location.href = url;
    }
    else
    {
        ShowOverlayFrame(url, height, width);
    }
}

/*function GB_show(strHtmlContent, height, width, hideCloseButton)*/
function GB_show(strHtmlContent, height, width)
{
    //window.scrollTo(0,0);
    GB_HEIGHT = height || 400;
    GB_WIDTH = width || 400;
    if (!GB_DONE) {
        /*var strHtml = "<div id='GB_overlay' /><div id='GB_window'><div style='float:left;'><img src='/images/img_gallery_cap.jpg' alt=''/></div>";
        if (hideCloseButton != true) strHtml += "<div style='float:right;padding-top:11px;padding-right:10px;'><img id='imgclose' src='/images/button_close.gif' alt='Close'/></div>";
        strHtml += "</div>";
        
        $(document.body).append(strHtml);

        if (hideCloseButton != true) $("#imgclose").click(GB_hide);*/
        $(document.body).append("<div id='GB_overlay' />" +
            "<div id='GB_window'>" +
            "<div style='float:left;'><img src='/images/img_gallery_cap.jpg' alt=''/></div>" +
            "<div style='float:right;padding-top:11px;padding-right:10px;'>" +
            "<img id='imgclose' src='/images/button_close.gif' alt='Close'/></div>" +
            "</div>");
        $("#imgclose").click(GB_hide);

        $(window).resize(GB_position);
        $(window).scroll(GB_position);
        GB_DONE = true; 
        
       if(!isIE6()) $("#GB_window").css({position:"fixed"});
    }

    if (get_element("wrapBody")) GB_WRAP=getHeight("wrapBody");    
    $("#GB_frame").remove();
    $("#GB_window").append(strHtmlContent);
    $("#GB_overlay").show();

    if ($.browser.msie) 
    {  
        document.getElementById('GB_overlay').style.filter = 
            "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=scale, src='/images/overlay.png')";    
        if ($.browser.version < 7)
        {
            /* Hide the select elements for IE6- */
            GB_HIDDEN_SELECTS = new Array();
            var objSelects = document.getElementsByTagName("SELECT");
            for (var i = 0; i < objSelects.length; i++)
            {
                if (objSelects[i].style.display != 'none')
                {
                    objSelects[i].style.display = 'none';
                    GB_HIDDEN_SELECTS.push(objSelects[i]);
                }
            }
        }       
    }
    else 
    {
        document.getElementById('GB_overlay').style.background = 
            "transparent url(/images/overlay.png) repeat scroll 10%";
    }
 
    GB_position();
  
    if (GB_ANIMATION)
    {
        $("#GB_window").slideDown("slow");
    }
    else
    {  
        $("#GB_window").show();      
    }
}

function GB_hide() 
{
    // hmmm
    _imagesUrl= null;
    _titles=null;
 
    $("#GB_window,#GB_overlay").hide();
	
	// See added because flash won't shutdown in Internet Explorer;
	if ( $('#GB_frame')[0] ) { // See if #GB_frame exists
		$("#GB_frame").remove(); // remove frame. 
	}
	
	if($.browser.msie && $.browser.version < 7) 
    {
        if (GB_HIDDEN_SELECTS != null && GB_HIDDEN_SELECTS.length > 0)
        {
            for (var i = 0; i < GB_HIDDEN_SELECTS.length; i++)
            {
                GB_HIDDEN_SELECTS[i].style.display = '';
            }
            GB_HIDDEN_SELECTS = null;
        }
    } 
}

function GB_move()
{
	$("#GB_window").css({top: "1000px"});
}

function GB_position()
{   
    var de = document.documentElement;
    var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
    var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    var iebody= (document.compatMode && document.compatMode != "BackCompat") ? 
        document.documentElement : document.body;
    var dsocleft=document.all? iebody.scrollLeft : pageXOffset;
    var dsoctop=document.all? iebody.scrollTop : pageYOffset; 
    var height = h < GB_HEIGHT ? h - 32 : GB_HEIGHT;

    if ((dsoctop+h)>getHeight("bodyheight"))
    {
        dsoctop=getHeight("bodyheight")-h;
    }
	
	var top = (h - height)/2;

	if (isIE6())
	{
		top += document.documentElement.scrollTop;
	}

    $("#GB_window").css({width:GB_WIDTH+"px",height:height+"px",  left: ((w - GB_WIDTH)/2)+"px", top: top+"px" });
    $("#GB_frame").css({width:GB_WIDTH+"px",height:(height - 64)+"px"});
   
    var bodyX= $('#bodyheight').offset().left;
    var bodyY=$('#bodyheight').offset().top;
    var bodyH=getHeight("bodyheight");

    var bodyW=document.getElementById("bodyheight").scrollWidth;
 
     if(navigator.appName == "Microsoft Internet Explorer") 
     {  
        if(document.documentElement.clientHeight>bodyH)
        {
            bodyH=document.documentElement.clientHeight;
        }
        $("#GB_overlay").css({height:(bodyH)+"px", top:bodyY + "px", width:bodyW+"px",left:bodyX+"px"});
     }
     else if(navigator.appName == "Netscape")
     {       
        bodyW=document.getElementById("bodyheight").clientWidth;
		
        if(window.innerHeight>bodyH)
        {
            bodyH=window.innerHeight;
            $("#GB_overlay").css({height:bodyH+"px", top:bodyY + "px", width:"100%",left:dsocleft+"px"});
        }
        else
        {
            $("#GB_overlay").css({height:bodyH+"px", top:bodyY + "px", width:bodyW+"px",left:dsocleft+"px"});
        }    
     }
     else
     {
        bodyH=document.getElementById("bodyheight").clientHeight+17;
        bodyW=document.getElementById("bodyheight").clientWidth;
       
        if(dsoctop < 0)
        { 
            bodyH=document.getElementById("bodyheight").clientHeight - dsoctop;
            $("#GB_overlay").css({height:bodyH+"px", top:dsoctop + "px", width:"100%",left:dsocleft+"px"});
        }
        else
        {
            $("#GB_overlay").css({height:bodyH+"px", top:dsoctop + "px", width:bodyW+"px",left:dsocleft+"px"});
        }
    }
}

(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

var GB_ANIMATION = true;
var _titles=new Array();
var _imagesUrl=new Array();
var _msg = "";

function lightBoxFromFlash(imageName,images,titles)
{  	
	// imageName should be in the format sh=CODE&photo=number
    ShowOverlayFrame('/shows/PhotoGallery.aspx?' + imageName, 500, 640);
}

function lightBoxGallery(showCode, photoNumber)
{
	ShowOverlayFrame('/shows/PhotoGallery.aspx?sh=' + showCode + '&photo=' + photoNumber, 500, 640);
}

/*
    Called by hero flash control to display image gallery
*/
function lightBoxWithMsg(val)
{  	
	_msg = val;	
    GB_show("screenshots_large.aspx?image=",450,640);
	return;
}

get_element = document.all ?
function (s_id) { return document.all[s_id] } :
function (s_id) { return document.getElementById(s_id) };

function getHeight(name) 
{    
	var tmpElem = get_element(name);
	if ($.browser.msie) return tmpElem.offsetHeight;       
	return parseInt(document.defaultView.getComputedStyle(tmpElem, "").getPropertyValue("height"));   
}

function getWidth(name) 
{    
	var tmpElem = get_element(name);
	if ($.browser.msie) return tmpElem.offsetWidth;       
	return parseInt(document.defaultView.getComputedStyle(tmpElem, "").getPropertyValue("height"));   
}

function isIE6()
{
	return (navigator.appName == "Microsoft Internet Explorer" && navigator.appVersion.indexOf("MSIE 6.0") > -1);
}