﻿// General JS functions

// Browser flags
var bIsMozilla = false;
var bIsIE = false;
var bIsSafari = false;          // Google chrome etc
var bDOCUMENT_READY = false;    // indicator we're good to go
jQuery(document).ready(
    function()
    {
        try {
            // Browser sniff
            jQuery.each(jQuery.browser, 
                function(i, val) {
                  if(i=="mozilla" && val==true) bIsMozilla = true;
                  if(i=="msie" && val==true) bIsIE = true;
                  if(i=="safari" && val==true) bIsSafari = true;
                }
            );
            
            // Top nav vertical centering
            jQuery('.menuTop').each(
                function() 
                {
                    try {
                        var contH = jQuery(this).height();
                        var linkH = jQuery('a', this).height();
                        var top = parseInt((contH/2)-(linkH/2), 10);
                        var nH = contH-top;
                        jQuery(this).css("padding-top", top).css("height", nH);
                    }
                    catch (e) {}
                }
            );
            /*jQuery('.menuTop').each(
                function(i) {
                    jQuery(this).css('cursor', 'pointer');
                    // pass click down to anchor
                    jQuery(this).click(
                        function(event) {
                            event.stopPropagation();
                            alert(jQuery(this+' > a').
                            jQuery(this+' > a').click();
                        }
                    );
                }
            );*/
            
            // Hijack click event to propagate down to the anchors element for main menu
            jQuery(".menuTop").css('cursor', 'pointer').click(
                function (event) {
                    JQ_ProcessClick(this, event)
                }
            );
            
            // Parallax
            try {
                jQuery('#parallax').show().jparallax({yparallax:true, 
                    frameDuration:50, 
                    mouseport: jQuery('#MainContainer')});
                // Tracker overlay for click processing...
                jQuery('#parallax2').jparallax({yparallax:false, 
                    frameDuration:50, 
                    mouseport: jQuery('#MainContainer')});
            }
            catch (e) {}
            
            // Content seperator
            //jQuery('.ContentSeperator').append('<img src="/images/content_seperator.png" alt="" />');

            // initialise shadowbox
            Shadowbox.init({
                skipSetup: true,
                animSequence: 'sync',
                autoplayMovies: false,
                slideshowDelay: 0,
                resizeDuration: 0.5,
                fadeDuration: 1.4,
                continuous: true,
                handleOversize: 'resize',
                overlayColor: '#000000',
                overlayOpacity: 0.70,
                positionAtTop: false,
                flashBgColor: '#ffffff',
                counterType: 'skip',
                displayNav: true,
                viewportPadding: 0,
                players: ['html','iframe']
            });        
            
            // ACCORDION SETUP
            jQuery('#accordion,.accordion').accordion({ 
                autoheight: false, 
                alwaysOpen: false,
                header: ".header"
            }).activate(-1);        // default all closed

            // WATERMARKS            
            jQuery(':text,:password,textarea').each(function() {
                if (jQuery(this).attr("watermark")) {
                    jQuery(this).watermark({watermarkText: jQuery(this).attr("watermark"), watermarkCssClass: 'tbWaterMark'});//, 
                    //    width: jQuery(this).width+'px', height: jQuery(this).height+'px'});
                }
            });

        }
        catch (err) {}

        // We're go...
        bDOCUMENT_READY = true;
    }
);

// Process our menu click event...
function JQ_ProcessClick(elem, event) {
    try {
        // Get the anchor...
        var anchor = jQuery("a", elem);
        // Get link...
        var link = anchor.attr("href");
        // and target...
        var target = anchor.attr("target").toLowerCase();
        // determine what to do...
        if (target == "_blank") {
            event.preventDefault();     // Stop it executing twice
            window.open(link);
        } else if (link.toLowerCase().substring(0,4) == "java") {
            event.preventDefault();     // Stop it executing twice
            eval(link);
        } else {
            window.location.href = link;
        }
    }
    catch(e) {}
}

// =====================================================
// SEARCH BOX FUNCTIONS
// =====================================================
function EnterSearchBox() {
	var val = document.forms[0].txtSearch;
	if (val) {
		if (val.value.toLowerCase() == SEARCHTEXT.toLowerCase()) {val.value = "";}
	}
}
function LeaveSearchBox() {
	var val = document.forms[0].txtSearch;
	if (val) {
		if (val.value == "") {val.value = SEARCHTEXT;}
	}
}
// Do search...
function DoSearch() {
    var form = document.forms[0];
	var val = form.txtSearch;
	if (val) {
		document.location.href = SEARCHURL+"?n=1&st="+(encodeURI(val.value))+"&pg=1";
	}
}
// Catch enter key for search submission
function CatchEnter(event) {
	if ((window.event && window.event.keyCode == 13) || 
		(event && event.which == 13)) {
		DoSearch();
	} else {
		return true;
	}
	return false;
}
// =====================================================

// Ensure we post back to correct url and not underlying template...
function SubmitForm() {
    var form = document.forms[0];
    form.submitted.value = "1";
    form.action = document.location.href;
    form.submit();
}

// Open a new window
function OpenPopup(url, w, h) {
    var op = window.open(url, "popup", "menubar=0,resizable=0,width="+w+",height="+h+",scrollbars=1,location=0");
    op.focus();
}

