﻿//Function to preload images
function preloadImages() {

}

//Function to get current page
function getCurrentPage() {
    var sPath = window.location.pathname;
    //Checks two levels
    var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
    return sPage;
}

function getCurrentUrl() {
    var sPath = window.location.pathname;
    return sPath;
}

//Function for highlighting menu tabs
function tabHighlight(controllerName) {
    $('#navbar #' + controllerName).attr("src", virtualToAbsolute("Content/Images/Menu/Menu-" + controllerName + "-Selected.png"));
    $('#navbar #' + controllerName).addClass("selected");

    //Highlight tab when enter hover
    $("#navbar .navtab").hover(
      function () {
          var tabId = $(this).attr('id');
          if (!$(this).hasClass('selected')) {
              $(this).attr("src", virtualToAbsolute("Content/Images/Menu/Menu-" + tabId + "-Over.png"));
          }
      },
      function () {
          var tabId = $(this).attr('id');
          if (!$(this).hasClass('selected')) {
              $(this).attr("src", virtualToAbsolute("Content/Images/Menu/Menu-" + tabId + ".png"));
          }
      }
    );
}

function submenuHighlight(actionName) {
    $('.submenu #' + actionName).addClass("selected");
    $('.submenu #' + actionName).attr("href", "#");
}


function showPersonDialog(person) {
    $.getJSON(virtualToAbsolute("AboutAyuda/GetPersonInfo"), { person: person }, function (data) {
        $('#person_info_dialog').html(data.result);
        $('#person_info_dialog').dialog('open');
    });
}

function getDemoVideo(videoId) {
    $.getJSON(virtualToAbsolute("Demos/GetDemoVideo"), { videoId: videoId }, function (data) {
        $('#demo_panel').html(data.result);
        VideoJS.setupAllWhenReady();
        VideoJS.player.play();
    });
}


function setupBackButtonHighlight() {

    $(".back_button").hover(
      function () {
          $('.back_button').attr('src', virtualToAbsolute("Content/Images/BackToListHighlightedSmall.png"));
      },
      function () {
          $('.back_button').attr('src', virtualToAbsolute("Content/Images/BackToListSmall.png"));
      }
    );
}

function showNews(news_id) {
    $("#news_list").hide();
    $("#" + news_id).show();
}

function showNewsList(news_id) {
    $("#" + news_id).hide();
    $("#news_list").show();
}

function showError(id, message) {
    var error_label = $("#" + id + " + .error_label");
    error_label.html(message);
    error_label.show();
    $("#" + id).focus();
}

function hideError(id) {
   $("#" + id + " + .error_label").fadeOut();
}

