/*
*	Copyright: (CC) 2009, El Mostrador. All rights reserved.
*	Version: 1.2.1
*	Author: Héctor Vergara R.
*	
*	File Description: Common functions.
*/

/* Basic Prototype functions */
Function.prototype.bind = function() { var fn = this; var args = $.makeArray(arguments); var scope = args.shift(); return function() { return fn.apply(scope, arguments); }}
var Class = function(d) { var c = function() { this.initialize.apply(this, arguments) }; for(var i in d) c.prototype[i] = d[i]; return c };

$.inspect = function(obj) {
    var output = '';
    for(var prop in obj) {
        output += (prop + " = " + obj[prop] + "\n");
    }
    alert(output);
}

/* Equal Height 2.0 - http://andreaslagerkvist.com/jquery/equal-height */
jQuery.fn.equalHeight = function () {
    var height        = 0;
    var maxHeight    = 0;

    // Store the tallest element's height
    this.each(function () {
        height	= jQuery(this).outerHeight();
        maxHeight		= (height > maxHeight) ? height : maxHeight;
    });

    // Set element's min-height to tallest element's height
    return this.each(function () {
        var t            = jQuery(this);
        var minHeight    = maxHeight - (t.outerHeight() - t.height());
        var property    = jQuery.browser.msie && jQuery.browser.version < 7 ? 'height' : 'min-height';

        t.css(property, minHeight + 'px');
    });
};

jQuery.fn.tabs = function() {
    var items = this;

    this.each(function() {
      this.hash = this.href.substring(this.href.indexOf('#'));
      
      $(this).click(function() {
        items.not(this.hash).each(function() { $(this.hash).hide(); $(this).parent().removeClass('active'); });
        $(this.hash).show(); 
        $(this).parent().addClass('active');
        return false;
      });

    });

};

var User = {
    info: {},
    logged: false,
    
    check: function() {
    
        var cookie = $.cookie('userinfo');

        if(cookie == undefined) {
            jQuery('p.anonymous').show();
            jQuery('p.logged').hide();
            return;
        }
        
        var parts = cookie.split('|');
        User.info = { realname: parts[0].replace(/\+/g, ' '), avatar: parts[1], url: parts[2] };

        jQuery('p.anonymous').hide();
        jQuery('p.logged').show();
        jQuery('p.logged strong').text( User.info.realname );
        
        User.logged = true;
            
    }

};

User.Location = {
    get: function() { return $.cookie('elmo_location') || 'CIXX0020' },
    set: function(location) { $.cookie('elmo_location', location) }
};





jQuery.fn.absWidth = function() {
    var width = 0;
    $(this).each(function() { width += parseInt($(this).outerWidth()); });
    return width;
}

jQuery.fn.slider = function(buttons) {
    var ref = $(this);

    ref.parent().height(ref.height());
    
    ref.current = 0;
    ref.offset  = parseInt(ref.parent().outerWidth());
    ref.maximum = Math.round(ref.children().absWidth() / ref.offset) - 1;
    
    ref.set = function(index) {
        this.current = index;
        this.animate({'left': this.current * -1 * ref.offset}, 'slow');
        
        $(buttons).removeClass('active');
        $(buttons).eq(index + 1).addClass('active');
    };

    $(buttons).children('.prev a').click(function() {
        var index = (ref.current == 0) ? ref.maximum : ref.current - 1;
        ref.set(index);
        return false;
    });
    
    $(buttons).children('.next a').click(function() {
        var index = (ref.current == ref.maximum) ? 0 : ref.current + 1;
        ref.set(index);
        return false;
    });
    
    $(buttons).children('.page a').click(function() { 
        var index = parseInt($(this).text());
        ref.set(index);
        return false;
    });
    
    ref.set(0);
};




jQuery(document).ready(function($) {

    $('#search-field').focus(function() { this.value = ''; });
    $('.search-trigger').click(function(event) { $('.search-ddown li').show(); event.stopPropagation(); });
    $('#search-form').submit(function() { $('.search-ddown li a[rel=web]').trigger('click'); return false; });    


    $('.search-ddown li a').click(function() {
        
        var rel   = this.getAttribute('rel');
        var value = $('#search-field').val();
        
        if(rel == 'web') {
            document.location.href = '/search/?q=' + encodeURIComponent( value );
        } else {
            document.location.href = '/legal/web/?titulo=' + encodeURIComponent( value ); 
        }

        return false;
    });
   
   
   
    $('#comment-area').keyup(function() {
    
        if(this.value.length > 450) {
            $('.character-counter span').addClass('warning');
        } else {
            $('.character-counter span').removeClass('warning');
        }
      
        if(this.value.length >= 500) {
            this.value = this.value.substring(0, 500);
        }
        
        $('.character-counter span').text( 500 - this.value.length );
        
    });

    if(typeof weather != 'undefined') {
        for(var code in weather.cities) {
            var city = weather.cities[code];
            
            var elem = $('<li><a href="#"></a></li>');
            var link = elem.children('a');
            
            link.text(city).attr('code', code);
            link.click(function() { Widget.Weather.update( this.getAttribute('code') ) });
            
            $('#cities ul').append(elem);
        }
    }

    $('.change-city').click(function(event){
        $('#cities').show();        
        event.stopPropagation();
    });

    $('body').click(function() {
        $('#cities').hide();
        $('.search-ddown li.none').hide();
    });
    
});


var Locale = {
    months:   ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
    weekdays: ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sábado']
};