function readCookie(nam) {
    var tC = document.cookie.split('; ');
    for (var i = tC.length - 1; i >= 0; i--) {
        var x = tC[i].split('=');
        if (nam == x[0]) return unescape(x[1]);
    }
    return null;
}
function writeCookie(nam,val) {
    document.cookie = nam + '=' + escape(val);
} 

function ShowPreview(file, w, h) {
    Shadowbox.open  ({
        content: "<img src='"+file+"' />",
        player: "html",
        width: w,
        height: h
    });
}

function ShowSWF(file, w, h, flashVars, flashParams) {
    var content = GetFlashCodeWithVars(file, w, h, false, flashVars, flashParams);
    Shadowbox.open  ({
        content: content,
        player: "html",
        width: w,
        height: h
    });
}

function ShowURL(url, w, h) {
    if (!bDOCUMENT_READY) return;
    Shadowbox.open  ({
        content: url,
        player: "iframe",
        width: w,
        height: h
    });
}

/* Show Quick Tour video */
function ShowQT(venueid) {
    if (!bDOCUMENT_READY) return;
    var flashVars = {
    };
    var flashParams = {
	    bgcolor:"#3a2789",
	    allowFullScreen:"true",
	    wmode:"window",
	    "scale":"showAll"
    };
	
	var content = "";
	var player = "html";
	if (venueid == "chicago" || venueid == "dallasfw")	
        content = GetFlashCodeWithVars('/swf/quicktour/mainplayer.swf?flvname=/swf/quicktour/ldcmainwip_us.flv', 640, 405, false, flashVars, flashParams);
    else if (venueid == "manchester") 
    {
        content = "http://www.youtube.com/embed/lf24HO5JXX8";
        player = "iframe";
    }
    else
        content = GetFlashCodeWithVars('/swf/quicktour/mainplayer.swf?flvname=/swf/quicktour/ldcmainwip.flv', 640, 405, false, flashVars, flashParams);

    Shadowbox.open  ({
        content: content,
        player: player,
        width: 640,
        height: 405
    });
}

/* Show interactive map */
function ShowIM(langid, code) 
{
    if (!bDOCUMENT_READY) return;

    var sIntro = "";
    // has user viewed intro already?
    var ck = readCookie('IM_Intro');
    if (ck == null || ck == "true") sIntro = "true";
    else sIntro = "false";
    // Now mark as read
    writeCookie('IM_Intro', 'false');

    var flashVars = {
	    config:"config.txt",
	    preloaderfonts:"preloader/PreloaderFonts.swf",
	    rootpath:"/swf/map",
	    deeplink:code,
	    locale:langid,
	    showintro:sIntro
    };
    var flashParams = {
	    bgcolor:"#FFFFFF",
	    allowFullScreen:"true",
	    wmode:"window",
	    "scale":"showAll"
    };
		
    var content = GetFlashCodeWithVars('/swf/map/preloader.swf', 820, 640, false, flashVars, flashParams);

    Shadowbox.open  ({
        options:    { 
            positionAtTop:  true
        }, 
        content: content,
        player: "html",
        width: 820,
        height: 640
    });
}

function GoVenue() {
    var s = document.getElementById("selVenue");
    if (s!=null) {
        if (s.value != "") {
            document.location.href = s.value;
        } else {
            s.selectedIndex = 0;
        }
    }
}

/* IE6 MENU FIX */
sfHover = function() {
     if (document.getElementById("Hdr_Menu") != null) {
         var sfEls = document.getElementById("Hdr_Menu").getElementsByTagName("LI");     
         for (var i=0; i<sfEls.length; i++) {         
            sfEls[i].onmouseover=function() {             
                this.className+=" sfhover";         
            }         
            sfEls[i].onmouseout=function() {             
                this.className=this.className.replace(new RegExp(" sfhover\\b"), "");         
            }     
         } 
     }
} 
if (window.attachEvent) window.attachEvent("onload", sfHover); 

// Select an item in <select>ion list or radio group by its value
function SelectByValue(src, val) {
    var type = (src.type) ? src.type : (src.length>0) ? src[0].type : "";
    if (type=="select-one") {
        for (var x=0;x<src.length;x++) {
            if (src[x].value == val) {
                src[x].selected = true;
                break;
            }
        }
    } else if (type=="radio") {
        for (var x=0;x<src.length;x++) {
            if (src[x].value == val) {
                src[x].checked = true;
                break;
            }
        }
    }
}

// Allow preview popup to link to admin console
function GoAdminURL(url) {
    // Allows preview window to update admin window
    if (window.opener && !window.opener.closed) {
        try {
            window.opener.document.location.href = url;
            window.opener.focus();
            window.close();
        }
        catch (e) {window.close();}
    } else {
        document.location.href = url;        
    }
}

// urlencode a string
function urlencode (str) {
    str = escape(str);
    return str.replace(/[*+\/@]|%20/g,
        function (s) {
            switch (s) {
                case "*": s = "%2A"; break;
                case "+": s = "%2B"; break;
                case "/": s = "%2F"; break;
                case "@": s = "%40"; break;
                case "%20": s = "+"; break;
            }
            return s;
        }
    );
}
