/* Smooth scrolling
Changes links that link to other parts of this page to scroll
smoothly to those links rather than jump to them directly, which
can be a little disorienting.

sil, http://www.kryogenix.org/

v1.0 2003-11-11
v1.1 2005-06-16 wrap it up in an object
*/

var ss = {
    fixAllLinks: function() {
        // Get a list of all links in the page
        var allLinks = document.getElementsByTagName('a');
        // Walk through the list
        for (var i=0;i<allLinks.length;i++) {
            var lnk = allLinks[i];
            if ((lnk.href && lnk.href.indexOf('#') != -1) && 
            ( (lnk.pathname == location.pathname) ||
            ('/'+lnk.pathname == location.pathname) ) && 
            (lnk.search == location.search)) {
                // If the link is internal to the page (begins in #)
                // then attach the smoothScroll function as an onclick
                // event handler
                ss.addEvent(lnk,'click',ss.smoothScroll);
            }
        }
    },

    smoothScroll: function(e) {
        // This is an event handler; get the clicked on element,
        // in a cross-browser fashion
        if (window.event) {
            target = window.event.srcElement;
        } else if (e) {
            target = e.target;
        } else return;

        // Make sure that the target is an element, not a text node
        // within an element
        if (target.nodeName.toLowerCase() != 'a') {
            target = target.parentNode;
        }

        // Paranoia; check this is an A tag
        if (target.nodeName.toLowerCase() != 'a') return;

        // Find the <a name> tag corresponding to this href
        // First strip off the hash (first character)
        anchor = target.hash.substr(1);
        // Now loop all A tags until we find one with that name
        var allLinks = document.getElementsByTagName('a');
        var destinationLink = null;
        for (var i=0;i<allLinks.length;i++) {
            var lnk = allLinks[i];
            if (lnk.name && (lnk.name == anchor)) {
                destinationLink = lnk;
                break;
            }
        }
        if (!destinationLink) destinationLink = document.getElementById(anchor);

        // If we didn't find a destination, give up and let the browser do
        // its thing
        if (!destinationLink) return true;

        // Find the destination's position
        var destx = destinationLink.offsetLeft; 
        var desty = destinationLink.offsetTop;
        var thisNode = destinationLink;
        while (thisNode.offsetParent && 
        (thisNode.offsetParent != document.body)) {
            thisNode = thisNode.offsetParent;
            destx += thisNode.offsetLeft;
            desty += thisNode.offsetTop;
        }

        // Stop any current scrolling
        clearInterval(ss.INTERVAL);

        cypos = ss.getCurrentYPos();

        ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
        ss.INTERVAL =
        setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);

        // And stop the actual click happening
        if (window.event) {
            window.event.cancelBubble = true;
            window.event.returnValue = false;
        }
        if (e && e.preventDefault && e.stopPropagation) {
            e.preventDefault();
            e.stopPropagation();
        }
    },

    scrollWindow: function(scramount,dest,anchor) {
        wascypos = ss.getCurrentYPos();
        isAbove = (wascypos < dest);
        window.scrollTo(0,wascypos + scramount);
        iscypos = ss.getCurrentYPos();
        isAboveNow = (iscypos < dest);
        if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
            // if we've just scrolled past the destination, or
            // we haven't moved from the last scroll (i.e., we're at the
            // bottom of the page) then scroll exactly to the link
            window.scrollTo(0,dest);
            // cancel the repeating timer
            clearInterval(ss.INTERVAL);
            // and jump to the link directly so the URL's right
            location.hash = anchor;
        }
    },

    getCurrentYPos: function() {
        if (document.body && document.body.scrollTop)
            return document.body.scrollTop;
        if (document.documentElement && document.documentElement.scrollTop)
            return document.documentElement.scrollTop;
        if (window.pageYOffset)
            return window.pageYOffset;
        return 0;
    },

    addEvent: function(elm, evType, fn, useCapture) {
        // addEvent and removeEvent
        // cross-browser event handling for IE5+,  NS6 and Mozilla
        // By Scott Andrew
        if (elm.addEventListener){
            elm.addEventListener(evType, fn, useCapture);
            return true;
        } else if (elm.attachEvent){
            var r = elm.attachEvent("on"+evType, fn);
            return r;
        } else {
            alert("Handler could not be removed");
        }
    } 
}

ss.STEPS = 25;

ss.addEvent(window,"load",ss.fixAllLinks);

var today = new Date();
function takeYear(theDate) {
    x = theDate.getYear();
    var y = x % 100;
    y += (y < 38) ? 2000 : 1900;
    return y;
}


