/*
 * Website Custom Scripts File
 *
 * Copyright (c) 2011 MagicBrain
 * http://www.magicbrain.biz
 *
 * Licensed under MIT
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Launch  : March 2011
 * Version : 3.0.1-rc1
 * Released: Tuesday 15th March, 2011 - 16:51
 */



/** JQUERY EXTENSION TO GRAB URL VARIABLES
--------------------------------------------------------------------------------------------------------------------------------------------*/
$.extend({
  getUrlVars: function(){
    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;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});



/** INPUTS CLEAR DEFAULT VALUE
--------------------------------------------------------------------------------------------------------------------------------------------*/
(function($){
    $.fn.clearDefault = function(){
        return this.each(function(){
            var default_value = $(this).val();
            $(this).focus(function(){
                if ($(this).val() == default_value)
                              $(this).val("");
            });
            $(this).blur(function(){
                if ($(this).val() == "")
                              $(this).val(default_value);
            });
        });
    };
})(jQuery);
// usuage example: $('input').clearDefault();




/** INPUTS NUMERIC ONLY (control handler)
--------------------------------------------------------------------------------------------------------------------------------------------*/
// Numeric only control handler
jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};



/** HANDLER FOR CLASS "OpenNewWindow"
--------------------------------------------------------------------------------------------------------------------------------------------*/
 $(function() {
    $('a.OpenNewWindow').click(function(){
        window.open($(this).attr('href'));
        return false;
    });
 });



;(function ($) {

    /**
     *  TOGGLE CONTENT [faqs]
     */

    jQuery.fn.MBToggle = function(options) {

        return this.each(function() {
            var opts = jQuery.extend({
                'null' : 0
            }, options);

            /* List variables */
            var toggle = $(this);

            /* Click on Toggle Heading */
            $('.toggle-title', this).click(function () {
                if ($(this).is('.active-toggle')) {
                    $(this).removeClass('active-toggle');
                    $('.toggle-content', toggle).slideUp(400);
                } else {
                    $(this).addClass('active-toggle');
                    $('.toggle-content', toggle).slideDown(400);
                }
                return false;
            });

        })
    }

})(jQuery);



/** CATALOGUE :: Fams and Prods Menu
 * ---------------------------------------------------------------------------------------------------------------------------------------- */
function initMenu() {
    $('#catMenu ul').hide();
    $('#catMenu ul:first').show();
    $('#catMenu li a').click(
    function() {
        var checkElement = $(this).next();
        if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            return false;
        }
        if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#catMenu ul:visible').slideUp('normal');
            checkElement.slideDown('normal');
            return false;
        }
    }
    );
}



   

/** ONLOAD :: Initialize Scripts
 * ---------------------------------------------------------------------------------------------------------------------------------------- */
jQuery(document).ready(function () {

    // initialize modal (fancybox)
    if ($("a.zoom").size()>=1)
    {
        $("a.zoom").fancybox({
            'padding'        : 1,
            'overlayOpacity' : 0.7,
            'zoomSpeedIn'    : 500,
            'zoomSpeedOut'   : 500
        });
    }

    $("#bookmarkme").click(function() {
        if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(location.href,document.title,"");
        } else if(window.external) { // IE Favorite
            window.external.AddFavorite(location.href,document.title); }
            else if(window.opera && window.print) { // Opera Hotlist
            this.title=document.title;
            return true;
        }
    });

    // check for sub menu and apply css styles for featured animations
    if ($("ul#mod_menu > li > ul").size() == 1)
    {
        var target = $("ul#mod_menu > li > ul");
        var total = target.find("li").size();
        var newHeight = total * 40;
        target.css({"height":newHeight});
    }

    // main menu effect :: first level
    $("ul#mod_menu > li").hover(
        function() { $(this).children("ul:first").slideDown(250); },
        function() { $(this).children("ul:first").slideUp(250); }
    );


    $("div.prodItem h2 a").click(function() {
        $(this).closest("h2").next("div").slideToggle();
        return false;
    });


    /* Catalogue Menu */
    initMenu();

    /* MBToggle */
    jQuery('.toggle').MBToggle();

});

