$(function() {

    //$.ajaxSetup({
        //timeout: 10000,
        //async:false
    //});

    var urllocation = $(location).attr('href'); 
    
    // redirect user if have anchor in url
	if(urllocation.indexOf("#") != -1 && urllocation.substr(-1) != '#'){
       var anchor_name = urllocation.substr(parseInt(urllocation.indexOf("#"))+1);
        $('#nav li').removeClass('active');
        $('#nav li.' + anchor_name).addClass('active');
        $('div.page, div.sidebar').hide();
        $('div.page-' + anchor_name).fadeIn();
        $('.sub_menu_' + anchor_name).show();
    } else {
        $('div.page:not(:first)').hide();
        $('div.sidebar:not(:first)').hide();
    }

    // bind menu navegation
    $('#nav a').unbind().click(function() {
        $('#col1, #col3').show();
        var selected = $('#nav li').index($('#nav li.active'));
        var index = $('#nav a').index(this);

        $(this).parents().children('li').removeClass('active');
        $(this).parent('li').addClass('active');

        var index_clicked = index;
        var index_opened = $('#col1_content > div:visible').index();
        
        if(parseInt(index_clicked) != parseInt(index_opened)) {
            if(parseInt(index_clicked) > parseInt(index_opened)) {
               display_direction = 'right';
            } else {
                display_direction = 'left';
            }
            
            $('#col1_content > div:visible').hide();
            $('#col3_content > div:visible').hide();
            $('#col1_content > div:eq('+ index +')').effect('slide', {'mode':'show', 'direction': display_direction}, 400);
            $('#col3_content > div:eq('+ index +')').fadeIn(800);
            $('.contact_nav a').css('font-weight','normal');
            //$('.page-shop ul.products').show();
            $('.sub_menu span').hide();
            $('.sub_menu span.'+$(this).attr('sub_menu')).show();
        }
        $('#dialog').hide();
    });

    // bind contact_nav navegation
    $('.contact_nav .util_menu a').unbind().click(function() {
            $('#col1, #col3').show();
            $('.sub_menu span').hide();
            $('.contact_nav a').css('font-weight','normal');
            $(this).css('font-weight','bold');
            $('#col1_content > div:visible').hide();
            $('#col3_content > div:visible').hide();
            $('#col1_content > div:eq('+ index +')').effect('slide', {'mode':'show', 'direction': display_direction}, 400);
            $('#col3_content > div:eq('+ index +')').fadeIn(800);
    });
    
    // hard link to open anchors (pages)
    $('.go_anchor').live('click', function() {
        var anchor_name = $(this).attr('href').substr(parseInt($(this).attr('href').indexOf("#"))+1);
        $('div#dialog').hide();
        $('#nav li').removeClass('active');
        $('#nav li.' + anchor_name).addClass('active');
        $('div.page').hide();
        $('div.sidebar').hide();
        $('div.page-' + anchor_name).fadeIn(800);
        $('#col1, #col3').show();
    });
    
    // hard link to open discos
    $('a.go_disco').click(function() {
        $('.disco-item').hide();
        $('.' + $(this).attr('disco')).effect('slide', {'mode':'show', 'direction': 'right'}, 400);
        return false;
    });
    
    /**
     * generic method to load html response into
     * selected selector
     */
    
    $('a.load').live('click', function() {
        var el = $(this);
        $(el.attr('selector')).load(el.attr('href'), function() {
            if(el.hasClass('goback')) {
                $(el.attr('selector') + ' > div').effect('slide', {'direction':'left', 'mode':'show'});
            }
            if(el.hasClass('continue')) {
                $(el.attr('selector') + ' > div').effect('slide', {'direction':'right', 'mode':'show'});
            }
        });
        return false;
    });
    
    /**
     * generic method to load html response into
     * DIALOG OVER
     */
    
    $('a.dialog').live('click', function() {
        var el = $(this);
        $('#col1, #col3').hide();
        $('#dialog').html('');
        $('#dialog').load(el.attr('href'));
        $('#dialog').show();
        /* $('#over').show(); */
        return false;
    });

	$.easing.elasout = function(x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	};
    
    $('div.image_playlist a').click(function() {
        $('div#image_canvas').hide();
        $('div#image_canvas').html('<img src="' + $(this).attr('href') + '" />');
        $('div#image_canvas').fadeIn();
        return false;
    });
    
});

