/*
 * 
 * Direct Partners - JavaScript
 *
 */

/*
 * DP namespace
 */
var DP = {};

/*
* DP Site Framework constructor
*/
DP.Framework = function Framework() {
    DP.mainNav = new DP.MainNav();
    DP.subNav = new DP.SubNav();
    DP.pageInit();
}

/*
 * Common functions
 */
 
//function to extend a class and create a subClass
DP.extend = function(subClass, baseClass) {
    function inheritance() { };
    inheritance.prototype = baseClass.prototype;

    subClass.prototype = new inheritance();
    subClass.prototype.constructor = subClass;
    subClass.baseConstructor = baseClass;
    subClass.superClass = baseClass.prototype;
}

DP.isCurrentPage = function(name) {
    if (name.toLowerCase() == $("body").attr("id").toLowerCase() || name.toLowerCase() == $("body").attr("class").toLowerCase()) {
        return true;
    }
    return false;
}

/*
* main-nav
*/

DP.MainNav = function MainNav() {
    this.init();
};

DP.MainNav.prototype.init = function() {
    //initialization of main navigation
}

DP.MainNav.prototype.addHoverState = function(id) {
    $("#nav").find("#" + id + " a").addClass("hover");
}

/*
 * Sub-nav
 */

DP.SubNav = function SubNav() {
    // Sub-nav activation switch - indicates whether user has hovered over sub-nav
    this.flag = false;
    this.init();
};

DP.SubNav.prototype.init = function() {
    this.clearStyles();
    this.hover();
}

// Deactivates sub-nav
DP.SubNav.prototype.clearStyles = function () {
    $("#sub-nav-div").css('display', 'none');
    $("#sub-nav-wrap").css('display', 'none');
    $("#sub-nav_about-us").css('width', '53px');
    $("#sub-nav_about-us li").css('display', 'none');
}

DP.SubNav.prototype.activateSubNav = function () {
    $("#sub-nav-wrap > *").stop(true, false);
    $("#sub-nav-div").css('display', 'block');      
    $("#sub-nav_about-us").css('width', '207px');
    $("#sub-nav_about-us > *").css('display', 'block');
}

DP.SubNav.prototype.openSubNav = function () {
// resets sub-nav animations, prevents bugs in quicker reactivations
if (!this.flag) {
        if ($("#sub-nav-div").is(":visible") || $("#sub-nav_about-us").is(":animated")) { $("#sub-nav-wrap > *").stop(true, false); $("#sub-nav_about-us > *").css('display', 'none'); }
    }
    $("#sub-nav-div").fadeIn(100, function() {
        if (!jQuery.support.opacity) {
            $("#sub-nav-wrap").css('display', 'block');
        }
        else {
            $("#sub-nav-wrap").fadeIn(100);
        }
        $("#sub-nav_about-us").animate({ width: "207px" }, 300, function() {
            $("#sub-nav_about-us > *").fadeIn(100);
        });
    });
}

DP.SubNav.prototype.closeSubNav = function() {
    if (!this.flag) {
        var that = this;
        $("#sub-nav_about-us > *").fadeOut(800, function() {
            $("#sub-nav_about-us").animate({ width: "35px" }, 700, function() {                
                if (!jQuery.support.opacity) {
                    setTimeout(function() { that.clearStyles() }, 50);
                }
                else {
                    $("#sub-nav-wrap").fadeOut(200);
                }                														
            });
        });
    }
    $("*").stop(true, true);
}

DP.SubNav.prototype.addHoverState = function(id) {
    $("#sub-nav-wrap").find("#" + id + " a").addClass("hover");
}

DP.SubNav.prototype.hover = function() {
    if (!DP.isCurrentPage("About Us") && !DP.isCurrentPage("EmployeeDetails") && !DP.isCurrentPage("our-house")) { //TODO: remove this check a refactor to separate fn
        var that = this;
        // Main navigation hover functions	
        $("#about a").hover(
	        function() {
	            that.openSubNav();
	        },
	        function() {
	            setTimeout(function() { that.closeSubNav() }, 400);
	        }
        );
        // Sub-Nav hover functions
        $("#sub-nav-wrap > *").hover(
	        function() {
	            that.flag = true;
	            that.activateSubNav();
	            // removed to prevent flicker -Tyson and Jason 12/10/09
	            //openSubNav();
	        },
	        function() {
	            that.flag = false;
	            that.closeSubNav();
	        }
        );
    }
}

