/**
 * JavaScript scripts set for CMS
 *
 * @copyright Copyright (c) 2009
 * @package   ECTO Framework
 * $Id: public.js 244 2009-12-07 15:44:56Z $
 */

var project = {};

project.title = {};
project.curHash = false;

project.loadPage = function(path)
{
    $.scrollTo(0, 200, function(){

        if(project.title[path] === undefined)
        {
            var hash = parseInt(Math.random() * 10000000);
            project.showLoading();

            $.get(path, function(data)
            {
                var title = /<title>(.*)<\/title>/i.exec(data);
                var html = project.getHtml(data);

                project.title[path] = {title:title[1],hash:hash};
                $('#body').append(html);

                $('.content[id="null"]').attr('id', 'content_'+ hash).hide();

                Cufon.replace('#content_'+ hash +' .bigText');
                Cufon.replace('h1');

                project.hideLoading();
                project.slideIn(path);
            });
        }
        else
        {
            project.slideIn(path);
        }
    });
};

project.slideIn = function(path)
{
    if(project.activePage == project.title[path].hash)
        return false;

    window.location.hash = '#'+ path;
    project.curHash = path;

    $('#body').width(5000);

    $('#body .content:first').after($('#content_'+ project.title[path].hash));
    $('#content_'+ project.title[path].hash).show();

    project.fixSizes();

    $('#body div:first').animate({'marginLeft':'-'+ $(window).width() +'px'}, 800, function(){
        $('#body').append($(this));
        $(this).css('marginLeft',0).hide();
        $('#body').width('auto');

        $(document).ready(function(){
            var newHeight = $(window).height() > $('.content:visible .sep').outerHeight() ? $(window).height() : $('.content:visible .sep').outerHeight();
            $('#body').stop().animate({height:newHeight+'px'}, 300, function(){
                project.fixSizes();
            });
        });
    });
    document.title = project.title[path].title;
    project.activePage = project.title[path].hash;
};

project.showLoading = function()
{
    $('#loading').css({
        top  : ($(window).height() - $('#loading').outerHeight()) / 2 + $(window).scrollTop(),
        left : ($(window).width() - $('#loading').outerWidth()) / 2
    }).show();
};

project.hideLoading = function()
{
    $('#loading').hide();
};

project.divIndex = 0;
project.getHtml = function(data)
{
    project.divIndex = 0;

    while(/<div /ig.test(data))
    {
        project.divIndex++;
        data = data.replace(/<div /i, '<div:'+ project.divIndex +' ');
    }
    while(/<\/div>/ig.test(data))
    {
        data = data.replace(/<\/div>/i, '</div:'+ project.divIndex +'>');
        project.divIndex--;
    }

    var tmp = /<div:1 id="body">([\S\s]*?)<\/div:1>/im.exec(data);

    if(tmp === null)
        alert(data);

    var html = tmp[1];
    html = html.replace(/<div:\d{1,7} /ig, '<div ');
    html = html.replace(/<\/div:\d{1,7}>/ig, '</div>');

    return html;
};

project.fixSizes = function()
{
    $('.content').height($(window).height() > $('#body').height() ? $(window).height() : $('#body').height());
    $('.content').width($(window).width());
};

project.imgSlideIn = function(e)
{
    $(e).find('img').stop().animate({left:'0px'}, {queue:false,duration:300});
};
project.imgSlideOut = function(e)
{
    $(e).find('img').stop().animate({left:$(e).width()+'px'}, {queue:false,duration:300});
};

project.checkHash = function()
{
    if(project.curHash && project.curHash != window.location.hash.substr(1))
    {
        if(window.location.hash.substr(1) == '')
        {
            project.curHash = '/';
            project.loadPage(project.curHash);
        }
        else
        {
            project.curHash = window.location.hash.substr(1);
            project.loadPage(project.curHash);
        }
    }
    setTimeout(function(){project.checkHash();}, 500);
};

$(document).ready(function(){
    if(window.location.hash != '')
    {
        window.location = project.curHash = window.location.hash.substr(1);
    }

    var hash = parseInt(Math.random() * 10000000);
    $('.content').attr('id', 'content_'+ hash);

    project.title[window.location.pathname] = {title:document.title,hash:hash};
    project.activePage = hash;

    $('body').append('<div id="loading"><img src="/design/spinner.gif" alt="" /></div>');

    Cufon.replace('#content_'+ hash +' .bigText');
    Cufon.replace('h1');
    Cufon.now();

    project.fixSizes();
    setTimeout(function(){project.checkHash();}, 2000);
});

$(window).resize(function(){ project.fixSizes(); });

var activeImg = [];
var allImages = [];

function expandImg(type, index)
{
    if(activeImg[type] === undefined)
        activeImg[type] = 0;

    if(index === undefined)
    {
        activeImg[type]++;
        index = activeImg[type];

        if(allImages[type][index] === undefined)
        {
            index = activeImg[type] = 0;
        }
    }
    else
    {
        index--;
        activeImg[type] = index;
    }
    $('#mainImg_'+ type).attr('src',allImages[type][index]);
    $('.galleryInner li').removeClass('active');
    $('.galleryInner li:eq('+ index +')').addClass('active');
}