function hide(id) {
    if (document.getElementById){
        obj = document.getElementById(id);
        if (obj.style.display == ""){
            obj.style.display = "none";
        }
    }
}
function show(id) {
    if (document.getElementById){
        obj = document.getElementById(id);
        if (obj.style.display == "none"){
            obj.style.display = "";
            obj.focus();
        }
    }
} 

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
        {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

function changecss(theClass,element,value) 
{
    //Last Updated on May 21, 2008
    //documentation for this script at
    //http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
    var cssRules;
    if (document.all) {
        cssRules = 'rules';
    }
    else if (document.getElementById) {
        cssRules = 'cssRules';
    }
    var added = false;
    for (var S = 0; S < document.styleSheets.length; S++)
        {
        for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) 
            {
            if (document.styleSheets[S][cssRules][R].selectorText == theClass)
                {
                if(document.styleSheets[S][cssRules][R].style[element])
                    {
                    document.styleSheets[S][cssRules][R].style[element] = value;
                    added=true;
                    break;
                }
            }
        }

        if(!added){
            if(document.styleSheets[S].insertRule)
                {
                document.styleSheets[S].insertRule(theClass+' { '+element+': '+value+'}',document.styleSheets[S][cssRules].length);
            } else if (document.styleSheets[S].addRule) 
                {
                document.styleSheets[S].addRule(theClass,element+': '+value+';'); 
            } 
        }
    } 
}


var viewportwidth;
var viewportheight;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight 
if (typeof window.innerWidth != 'undefined')
    {
    viewportwidth = window.innerWidth,
    viewportheight = window.innerHeight
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
    {
    viewportwidth = document.documentElement.clientWidth,
    viewportheight = document.documentElement.clientHeight
}
// older versions of IE 
else
    {
    viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
    viewportheight = document.getElementsByTagName('body')[0].clientHeight
}



/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!                    
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
    // Load active video inside popup, but don't show the whole popup yet.
    $active_video = $('#popupContact div.welcomeVideo').show();
    $active_video.find('object, embed').css('visibility', 'hidden').show();

    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").slideDown("slow", function() {
            // Finally show the video.
            $active_video.find('object, embed').css('visibility', 'visible');
        });
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $('#popupContact div.welcomeVideo:visible').find('object, embed').css('visibility', 'hidden');
        $("#backgroundPopup").fadeOut();
        $("#popupContact").slideUp("slow");
        popupStatus = 0;
    }

    // Clear Pop-Up Content
    $('#popupContact').html('');
}


//centering popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6

    $("#backgroundPopup").css({
        "height": windowHeight
    });    
}
//CONTROLLING EVENTS IN jQuery
$(function(){
    //LOADING POPUP
    //Click the button event!
    $(".videoLink").click(function(e){
        // Get Pop-Up Content
        // Insert Content
        window.location = "#topper";
        //ss.smoothScroll(e);
        $('#popupContact').load('promoVideo.htm?date=' + new Date().getTime(), function(){
            // centering with css
            centerPopup();
            // load popup
            loadPopup();
        });
        return false;
    });

    //CLOSING POPUP
    //Click the x event!
    $("#popupContactClose").live('click', function(){
        disablePopup();
    });
    //Click out event!
    $("#backgroundPopup").live('click', function(){
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
        }
    });

});

//Watermark Input Plugin
(function($){var map=new Array();$.Watermark={ShowAll:function(){for(var i=0;i<map.length;i++){if(map[i].obj.val()==""){map[i].obj.val(map[i].text);map[i].obj.css("color",map[i].WatermarkColor)}else{map[i].obj.css("color",map[i].DefaultColor)}}},HideAll:function(){for(var i=0;i<map.length;i++){if(map[i].obj.val()==map[i].text)map[i].obj.val("")}}}
$.fn.Watermark=function(text,color){if(!color)color="#aaa";return this.each(function(){var input=$(this);var defaultColor=input.css("color");map[map.length]={text:text,obj:input,DefaultColor:defaultColor,WatermarkColor:color};function clearMessage(){if(input.val()==text)input.val("");input.css("color",defaultColor)}function insertMessage(){if(input.val().length==0||input.val()==text){input.val(text);input.css("color",color)}else input.css("color",defaultColor)}input.focus(clearMessage);input.blur(insertMessage);input.change(insertMessage);insertMessage()})}})(jQuery);

$(function(){
    $(document).ready(function() {
        $("#seoBox").fadeOut(0);
        $('a[rel=lightbox[gallery]]').lightBox();
        try {
            oHandler = $("#languageCombo").msDropDown().data("dd2");
        } catch(e) {
            //alert("Error: "+e.message);
        }
        var viewportWidth = window.innerWidth ? window.innerWidth : $(window).width();
        if(viewportWidth < 1222)
            $("#callNow").hide();
        if(viewportWidth < 1030)
            $("#socialMedia").hide();
        else{
            $("#socialMedia a").hover(function() {
                var e = this;
                $(e).stop().animate({ marginLeft: "17px" }, 250, function() {
                    $(e).animate({ marginLeft: "14px" }, 250);
                });
            },function(){
                var e = this;
                $(e).stop().animate({ marginLeft: "-4px" }, 250, function() {
                    $(e).animate({ marginLeft: "1px" }, 250);
                });
            });
        }
        $("#searchbox").Watermark("Search Site...");
        $("#searchbox").focus(function(){
            $(this).animate({width: "+=40px"}, 100);
        });
        $("#searchbox").blur(function(){
            $(this).animate({width: "-=40px"}, 100);
        });
    });
});
function redirectSearch()
{
    location.href = "search&q=" + $("#searchbox").val() + ".html";
}