function signupSymphony() {
    var error = false;
    var company_name = $("#company_name");
    var company_address = $("#company_address");
    var contact_name = $("#contact_name");
    var contact_email = $("#contact_email");
    var contact_phone = $("#contact_phone");
    var contact_fax = $("#contact_fax");
    var account_type_id = $("#account_type").val();
    var account_type = $("#account_type option:selected").html();

    var media_category = $("#media_category");
    var media_cat_outdoor = false;
    var media_cat_place_based = false;
    if ($("#media_category_1").attr("checked")) media_cat_outdoor = true;
    if ($("#media_category_2").attr("checked")) media_cat_place_based = true;

    var digital_inventory = "";
    if ($("#digital_inventory_1").attr("checked")) digital_inventory = "Yes";
    else digital_inventory = "No";

    var total_static_faces = $("#total_static_faces");
    var total_digital_faces = $("#total_digital_faces");

    //Form validation
    if (account_type_id == 1) {
        if (total_digital_faces.val() == '') { showError(total_digital_faces.attr("id"), "Please enter a total # of static faces"); error = true; }
        else hideError(total_digital_faces.attr("id"));

        if (total_static_faces.val() == '') { showError(total_static_faces.attr("id"), "Please enter a total # of digital faces"); error = true; }
        else hideError(total_static_faces.attr("id"));

        if (!media_cat_outdoor && !media_cat_place_based) { showError(media_category.attr("id"), "Please select at least one category"); error = true; }
        else hideError(media_category.attr("id"));
    }

    if (company_name.val() == '') { showError(company_name.attr("id"), "Please enter a company name"); error = true; }
    else hideError(company_name.attr("id"));

    if (contact_email.val() == '') {showError(contact_email.attr("id"), "Please enter an email address"); error = true;}
    else if (!emailValidation(contact_email.val())) {showError(contact_email.attr("id"), "Please enter a valid email address"); error = true;}
    else hideError(contact_email.attr("id"));

    if (contact_name.val() == '') { showError(contact_name.attr("id"), "Please enter a contact name"); error = true; }
    else hideError(contact_name.attr("id"));



    if (!error) {
        //Build the media catgory string
        var media_category = "";
        if (media_cat_outdoor) media_category += $("label[for=media_category_1]").html() + ", ";
        if (media_cat_place_based) media_category += $("label[for=media_category_2]").html() + ", ";
        media_category = media_category.slice(0, -2);

        $('#symphony_signup_dialog').dialog('close');
        $('#symphony_thanks_dialog').dialog('open');

        $.getJSON(virtualToAbsolute("Home/SendSyphonyRegistration"), { 
            company_name: escape(company_name.val()), 
            company_address: escape(company_address.val()), 
            contact_name: escape(contact_name.val()), 
            contact_email: escape(contact_email.val()), 
            contact_phone: escape(contact_phone.val()), 
            contact_fax: escape(contact_fax.val()),
            account_type: escape(account_type),
            media_category: escape(media_category), 
            total_static_faces: escape(total_static_faces.val()),
            total_digital_faces: escape(total_digital_faces.val())
        }, function (data) {
            //Reset the form
            company_name.val(""); company_address.val(""); contact_name.val(""); contact_email.val(""); contact_phone.val(""); contact_fax.val(""); total_static_faces.val(""); total_digital_faces.val("");
        });    
    } 

    
}

//================== Setup Functions ===================//

function setupCustomForm() {
    if ($("select").length > 0) $("select").msDropDown();
}

// Function to setup sliders
function setupHomeSlider() {
    if ($('#home_carousel').length > 0) {
        $('#home_carousel').bxSlider({
            ticker: true,
            tickerSpeed: 2500,
            tickerHover: true,
            displaySlideQty: 4
        });
    }
}

function setupDemoSlider(index) {

    if ($('#demos_carousel').length > 0) {

        if (index == null) index = 1;
        else {
            $("#demo_" + index).addClass("selected");
            $("#demo_" + index).css({ background: "transparent url(" + virtualToAbsolute('Content/Images/SelectionFrame.png') + ") no-repeat 50% 82%" });
            getDemoVideo(index);
        }
        $("#demos_carousel").jcarousel({
            start: index,
            scroll: 1,
            wrap: "circular",
            //easing: "easeOutBack",
            vertical: true
        });

        $("#demos_carousel li").hover(
            function () {
                if (!$(this).hasClass("selected")) $(this).css('background', "transparent url(" + virtualToAbsolute('Content/Images/HighlightFrame.png') + ") no-repeat 50% 82%");
            },
            function () {
                if (!$(this).hasClass("selected")) $(this).css('background', "none");
            }
        );

        $("#demos_carousel li").click(function () {
            $("#demos_carousel li.selected").css({ background: 'none' });
            $("#demos_carousel li.selected").removeClass("selected");
            $(this).addClass("selected");
            $(this).css({ background: "transparent url(" + virtualToAbsolute('Content/Images/SelectionFrame.png') + ") no-repeat 50% 82%" });
        });

    }
}


function setupPicZoom() {
    if ($('.image_zoom').length > 0) {
        $(".image_zoom").fancybox({
            'transitionIn': 'elastic',
            'transitionOut': 'elastic',
            //'easingIn': 'easeOutBack',
            'titleShow': false,
            'margin': 0,
            'padding': 4,
            'hideOnContentClick': true,
            'speedOut': 300
        });
    }
}

function setupReflection() {
    if ($('img.reflect').length > 0) {
        $("img.reflect").reflect({});
    }
}

