$j=jQuery.noConflict();

/* Cookis plugin */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/*Smooth Scrolling*/
(function(a){a.fn.smoothScroll=function(b){b=~~b||400;this.find('a[href*=#]').click(function(e){var c=a.browser.opera?a('html'):a('html,body'),d=a(this.hash);e.preventDefault();if(location.pathname.replace(/^\//,'')===this.pathname.replace(/^\//,'')&&location.hostname===this.hostname){if(d.length){c.stop().animate({scrollTop:d.offset().top},b,function(){location.hash=d.attr('id')})}}});return this}})(jQuery);

/* Display template layout */
function displayFull()
{
    $j('.template').each(function(index){
         $j(this).attr( 'class', 'fulltemplate' );
         var thumb = $j(this).find( '.livepreview' );
         thumb.attr( 'src', thumb.attr('src').replace('-225x128','') ).css({ 'width' : '480px', 'height' : '274px' });
    });
}

/* Display full template layout */
function displayThumbs()
{
    $j('.fulltemplate').each(function(){
       $j(this).attr( 'class', 'template' );
       var full = $j(this).find( '.livepreview' );
       full.attr( 'src', full.attr('src').replace(/\.(\w{3,4})$/,'-225x128.$1') ).css({ 'width' : '225px', 'height' : '128px' });
    });
}

$j(document).ready(function()
{
    /* Layouts switcher */
    $j('#full').live( 'click', function(){
       if( ! $j(this).hasClass('active') )
       {
            displayFull();
            $j.cookie('layout', null, { path : '/', expires : -1 });
            $j.cookie('layout', 'full', { path : '/', expires : 7 });
            $j(this).siblings().removeClass('active');
            $j(this).addClass('active');
            _gaq.push([ '_trackEvent', 'Views', 'Full' ]);
        }
        return false;
    });
    $j('#thumbs').live( 'click', function(){
       if( ! $j(this).hasClass('active') )
       {
            displayThumbs();
            $j.cookie('layout', null, { path : '/', expires : -1 });
            $j(this).siblings().removeClass('active');
            $j(this).addClass('active');
            _gaq.push([ '_trackEvent', 'Views', 'Thumbnails' ]);
        }
        return false;
    });
    
    $j('#newpage').click(function(){
        if( $j('#newpage:checked').val() == 'demowindows' )
        {
            $j('.meta .fl').each(function(){
                $j(this).attr('target','_blank');
            });
            $j.cookie('demow', null, { path : '/', expires : -1 });
            $j.cookie('demow', 'new', { path : '/', expires : 7 });
            _gaq.push([ '_trackEvent', 'Demos', 'New window' ]);
        }
        else
        {
            $j.cookie('demow', null, { path : '/', expires : -1 });
            $j('.meta .fl').each(function(){
                $j(this).removeAttr('target'); 
            });
            _gaq.push([ '_trackEvent', 'Demos', 'Self window' ]);
        }
    });
    
    $j('.url').click(function(){ _gaq.push([ '_trackEvent', 'Commenters', $j(this).attr('href') ]); setTimeout('document.location = "' + $j(this).attr('href') + '"', 100); return false; });
    
    /* Empty search */
    $j('#b').click(function(){ if( $j('#s').val() == 'Search' ) { $j('#s').focus(); return false; } });
    $j('#termsearch').click(function(){ if( $j('#termsection').val() == '' ) { $j('#termsection').focus(); return false; } });

    /* Comment reply */
    $j(".replyto").bind("click", function() {
        var idc = $j(this).closest("li").attr("id");var auc = $j("#"+ idc + " cite").text();var aco = $j("textarea#comment").val();var form = $j("#respond-form");
        $j("textarea#comment").val( aco + "<a href='#" + idc + "' title='In reply to " + auc + "'>@" + auc + "</a>: ").focus(); 
    });
    $j(".commentlist > li").hover( function () { 
        $j("#"+$j(this).attr("id")+" .replyto").css("display", "inline"); }, 
        function () { $j("#"+$j(this).attr("id")+" .replyto").css("display", "none"); });

    /* Analytics */
    $j('.downloadlink').click(function(){ _gaq.push([ '_trackPageview', '/template-download/'+$j(this).attr('id').replace(/([^_]+)_(.*)/,'$2/blogger-template-$1')+'/' ]); });
    $j('#suscription a').click(function(){ _gaq.push([ '_trackEvent', 'Suscription Links', '/'+$j(this).attr('id')+'/' ]); setTimeout('document.location = "' + $j(this).attr('href') + '"', 100); return false; });
    $j('.outbound').click(function(){ _gaq.push(['_trackEvent', 'Outbound Links', '/'+$j(this).attr('id').replace('_','/')+'/' ]); setTimeout('document.location = "' + $j(this).attr('href') + '"', 100); return false; });
    
    /* Sidebar lists */
    $j(".linkcontraido").bind("click", function () { $j(this).next().toggle("fast"); $j(this).toggleClass("linkexpandido"); });

    $j('.post, .fulltemplate, #comments').smoothScroll();
});

