﻿var addthis_pub = "4catsink";

$(document).ready(function() {
    //initFooter();
    setExternalLinks();
    //initAddThisButton();  
});

function initFooter() {

    // Inserts standard content into every page with a footer-inner element (div)

    if (document.getElementById('footer-inner')) {
        document.getElementById('footer-inner').innerHTML = '<div id="addnow"><a><img src="" alt="Addthis widget" /></a></div>4 Cats, Ink | 7485 W Saratoga Pl Denver, CO 80123 | 303.371.1186 | <a href="mailto:contact@4catsink">contact@4catsink.com</a>'
    };
};

function initAddThisButton() {

    $('#addnow img').attr('src', 'http://s7.addthis.com/static/btn/lg-share-en.gif');
    $('#addnow a').attr('href', 'http://www.addthis.com/bookmark.php');
    $('#addnow a').attr('title', 'This link opens the addnow widget.');
    $('#addnow a').click(function() { return addthis_sendto(); });
    $('#addnow a').mouseover(function() { return addthis_open(this, '', '[URL]', '[TITLE]') });
    $('#addnow a').mouseout(function() { addthis_close() });

};

function setExternalLinks() {
    //jquery version

    $('a.nw').click(function() { window.open(this.href); return false; });
    $('a.nw').attr('title', 'This link will open in a new window.');

    //      Javascript version
    //    var links, i;
    //    links = document.getElementsByTagName('a');
    //    for (i = 0; i < links.length; i++) {
    //        if (links[i].className == "nw") {
    //            links[i].onclick = function() {
    //                window.open(this.href); return false;
    //            };
    //        };
    //    };
};

///// Start of Menu Plugin

// ******************************************************************
// Javascript XML-menu parser.
// Reads an XML file, parses it, and converts it to <ul> styled menu.
// Required files: js-menu.js (this file); menu.css; and menuData.xml
//
// Copyright 2009 Eric L. Mott and 4 Cats, Ink: www.4CatsInk.com
// Free for use by including this copyright.
//
//  Usage example:
//  <head>
//      <title>XML Menu Sample</title>
//      <link href="menu.css" rel="stylesheet" type="text/css" />
//      <script src="js-menu.js" type="text/javascript"></script>
//  </head>
//  <body>
//    <div class="menu" id="menu"></div>
//    <div id="sitemap"></div>
//    <script type="text/javascript">initMenu("menuData.xml", "Home", 1);</script>
//  </body>
//
//  Email comments and suggestions for improvement by emailing Eric 
//      eric@4catsink.com
//
//  That is all.
//
//*******************************************************************


// Create global variables
var str = "";
var currentpage = "";
var pagedepth = "";

//function initMenu(file, myPage, depth) {
// Function to initiate the menu; three parameters:
// file --> server path to the data file
// myPage --> label text of the page calling the script
// depth --> how many levels deep the page is
// Checks for the existance of either a "menu" or "sitemap"  ID attritube (div)

//    var xmlDoc = loadXMLDoc(file);
//    var root = xmlDoc.documentElement;
//    currentpage = myPage;
//    pagedepth = depth;
//    var xml = buildMenuString(root);
//    if (document.getElementById("menu")) {
//        document.getElementById("menu").innerHTML = xml;
//    };
//    if (document.getElementById("sitemap")) {
//        document.getElementById("sitemap").innerHTML = xml;
//    };