function setupDialog() {
    if ($('#person_info_dialog').length > 0) {
        $('#person_info_dialog').dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
            open: function () { setupReflection(); },
            show: "slide",
            width: 980,
            modal: true
            //buttons: { "Ok": function () { $(this).dialog("close"); } }
        });
    }

    if ($('#symphony_signup_dialog').length > 0) {
        $('#symphony_signup_dialog').dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
            show: "fade",
            width: 750,
            modal: true
            //buttons: { "Submit": function () { signupSymphony(); }, "Cancel": function () { $(this).dialog("close");} }
        });
    }

    if ($('#symphony_thanks_dialog').length > 0) {
        $('#symphony_thanks_dialog').dialog({
            autoOpen: false,
            draggable: false,
            resizable: false,
            width: 750,
            modal: true
            //buttons: {"Done": function () { $(this).dialog("close"); } }
        });
    }

}


function emailValidation(str) {
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false
    }

    if (str.indexOf(" ") != -1) {
        return false
    }

    return true
}


/*
* JHeartbeat 0.1.1 Beta
* By Jason Levine (http://www.jasons-toolbox.com)
* A heartbeat plugin for the jquery library to help keep sessions alive.
*/

$.jheartbeat = {

    options: {
        delay: 10000
    },

    beatfunction: function () {

    },

    timeoutobj: {
        id: -1
    },

    set: function (options, onbeatfunction) {
        if (this.timeoutobj.id > -1) {
            clearTimeout(this.timeoutobj);
        }
        if (options) {
            $.extend(this.options, options);
        }
        if (onbeatfunction) {
            this.beatfunction = onbeatfunction;
        }

        // Add the HeartBeatDIV to the page
        $("body").append("<div id=\"HeartBeatDIV\" style=\"display: none;\"></div>");
        this.timeoutobj.id = setTimeout("$.jheartbeat.beat();", this.options.delay);
    },

    beat: function () {
        this.timeoutobj.id = setTimeout("$.jheartbeat.beat();", this.options.delay);
        this.beatfunction();
    }
};


/* URL Parser Plugin */
(function (a, b) { function j(a) { var d = a.tagName; if (d !== b) return c[d.toLowerCase()]; return d } function i(a, b) { var c = decodeURI(a), e = f[b || false ? "strict" : "loose"].exec(c), i = { attr: {}, param: {}, seg: {} }, j = 14; while (j--) { i.attr[d[j]] = e[j] || "" } i.param["query"] = {}; i.param["fragment"] = {}; i.attr["query"].replace(g, function (a, b, c) { if (b) { i.param["query"][b] = c } }); i.attr["fragment"].replace(h, function (a, b, c) { if (b) { i.param["fragment"][b] = c } }); i.seg["path"] = i.attr.path.replace(/^\/+|\/+$/g, "").split("/"); i.seg["fragment"] = i.attr.fragment.replace(/^\/+|\/+$/g, "").split("/"); i.attr["base"] = i.attr.host ? i.attr.protocol + "://" + i.attr.host + (i.attr.port ? ":" + i.attr.port : "") : ""; return i } var c = { a: "href", img: "src", form: "action", base: "href", script: "src", iframe: "src", link: "href" }, d = ["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory", "file", "query", "fragment"], e = { anchor: "fragment" }, f = { strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ }, g = /(?:^|&|;)([^&=;]*)=?([^&;]*)/g, h = /(?:^|&|;)([^&=;]*)=?([^&;]*)/g; a.fn.url = function (b) { var c = ""; if (this.length) { c = a(this).attr(j(this[0])) || "" } return a.url(c, b) }; a.url = function (a, c) { if (arguments.length === 1 && a === true) { c = true; a = b } c = c || false; a = a || window.location.toString(); return { data: i(a, c), attr: function (a) { a = e[a] || a; return a !== b ? this.data.attr[a] : this.data.attr }, param: function (a) { return a !== b ? this.data.param.query[a] : this.data.param.query }, fparam: function (a) { return a !== b ? this.data.param.fragment[a] : this.data.param.fragment }, segment: function (a) { if (a === b) { return this.data.seg.path } else { a = a < 0 ? this.data.seg.path.length + a : a - 1; return this.data.seg.path[a] } }, fsegment: function (a) { if (a === b) { return this.data.seg.fragment } else { a = a < 0 ? this.data.seg.fragment.length + a : a - 1; return this.data.seg.fragment[a] } } } } })(jQuery);