DP.SubNav.prototype.webkit = function() {
    if (/AppleWebKit[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
        DP.subNav.openSubNav();
        DP.subNav.closeSubNav();
        DP.subNav.clearStyles();
    }
}

/*
 * Initialize proper page(s)
 */
DP.pageInit = function() {
    new DP.Global();
    
    if (DP.isCurrentPage("our-people")) {
        new DP.OurPeople();
    }
    else if (DP.isCurrentPage("services")) {
        new DP.Services();
    }
    else if (DP.isCurrentPage("clients")) {
        new DP.Clients();
    }
    else if (DP.isCurrentPage("contact")) {
        new DP.Contact();
    }
    else if (DP.isCurrentPage("our-work")) {
        new DP.OurWork();
    }
    else if (DP.isCurrentPage("our-house")) {
        new DP.OurHouse();
    }
    else if (DP.isCurrentPage("About Us")) {
        new DP.AboutUs();
    }
    else if (DP.isCurrentPage("EmployeeDetails")) {
        new DP.EmployeeDetails();
    }
    else if (DP.isCurrentPage("gallery")) {
        new DP.Gallery();
    }
}

/*
* Global page code
*/
DP.Global = function() {
    this.init();
}
DP.Global.prototype.init = function() {
    DP.subNav.webkit();
}

/*
* Our People page
*/
DP.AboutUs = function AboutUs() {
    this.init();
}
DP.AboutUs.prototype.init = function () {
    this.nav();
}
DP.AboutUs.prototype.nav = function () {
    DP.subNav.openSubNav();
    DP.mainNav.addHoverState("about");
}

/*
 * Our People page
 */
DP.OurPeople = function OurPeople() {
    this.init();
}
DP.OurPeople.prototype.init = function () {
    this.nav();
}
DP.OurPeople.prototype.nav = function () {
    DP.subNav.openSubNav();
    DP.mainNav.addHoverState("about");
    DP.subNav.addHoverState("people");
}

/*
 * Our People Employee Details page
 */
DP.EmployeeDetails = function EmployeeDetails() {
    this.init();
}

DP.EmployeeDetails.prototype.init = function() {
    this.animateGraph();
    this.nav();
}

DP.EmployeeDetails.prototype.animateGraph = function() {
    setTimeout(
        function () {
            $("#news-mag .bar").animate({ height: mediaGraphNewsMagHeight + "px" }, 500);
            $("#tv-radio .bar").animate({ height: mediaGraphTvRadioHeight + "px" }, 500);
            $("#internet .bar").animate({ height: mediaGraphInternetHeight + "px" }, 500);
        }, 800);
}

DP.EmployeeDetails.prototype.nav = function() {
    DP.subNav.openSubNav();
    DP.mainNav.addHoverState("about");
    DP.subNav.addHoverState("people");
}

/*
 * Services Page
 */
DP.Services = function Services() {
    this.init();
}

DP.Services.prototype.init = function () {
    this.nav();
    this.tooltip();
}

DP.Services.prototype.tooltip = function() {
    var idName = "";
    $(".services-sub-sections").each(
    function() {
        // Added 'over' class to tooltip when
        // to prevent it from hiding when the user mouses back over the same trigger
        // -Jason Monma 01/06/2010
        $(this).hover(
            function() {
                idName = $(this).attr("class").replace("services-sub-sections ", "");
                $("#" + idName).addClass("over");
            },
            function() {
                idName = $(this).attr("class").replace("services-sub-sections ", "");
                $("#" + idName).removeClass("over");
            }
        );
        
        idName = $(this).attr("class").replace("services-sub-sections ","");
        $(this).tooltip({
            tip: "#" + idName,
            opacity: 1,
            delay: 100,
            position: "bottom right",
            //offset: [-226, -290],
            offset: [-252, -340 + (idName.length * 12)],
            relative: 'true',
            effect: 'slide',           
            direction: 'top'
        });
    });
}

DP.Services.prototype.nav = function () {
    DP.mainNav.addHoverState("services");
}

/*
 * Clients Page
 */
DP.Clients = function Clients() {
    this.init();
}

DP.Clients.prototype.init = function() {
    this.nav();
    this.tooltip();
}

DP.Clients.prototype.tooltip = function() {
    var idName = "";
    $(".client > a, .client > div").each(
        function() {
            // Added 'over' class to tooltip when
            // to prevent it from hiding when the user mouses back over the same trigger
            // -Jason Monma 01/06/2010
            $(this).hover(
                function() {
                    $("#" + $(this).attr("class")).addClass("over");
                },
                function() {
                    $("#" + $(this).attr("class")).removeClass("over");
                }
            );
            
            idName = "#" + $(this).attr("class");
            $(this).tooltip({
                tip: idName,
                opacity: 1,
                delay: 100,
                position: "bottom right",
                offset: [-100, -50],
                relative: 'true',
                effect: 'slide',
                direction: 'up'
            });
        });
}

DP.Clients.prototype.nav = function () {
    DP.mainNav.addHoverState("clients");
}

/*
 * Contact Page
 */
DP.Contact = function Clients() {
this.init();

}

DP.Contact.prototype.init = function() {
    this.nav();
    this.ContactSubmitHover();
}

DP.Contact.prototype.nav = function() {
    DP.mainNav.addHoverState("contact");
}

DP.Contact.prototype.ContactSubmitHover = function() {
    $(".submit").mouseover(function() {
         $(this).addClass("submit_hover");
     })
      .mouseout(function() {
             $(this).removeClass("submit_hover");
     });

}

/*
 * Our Work Page
 */
DP.OurWork = function Clients() {
    this.init();
}

DP.OurWork.prototype.init = function() {
    this.nav();
}

DP.OurWork.prototype.nav = function() {
    DP.mainNav.addHoverState("work");
}

/*
 * Our Home Page
 */
DP.OurHouse = function Clients() {
    this.init();
}

DP.OurHouse.prototype.init = function() {
    this.nav();
}

DP.OurHouse.prototype.nav = function() {
    DP.subNav.openSubNav();
    DP.mainNav.addHoverState("about");
    DP.subNav.addHoverState("house");
}

/*
 * Gallery Page
 */
DP.Gallery = function Gallery() {
    this.init();
}

DP.Gallery.prototype.init = function() {
    new DP.Forms();
}

/*
 * Custom Forms
 */

DP.Forms = function Forms() {
    this.checkboxHeight = "25";
    this.radioHeight = "25";
    this.selectWidth = "208";
    this.init();
}

DP.Forms.prototype.init = function() {
    $("head").append('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + this.selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; }</style>');
    var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
    for (a = 0; a < inputs.length; a++) {
        if ((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
            span[a] = document.createElement("span");
            span[a].className = inputs[a].type;

            if (inputs[a].checked == true) {
                if (inputs[a].type == "checkbox") {
                    position = "0 -" + (this.checkboxHeight * 2) + "px";
                    span[a].style.backgroundPosition = position;
                } else {
                    position = "0 -" + (this.radioHeight * 2) + "px";
                    span[a].style.backgroundPosition = position;
                }
            }
            inputs[a].parentNode.insertBefore(span[a], inputs[a]);
            inputs[a].onchange = this.clear;
            span[a].onmousedown = this.pushed;
            span[a].onmouseup = this.check;
            document.onmouseup = this.clear;
        }
    }
    inputs = document.getElementsByTagName("select");
    for (a = 0; a < inputs.length; a++) {
        if (inputs[a].className == "styled") {
            option = inputs[a].getElementsByTagName("option");
            active = option[0].childNodes[0].nodeValue;
            textnode = document.createTextNode(active);
            for (b = 0; b < option.length; b++) {
                if (option[b].selected == true) {
                    textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
                }
            }
            span[a] = document.createElement("span");
            span[a].className = "select";
            span[a].id = "select" + inputs[a].name;
            span[a].appendChild(textnode);
            inputs[a].parentNode.insertBefore(span[a], inputs[a]);
            /*inputs[a].onchange = this.choose;*/
        }
    }
}

DP.Forms.prototype.pushed = function () {
    element = this.nextSibling;
    if (element.checked == true && element.type == "checkbox") {
        this.style.backgroundPosition = "0 -" + this.checkboxHeight * 3 + "px";
    } else if (element.checked == true && element.type == "radio") {
        this.style.backgroundPosition = "0 -" + this.radioHeight * 3 + "px";
    } else if (element.checked != true && element.type == "checkbox") {
        this.style.backgroundPosition = "0 -" + this.checkboxHeight + "px";
    } else {
        this.style.backgroundPosition = "0 -" + this.radioHeight + "px";
    }
}

DP.Forms.prototype.check = function() {
    element = this.nextSibling;
    if (element.checked == true && element.type == "checkbox") {
        this.style.backgroundPosition = "0 0";
        element.checked = false;
    } else {
        if (element.type == "checkbox") {
            this.style.backgroundPosition = "0 -" + this.checkboxHeight * 2 + "px";
        } else {
            this.style.backgroundPosition = "0 -" + this.radioHeight * 2 + "px";
            group = this.nextSibling.name;
            inputs = document.getElementsByTagName("input");
            for (a = 0; a < inputs.length; a++) {
                if (inputs[a].name == group && inputs[a] != this.nextSibling) {
                    inputs[a].previousSibling.style.backgroundPosition = "0 0";
                }
            }
        }
        element.checked = true;
    }
}

DP.Forms.prototype.clear = function() {
    inputs = document.getElementsByTagName("input");
    for (var b = 0; b < inputs.length; b++) {
        if (inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
            inputs[b].previousSibling.style.backgroundPosition = "0 -" + this.checkboxHeight * 2 + "px";
        } else if (inputs[b].type == "checkbox" && inputs[b].className == "styled") {
            inputs[b].previousSibling.style.backgroundPosition = "0 0";
        } else if (inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
            inputs[b].previousSibling.style.backgroundPosition = "0 -" + this.radioHeight * 2 + "px";
        } else if (inputs[b].type == "radio" && inputs[b].className == "styled") {
            inputs[b].previousSibling.style.backgroundPosition = "0 0";
        }
    }
}

DP.Forms.prototype.choose = function() {
    option = this.getElementsByTagName("option");
    for (d = 0; d < option.length; d++) {
        if (option[d].selected == true) {
            document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
        }
    }
}




/*
* On document ready
*/
$(document).ready(function() {

    DP.framework = new DP.Framework();

});