﻿var ucc = {};
//Page elements
ucc.el = {};
//Page name
ucc.page = '';

//Kill console.log if it doesn't exist
if (window.console === undefined) {
    window.console = {
        log: function() { }
    };
}

ucc.placeholding = function() {
    $(this).attr('placeholder', this.title);
    this.placeholder = this.placeholder || this.title || '';
    $(this).val('');

    if (!Modernizr.input.placeholder) {

        if ($(this).val() == "" && this.placeholder != "") {
            $(this).val(this.placeholder);
            $(this).focus(function() {
                if ($(this).val() == this.placeholder) $(this).val("");
            });
            $(this).blur(function() {
                if ($(this).val() == "") $(this).val(this.placeholder);
            });
        }
    }
}

ucc.balanceBottomBoxes = function() {
    var quicklinks = $('#quicklinks'),
        news = $('#news');

    console.log([quicklinks.height(), news.height()]);

    if (quicklinks.height() > news.height()) {

        news.height(quicklinks.height() + 10)

    }
    else if (news.height() > quicklinks.height()) {
        quicklinks.height(news.height()-10)
    }

}

ucc.quicklinking = function(ql) {

    //    var list, links = ql.find('li');

    //    //If less than 4, no need to split it.
    //    if (links.length > 4) {
    //        

    //        list = ql.find('ul');

    //        links.each(function(count) {

    //            list.append(this);
    //            if ((count + 1) % 4 == 0) {
    //                ql.append(list);
    //                list = $('<ul></ul>');
    //            }
    //            
    //        })
    //        ql.append(list);
    //    }


    var lists = ql.find('ul'), links = ql.find('li');
    links.remove();
    console.log();

    links.each(function(count) {

        $(lists.get(count % 4)).append(this);
        
    });

}

ucc.tabs = function(subnav) {
    $('.pane').hide();
    $('.pane:first').show();
    subnav.find('a:first').addClass('active');

    subnav.find('a').click(function(e) {
        e.preventDefault();

        subnav.find('a.active').removeClass('active');
        $(this).addClass('active');

        var pane = $('#' + this.href.split('#')[1]);

        $('.pane:visible').hide();
        pane.show();
    })
}

ucc.faqs = function(faqs) {
    console.log(faqs)

    if (faqs.length > 0) {

        faqs.find('.question').click(function() {

            if ($(this).next().is(':visible')) {
                $(this).find('span.plus-minus').html('[+]');
                $(this).next().hide('slow');
            }
            else {
                $(this).find('span.plus-minus').html('[-]');
                faqs.find('div.answer:visible').hide('slow')
                .prev().find('span.plus-minus').html('[+]');
                $(this).next().show('slow');
            }




        }).next('.answer').hide();


    }
}

ucc.search = function(e) {
    if (e.keyCode == 13) {
        window.location = "/Search.aspx?q=" + ucc.el.search.val();
    }
}

ucc.flashXML = function() {
    
    //console.log('Calling flashXML() function with:');
    //console.log(flashXML);
    
    return flashXML;
}

ucc.showLocation = function(location) {
//    console.log('Args:')
//    console.log(arguments);
//    console.log('Showing: ' + location);

//    ucc.el.content.find('.company-clear').each(function(i) { console.log('comany-clear ' + this.innerHTML) });
//    ucc.el.content.find('.region').each(function(i) { console.log('region ' + this.innerHTML) });
//    ucc.el.content.find('.' + location).each(function(i) { console.log('matched location ' + this.innerHTML) });

    ucc.el.content.find('.company-clear').remove();
    ucc.el.content.find('.region').hide();
    ucc.el.content.find('.' + location).show();

    ucc.el.content.find('.company:visible:odd').after('<div class="company-clear"></div>')


}

ucc.sidebarNavigation = function(navList) {
    var rootItem = navList.find('li.egSelected:first')
    console.log(rootItem);

    rootItem
    .find('li').each(function() {

        if ($(this).find('ul').length > 0) {
            console.log(this);
            if ($(this).hasClass('egSelected')) {
                $(this).addClass('open');
            }
            else {
                $(this).addClass('closed');
            }

            $(this).find('a:first').click(ucc.toggleNavOpen);
        }
    });

}

ucc.toggleNavOpen = function(event) {
    event.preventDefault();
    event.stopPropagation();

    var listItem = $(this).parent();

    if (listItem.hasClass('open')) {
        listItem.removeClass('open').addClass('closed');
    }
    else {
        listItem.removeClass('closed').addClass('open');
    }

    listItem.siblings('.open')
    .removeClass('open').addClass('closed')
    .find('.open')
    .removeClass('open').addClass('closed');
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode: function(string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode: function(string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode: function(string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode: function(utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while (i < utftext.length) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