//};
function loadXMLDoc(dname) {
    // function to load the XML data file.
    // Uses either XMLHttpRequest or ActiveX Microsoft.XMLHTTP depending on browser support.

    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    }
    else {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    };
    xhttp.open("GET", dname, false);
    xhttp.send("");
    return xhttp.responseXML;
};
function buildMenuString(tree) {
    //function to recursively build the HTML menu
    //looks for <ul>, <li>, <a> nodeNames
    // recursion takes place before the closing HTML tags (</ul>,</li>,</a>)
    //see menuData.xml for example file
    //builds a string by concatenation, which is returned to the initMenu function

    if (tree.hasChildNodes()) {
        switch (tree.nodeName) {
            case "Menu":
                //if the node is a list, open the list tag
                str += '<ul>';
                //before closing the list tag, check to see if it has any children and recurse this function    
                var nodes = tree.childNodes.length;
                for (var i = 0; i < tree.childNodes.length; i++) {
                    buildMenuString(tree.childNodes[i])
                };
                str += '</ul>'
                break;
            case "menuItem":
                var submenuclass = false;
                var atts = tree.attributes;
                for (var a = 0; a < atts.length; a++) {
                    var att = atts.item(a);
                    if (att.nodeName == "class") {
                        if (att.nodeValue == "sub") {
                            submenuclass = true;
                        };
                    };
                };
                if (submenuclass) {
                    str += '<li class="sub">';
                }
                else {
                    str += '<li>';
                };
                nodes = tree.childNodes.length;
                for (i = 0; i < tree.childNodes.length; i++) {
                    buildMenuString(tree.childNodes[i]) //Recursion
                };
                str += '</li>'
                break;
            case "itemLink":
                str += '<a';
                atts = tree.attributes;
                for (a = 0; a < atts.length; a++) {
                    att = atts.item(a);
                    if (att.nodeName == "class") {
                        if (att.nodeValue == "nw") {
                            str += ' class="nw" ';
                        };
                    };
                    if (att.nodeName == "href") {
                        //need to pad the link with a '../' for each level deep it is in the website
                        if (att.nodeValue.substring(0, 5) != "http:") {
                            //do nothing for external links
                            switch (pagedepth) {
                                case 2:
                                    str += ' href="../' + att.nodeValue;
                                    break;
                                case 3:
                                    str += ' href="../../' + att.nodeValue;
                                    break;
                                case 4:
                                    str += ' href="../../../' + att.nodeValue;
                                    break;
                                case 5:
                                    str += ' href="../../../../' + att.nodeValue;
                                    break;
                                default:
                                    str += ' href="' + att.nodeValue;
                            };

                        } else { str += ' href="' + att.nodeValue; };


                    };
                };
                var thispage = tree.childNodes[0].nodeValue
                if (currentpage == thispage) {
                    str += '" id="current">' + thispage + '</a>'
                }
                else {
                    str += '">' + thispage + '</a>'
                };
                break;
            default:
                str += '<' + tree.nodeName + '>';
        };
    };
    return str;
};


///// End of Menu Plugin


//// Start of Tab Control Plugin

// jquery.tabcontrols.js
// Eric L. Mott  www.4CatsInk.com

$(document).ready(function() {

    $("[id^='tabcontent']").attr('class', 'hide');
    $("[id^='toggle']").attr('class', 'off');
    initTabEvents();   
});

function initTabbedContent(maxTab) {
    var myTab = queryString('tab');

    if (myTab == "false") {
        ShowTab('1');
    }
    else if (myTab > maxTab) {
        ShowTab('1');
    }
    else {
        ShowTab(myTab);
    };
};

function initTabEvents() {
    $('#left_tab_controls div, #right_tab_controls div').click(function() {
        $("[id^='tabcontent']").attr('class', 'hide');
        $('#left_tab_controls div,  #right_tab_controls div').attr('class', 'off');
        $(this).attr('class', 'on');
        var max = $(this).attr('id').replace('toggle', 'tabcontent');
        $("#" + max).attr('class', 'show');
    });
};


function ShowTab(tabID) {
    var toggle;
    var content;

    toggle = "toggle" + tabID;
    content = "tabcontent" + tabID;

    $("#" + toggle).attr('class', 'on');
    $("#" + content).attr('class', 'show');

};

//// end of tab control plugin


//// start of query string plugin

function queryString(key) {
    var page = new PageQuery(window.location.search);
    return unescape(page.getValue(key));
};

//  Parsing The Querystring with Javascript
//  Source: Peter A. Bromberg, Ph.D
//  http://www.eggheadcafe.com/articles/20020107.asp

