﻿try { //Prevent backround flicker in IE
    document.execCommand('BackgroundImageCache', false, true);
} catch (e) { }


var foxshare = {};
foxshare.ui = {};

foxshare.ui.handleReadyEvent = function() {

    // Check if there are any "jquery_load_config" classed elements.
    jQuery.each(jQuery('input.jquery_load_config'),
        function(hrindex, hrobject) {
            jQuery('#' + hrobject.name).load(hrobject.value);
        }
    );
}

// Check if the location and DOM has data that indicates
// that some field-values should be populated automatically.
foxshare.ui.checkAutoFields = function() {
    if (document.location.toString().indexOf("rendermode=popup") > 0) {
        if (top.frames.document.title && jQuery('#modal_opener')) {
            jQuery('#modal_opener').val(top.frames.document.title);
        }
        else if (document.referrer && jQuery('#modal_opener')) {
            jQuery('#modal_opener').val(document.referrer);
        }
        // Check for a parent frame that has a known mail field and a form field in this page
        if (jQuery('#message_mail') && parent && parent.document && parent.document.getElementById('mail_address')) {
            jQuery('#message_mail').val(parent.document.getElementById('mail_address').value);
        }
    }

}


foxshare.tracking = {
    trackEvent: (function(category, action, label) {
        // Do a Urchin page track click if the obj is available.
        try {
            _gaq.push(['_trackEvent', category, action + " " + label]);
        } catch (err) {
            try {
                var pageTracker = _gat._getTracker("UA-5800122-1");
                try {
                    pageTracker._trackEvent(category, action + " " + label);
                }
                catch (evtErr) {
                    pageTracker._trackPageview("/splash/" + category + "/" + action + "/" + label);
                }
            }
            catch (errsub) {
            }
            // document.getElementById("ga_tracker_error_message").innerHTML += err.message;
        }
    }),
    trackVideoClick: (function(videoTitle) {
        var trackPageUrl = document.location.toString();

        if (document.location.pathname) {
            trackPageUrl = document.location.pathname.toString();
        }
        if (trackPageUrl.length < 2) {
            trackPageUrl = "/Default.aspx";
        }
        trackPageUrl += "/" + videoTitle;
        // Try log the click
        foxshare.tracking.trackEvent("Video", "Play", trackPageUrl);
    }),
    trackMoreInfoClick: (function() {
        var trackLabel = document.title;
        if (trackLabel === null || trackLabel === "") {
            trackLabel = document.location;
        }
        foxshare.tracking.trackEvent("More info", "Popup", trackLabel);
    })
};



foxshare.ui.displayVideo = function(sender, baseHref, videoSrc, width, height) {
    // sender.innerHTML = baseHref + videoSrc + ", " + width + '*' + height;
    // Display a shadowbox with the video...

    // Check for the internal analytics lib
    if (window["foxshare"] && window["foxshare"]["tracking"] && window["foxshare"]["tracking"]["trackVideoClick"]) {
        window["foxshare"]["tracking"]["trackVideoClick"](videoSrc);
    }
    var media = (videoSrc.toString().indexOf(".flv") > 0) ? "flv" : "swf";

    Shadowbox.init({ skipInit: true, showMovieControls: false, flashvars: { autoplay: true, autoplayMovies: true} });
    Shadowbox.open({
    type: media,
        content: baseHref + videoSrc,
        width: width,
        height: height,
        autoplay: "true"
    });
}


foxshare.splash = {
    btnclick: (function(buttonid) {

        if (buttonid === "contact") {
            // shadowbox;player=iframe;width=;height=" href="
            Shadowbox.open({
                type: "iframe",
                content: "/om-oss/kontakta-oss.aspx?rendermode=popup",
                width: 900,
                height: 630
            });
        }
        else if (buttonid === "opportunities") {
        top.location.href = "/lms/";
        }
        else {
            alert(buttonid);
        }
    }),
    showClip: (function(clipId) {
        switch (clipId) {
            case "foxshare_trailer":
                foxshare.splash.showFoxshareVideo();
                break;
            case "projektpsykologi_trailer":
                foxshare.splash.showProjektPsykologiVideo();
                break;
            default:
                // Handle undefined case?
                alert(clipId);
                break;
        }
    }),
    showFoxshareVideo: (function() {
        foxshare.ui.displayVideo(null, 'http://s.foxshare.se/Media/', 'foxshare_trailer.flv', 854, 480);
        // Try track the event
    }),
    showProjektPsykologiVideo: (function() {
        foxshare.ui.displayVideo(null, 'http://s.foxshare.se/Media/', 'trailer_prjps_854x480-FOXSHARE.flv', 854, 480);
    })
};



// Bind flash call to the lib:
var showclip = foxshare.splash.showClip;



/******************************************************************************************/
// UI Methods
foxshare.ui.toggleTabContainer = function(sender, targetId) {


    jQuery("#" + sender.parentNode.id + " > a").removeClass("current");
    sender.className = "tab_link current";

    var targetNode = document.getElementById(targetId);
    if (targetNode) {
        var targetParentNode = targetNode.parentNode;
        if (targetParentNode) {
            jQuery("#" + targetNode.parentNode.id + " > div").removeClass("tab_content_current");
        }
        targetNode.className = "tab_content tab_content_current";
    }
    return false;
}
