/*------------------------------------------------------------------------------
    JS Document (https://developer.mozilla.org/en/JavaScript)

    project:    Cada
    created:    2011-10-20
    author:     Christophe ANDRIEU (http://www.stpo.fr)

    summary:    CONSTANTES
                UTILITIES
                WINDOW.ONLOAD
                ROLLOVER
                ACCORDION
                TABS
                FORMS
----------------------------------------------------------------------------- */
/*  =CONSTANTES
----------------------------------------------------------------------------- */
jQuery.noConflict();
var d = document;
var w = window;
cada = {};


/*  =UTILITIES
----------------------------------------------------------------------------- */
var log = function(x) {
    if (typeof console != 'undefined') {
        console.log(x);
    }
};


/*  =WINDOW.ONLOAD
----------------------------------------------------------------------------- */
jQuery(document).ready(function(){
    
    // Call Functions
    cada.rollMe();                  // image rollover
    cada.placeholder();             // form placeholder
    cada.collapse();                // accordions
    cada.tabs();                    // tabs
    
    
    if (jQuery.browser.msie && ((jQuery.browser.version == 6) || (jQuery.browser.version == 7))){
        // IE 6-7 FUNCTIONS ONLY
        
    }

});


/*  =ROLLOVER
----------------------------------------------------------------------------- */
cada.rollMe = function() {
    
    jQuery('.rollMe').each(function(){
        
        var myImg = jQuery(this);
        
        // image preload
        var myRolledImg = document.createElement('img');
        myRolledImg.setAttribute("src", myImg[0].src.slice(0,-4) + '_hover' + myImg[0].src.substr(myImg[0].src.length - 4));
        
        // image rollover
        myImg.parent('a, button').hover(function(){
            
            myImg[0].src = myImg[0].src.slice(0,-4) + '_hover' + myImg[0].src.substr(myImg[0].src.length - 4); // tricky IE substr bug
            
        }, function(){
        
            myImg[0].src = myImg[0].src.split('_hover')[0] + myImg[0].src.split('_hover')[1];
            
        });
        
    });
        
}


/*  =ACCORDION
----------------------------------------------------------------------------- */
cada.collapse = function() {
    
    jQuery('.collapse-caller').each(function(){
        
        var that = jQuery(this);
        var parent = that.parent();
        
        that.bind('click', function(){
            
            if (parent.hasClass('on')) parent.removeClass('on');
            else parent.addClass('on');
            
            return false;
            
        });
        
    });
        
}


/*  =TABS
----------------------------------------------------------------------------- */
cada.tabs = function() {
    
    // behave
    jQuery('.tabs a').bind('click', function(){
        
        jQuery('.tabs li').removeClass('on');
        jQuery('.tab').hide();
        
        jQuery(this).parent().addClass('on');
        jQuery(jQuery(this).attr('href')).show();
        
        return false;
        
    });
    
    // init
    jQuery('.tab').hide();
    jQuery('#tab-1').show();
    jQuery('.tabs li:first-child').addClass('on');
        
}


/*  =FORMS
----------------------------------------------------------------------------- */
cada.placeholder = function(){
    
    jQuery('input[type=text], input[type=search], input[type=password], input[type=email], textarea:not([readonly]').each(function(i){
    
        var that = jQuery(this);
        
        // placeHold me!
        if (that.attr('value') == "") {
            that.val(that.attr('title'));
            that.addClass('placeholded');
        }
        
        // only for webkit
        that.bind('mouseup', function(e){
            e.preventDefault();
        });

        that.bind('focus', function(e){
            
            //if (that.val() == that.attr('title')){
                 that.select();
                 that.removeClass('placeholded');
            //}
        });
        
        that.bind('blur', function(e){
            
            if (that.val() != that.attr('title')) that.removeClass('placeholded');
            else that.addClass('placeholded');
            
            // refill if empty
            if (!/[a-zA-Z0-9]/.test(that.val())) {
                that.val(that.attr('title'));
                that.addClass('placeholded');
            }
            
        });
    });
};

