jQuery.fn.exists = function() { return (this.length > 0); };
var page = 0;

$(document).ready(function(){

	Shadowbox.init();
});

jQuery(function(){
    $('#navigation li:last').addClass('last');
    
    $('#calendar #left, #calendar #right').live('click',function(){
        var direction = $(this).attr('id');
        $('#calendar').hide("slide", {direction: direction}, 400);
        doCalendar(direction);
        return false;
    });
    
    $('#home-events ul').cycle();
    
    $('#text-size a').click(function(){
        doResize($(this).attr('id'));
        return false;
    });
    
    var size = readCookie('text-size');
    if(size)
        doResize(size);
        
    $(window).bind("resize", function(){
        resizeOverlay();
        $("#book-overlay").trigger("resize");
    }).trigger("resize");
    resizeOverlay();
    
    $('.book-button').click(function(){
        if($('#book-overlay').exists()) {
            var href = $(this).attr('href');
            if(href!='/book-offline') {
                $('#book-overlay #online a').attr('href', href);
                $('#book-overlay').fadeIn('slow'); 
                return false;   
            } 
        }         
    });
    
    $('#book-overlay').click(function(){
        $('#book-overlay').fadeOut('slow');     
    });
 
});

function resizeOverlay() {
    if($('#book-overlay').exists()) {
        var height = getWindowHeight();
        var width = getWindowWidth();
        var sized = height;
        height = $('body').css('height');
        if(height) {
            height = parseInt(height.substr(0,height.indexOf('p')))+8;
            if(height > sized)
                sized = height;
        }
            
        $('#book-overlay').css('height',(sized+40)+'px');
    }
}


function doResize(size) {
    $('#text-size a').removeClass('selected');
    $('#text-size a#'+size).addClass('selected');
    switch(size) {
        case 'normal': {
            $('#content #copy, #content #detail p, #content #description p').css('font-size','1.0em');
            break;
        }
        case 'medium': {
            $('#content #copy, #content #detail p, #content #description p').css('font-size','1.2em');
            break;
        }
        case 'large': {
            $('#content #copy, #content #detail p, #content #description p').css('font-size','1.4em');
            break;
        }
    }
    eraseCookie('text-size');
    createCookie('text-size',size,365); // 1 year :)
}

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

function doCalendar(direction) {
    //$('#payment-form').html('<img src="/media/images/icons/ajax-loader-small.gif" alt="Loading..." />');
    var url = window.location.toString();
    url = url.split('?'); url = url[0]; url = url.split('/');
    furl = '';
    for(var x=3;x<url.length;x++)
        furl += '/'+url[x];

    $.post('/ajax',
    {
        'act': 'ucalendar',
        'year': $('input[name="year"]').val(),
        'month': $('input[name="month"]').val(),
        'direction': direction,
        'ob_alias': furl
    },
    function(data){
        if(data.error) {
            alert(data.error);
            return;
        }    
        var d = (direction=='left'?'right':'left');
        $('#calendar').html(data.result);
        $('#calendar').show("slide", {direction: d}, 400);
    },'json');
}
