/* MAIN FEATURES */

function scrollWin(){
    $('html, body').animate({scrollTop: $("#logo").offset().top}, 750);
}

function loadAccordions(){
    jQuery.each(site_content, function(key, value) {
        $($('div#' + key + ' div.top')[0]).bind("click", function(e)
        {
            scrollWin();
            var name = $($(e.target)[0]).parents('div#main_content div[id]')[0].id;
            animatedcollapse.toggle('content_' + name);
            e.stopPropagation();
        });
        animatedcollapse.addDiv('content_' + key, 'fade=0,speed=' + value.speed + ',group=content');
    });

    animatedcollapse.ontoggle=function($, divobj, state){ //fires each time a DIV is expanded/contracted
        //$: Access to jQuery
        //divobj: DOM reference to DIV being expanded/ collapsed. Use "divobj.id" to get its ID
        //state: "block" or "none", depending on state
        if(divobj == undefined) return;

        switch(state)
        {
            case 'block': doLoad(divobj); break;
            case 'none':  doUnload(divobj); break;
        }
    }

    animatedcollapse.init();
}

function loadMainShadowboxes(){
    $("a.shadowbox.html").each(
    function(ix){
        $(this).click(function(e){ return openHtmlShadowbox(e); });
    });

    $("a.shadowbox.image").each(
    function(ix){
        $(this).click(function(e){ return openImgShadowbox(e); });
    });
}

function getMainLink(divobj){
    var script = eval('site_content. ' + $('div#' + divobj.id.replace('content_',''))[0].id + '.script');

    return script;
}

function getContentByAjax(url){
    return $.ajax({
        type: 'get',
        cache: false,
        url: url,
        async: false
    }).responseText;
}

function doLoad(divobj)
{
    $.ajax({
        type: 'GET',
        url: getMainLink(divobj),
        cache: false, // needed for ie
        success: function(h)
        {
            divobj.innerHTML = h;
            bindPreviewsHandlers();
			loadMainShadowboxes();
        }
    });
}

function doUnload(divobj)
{
    $(divobj).empty().html("Loading...");
}

function doLoadService(url, divID){

    var content = $('#' + divID )[0];
    content.innerHTML = "<span class='loading'>Loading...</span>";
    content.style.background = "#FFFFFF url('/themes/ld/imagenes/indicator.gif') no-repeat center center";

    content.innerHTML = getContentByAjax(url);
    content.style.background = "url(/themes/ld/imagenes/wm_portfolio.gif) no-repeat 10px 0";

    bindPreviewsHandlers();

    return false;
}

function restricted_service()
{
    if($('#pw_confidential').val())
    {
        var psw = $('#pw_confidential').val();
        doLoadService('/extensions/liquid/content/portfolio_content.php?section=11&ajax=1&psw='+psw,'portfolio_content');
    }
    else
    {
        alert('Please enter your access password');
    }
    return false;
}
/* SHADOWBOX FEATURES */

function openHtmlShadowbox(evento){
    var aTag = ($(evento.target).is("a.shadowbox"))?evento.target:$(evento.target).parents('a.shadowbox')[0];
    var html = $.ajax({ type: 'get', url: aTag.href, async: false}).responseText;
    Shadowbox.open({content: html, player: "html", width: 950, height: 500});
    return false;
}

function openImgShadowbox(e){
    var aTag = $(e.target).parents('a.shadowbox')[0];
    Shadowbox.open({ content: aTag.href, title: aTag.title, player: "img"});
    return false;
}

function bindPreviewsHandlers(){
    $("div#portfolio_content a.shadowbox.html").each(function(ix){
        $(this).click(function(e){ return openHtmlShadowbox(e); });
    });

    $("div#image_side a.shadowbox.img").each(function(ix){
        $(this).click(function(e){ return openImgShadowbox(e); });
    });
}

function createIfExistsImgSliders()
{
    if($("#slider").length > 0)
    {
        $("#slider").easySlider();
    }
}
