/**
 * custom.js
 *
 * Author: JohnGR
 *
 * Custom javascript code
 *
 */

// IE gives an error if we run the setOrient() function and then javascript isn't working,
// so we better not let that happen
if (navigator.appName != "Microsoft Internet Explorer")
{
    // On load scroll the page down a pixel (this hides mobile safari's url bar)
    window.addEventListener('load', function(){
        setTimeout(scrollTo, 0, 0, 1);
    }, false);

    // Run the setOrient() function when the page loads and when the device orientation changes
    window.addEventListener('load', setOrient, false);
    window.addEventListener('orientationchange', setOrient, false);
}

/*
 * setOrient()
 *
 * Checks for the device orientation and adds the class 'up' (for portrait)
 * or 'land' (for landscape) to the body element
 *
 */
function setOrient() {
    var orient = Math.abs(window.orientation) === 90 ? 'land' : 'up';
    var cl = document.body.className;
    cl = cl.replace(/up|land/, orient);
    document.body.className = cl;
}

/*
 * function tabs(tab)
 *
 * Switch tabs
 *
 * Adds the class active to the new clicked link and only shows the selected tab
 *
 */
function tabs(tab)
{
    $('#tabs li a').removeClass("active");
    tab.addClass("active");

	$('.istab').hide();
	$('#' + tab.attr("rel")).show();
}

/* Character limit (jquery plugin) */
(function($){
    $.fn.extend({
        limit:function(limit,element){
            var interval,f;
            var self=$(this);
            $(this).focus(function(){
                interval=window.setInterval(substring,100)
                });
            $(this).blur(function(){
                clearInterval(interval);
                substring()
                });
            substringFunction="function substring(){ var val = $(self).val();var length = val.length;if(length > limit){$(self).val($(self).val().substring(0,limit));}";
            if(typeof element!='undefined')substringFunction+="if($(element).html() != limit-length){$(element).html((limit-length<=0)?'0':limit-length);}";
            substringFunction+="}";
            eval(substringFunction);
            substring()
            }
        })
})(jQuery);
/* END Character limit (jquery plugin) */

/* pop! (jquery plugin) */
(function($) {

    $.pop = function(options){

        // settings
        var settings = {
            pop_class : '.pop',
            pop_toggle_text : ''
        }

        // inject html wrapper
        function initpops (){
            $(settings.pop_class).each(function() {
                var pop_classes = $(this).attr("class");
                $(this).addClass("pop_menu");
                $(this).wrap("<div class='"+pop_classes+"'></div>");
                $(".pop_menu").attr("class", "pop_menu");
                $(this).before(" \
          <div class='pop_toggle'>"+settings.pop_toggle_text+"</div> \
          ");
            });
        }
        initpops();

        // assign reverse z-indexes to each pop
        var totalpops = $(settings.pop_class).size() + 1000;
        $(settings.pop_class).each(function(i) {
            var popzindex = totalpops - i;
            $(this).css({
                zIndex: popzindex
            });
        });
        // close pops if user clicks outside of pop
        activePop = null;
        function closeInactivePop() {
            $(settings.pop_class).each(function (i) {
                if ($(this).hasClass('active') && i!=activePop) {
                    $(this).removeClass('active');
                }
            });
            return false;
        }
        $(settings.pop_class).mouseover(function() {
            activePop = $(settings.pop_class).index(this);
        });
        $(settings.pop_class).mouseout(function() {
            activePop = null;
        });

        $(document.body).click(function(){
            closeInactivePop();
        });
        // toggle that pop
        $(".pop_toggle").click(function(){
            $(this).parent(settings.pop_class).toggleClass("active");
        });
    }

})(jQuery);
/* END pop! (jquery plugin) */