function PageQuery(q) {


    if (q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();
    if (q) {
        for (var i = 0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }
    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
        for (var j = 0; j < this.keyValuePairs.length; j++) {
            if (this.keyValuePairs[j].split("=")[0] == s)
                return this.keyValuePairs[j].split("=")[1];
        }
        return false;
    }
    this.getParameters = function() {
        var a = new Array(this.getLength());
        for (var j = 0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
        return a;
    }
    this.getLength = function() { return this.keyValuePairs.length; }
};


//// end of query string plugin

//// start of innerfade plugin

/* =========================================================

// jquery.innerfade.js

// Datum: 2008-02-14
// Firma: Medienfreunde Hofmann & Baldes GbR
// Author: Torsten Baldes
// Mail: t.baldes@medienfreunde.com
// Web: http://medienfreunde.com

// based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/
// and Ralf S. Engelschall http://trainofthoughts.org/

*
*  <ul id="news"> 
*      <li>content 1</li>
*      <li>content 2</li>
*      <li>content 3</li>
*  </ul>
*  
*  $('#news').innerfade({ 
*	  animationtype: Type of animation 'fade' or 'slide' (Default: 'fade'), 
*	  speed: Fading-/Sliding-Speed in milliseconds or keywords (slow, normal or fast) (Default: 'normal'), 
*	  timeout: Time between the fades in milliseconds (Default: '2000'), 
*	  type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'), 
* 		containerheight: Height of the containing element in any css-height-value (Default: 'auto'),
*	  runningclass: CSS-Class which the container get’s applied (Default: 'innerfade'),
*	  children: optional children selector (Default: null)
*  }); 
*

// ========================================================= */


(function($) {

    $.fn.innerfade = function(options) {
        return this.each(function() {
            $.innerfade(this, options);
        });
    };

    $.innerfade = function(container, options) {
        var settings = {
            'animationtype': 'fade',
            'speed': 'normal',
            'type': 'sequence',
            'timeout': 2000,
            'containerheight': 'auto',
            'runningclass': 'innerfade',
            'children': null
        };
        var elements;
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            elements = $(container).children();
        else
            elements = $(container).children(settings.children);
        if (elements.length > 1) {
            $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length - i)).css('position', 'absolute').hide();
            };
            if (settings.type == "sequence") {
                setTimeout(function() {
                    $.innerfade.next(elements, settings, 1, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings.type == "random") {
                var last = Math.floor(Math.random() * (elements.length));
                setTimeout(function() {
                    do {
                        current = Math.floor(Math.random() * (elements.length));
                    } while (last == current);
                    $.innerfade.next(elements, settings, current, last);
                }, settings.timeout);
                $(elements[last]).show();
            } else if (settings.type == 'random_start') {
                settings.type = 'sequence';
                var current = Math.floor(Math.random() * (elements.length));
                setTimeout(function() {
                    $.innerfade.next(elements, settings, (current + 1) % elements.length, current);
                }, settings.timeout);
                $(elements[current]).show();
            } else {
                alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
            }
        }
    };

    $.innerfade.next = function(elements, settings, current, last) {
        if (settings.animationtype == 'slide') {
            $(elements[last]).slideUp(settings.speed);
            $(elements[current]).slideDown(settings.speed);
        } else if (settings.animationtype == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.speed, function() {
                removeFilter($(this)[0]);
            });
        } else
            alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
        if (settings.type == "sequence") {
            if ((current + 1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings.type == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
        setTimeout((function() {
            $.innerfade.next(elements, settings, current, last);
        }), settings.timeout);
    };

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
    if (element.style.removeAttribute) {
        element.style.removeAttribute('filter');
    }
}


//// end of innerfade plugin
//function setContentHeight() {
//  ----not in use------
//    var myHeight;  
//    myHeight = screen.availHeight;
//    if (myHeight > 0 || myHeight != null) {
//        if (document.getElementById("content")) {
//            var myContent = document.getElementById("content");
//            myContent.style.minHeight = myHeight * 0.56 + "px"; 
//        };

//    };
//};


//window.onload = function(e) {
//};