var galleryScrollHeight = 620;

var menuCloud = null;


var hash = "";
function checkHistory(){
    if (window.location.hash != hash){
        hash = window.location.hash;
        loadContent(window.location.hash.substring(1));

    }    
}

function loadContent(value){
    if (value == ""){
        window.location.reload();
        return;
    }
    try {
        menuCloud.updateSelected(value);
        if(typeof(console) !== 'undefined' && console != null) {
            console.log("menuCloud.updateSlected( ",value, ")");
        }
    } catch (e){
        if(typeof(console) !== 'undefined' && console != null) {
            console.log(e);
        }
    }

   $('#content-main').html('<p>loading</p>').load('index.php?_contents=' + value, function() {
        bindGallery();
        $('.pp_pic_holder, .pp_overlay, .ppt').remove();
        var images = $("a[rel^='prettyPhoto']");
        if (images.size() > 0) {
            images.prettyPhoto({theme:'facebook'});
        }

    });
}


function checkScrollUp() {
    var top = $('.gallery-scroll').scrollLeft();
    if (top <= 0) {
        $('.arrow-left').css('opacity', 0.4);
    } else {
        $('.arrow-left').css('opacity', 1);
    }
}

function checkScrollDown() {
    var top = $('.gallery-scroll').scrollLeft();
    if (top >= $('.gallery-scroll-content').width() - galleryScrollHeight) {
        $('.arrow-right').css('opacity', 0.4);
    } else {
        $('.arrow-right').css('opacity', 1);
    }
}
function toggleContent(value) {
    window.location.hash = value;
}

function scrollerShowNext() {
    var next = $('.image-scroller li:visible').hide().next();
    if (next.size() == 0) {
        next = $('.image-scroller li').eq(0);
    }
    next.show();
}

function bindGallery() {
    $('.arrow-right').click(function() {
        var left = $('.gallery-scroll').scrollLeft() + galleryScrollHeight;
        $('.gallery-scroll').animate({
            scrollLeft: left
        }, {
            duration: 550,
            complete:function() {
                checkScrollUp();
                checkScrollDown();
            }
        });
    });

    $('.arrow-left').click(function() {
        var left = $('.gallery-scroll').scrollLeft() - galleryScrollHeight;
        $('.gallery-scroll').animate({
            scrollLeft: left
        }, {
            duration: 550,
            complete:function() {
                checkScrollUp();
                checkScrollDown();
            }
        });
    });
    $('.gallery-scroll').scrollLeft(0);
    checkScrollUp();
    checkScrollDown();
}

var footerText = {};
function initContact() {
    var val;
    $("#footer-form input, #footer-form textarea").each(function() {
        val = this.tagName == 'textarea' ? $(this).html() : $(this).val();
        footerText[$(this).attr('name')] = val;
        $(this).css('color', '#aaa');
    }).bind('focus', function() {
        val = this.tagName == 'textarea' ? $(this).html() : $(this).val();
        if (val == footerText[$(this).attr('name')]) {
            if (this.tagName == 'textarea') {
                $(this).html('');
            } else {
                $(this).val('');
            }
            $(this).css('color', '#000');
        }
    }).bind('blur', function() {
        val = this.tagName == 'textarea' ? $(this).html() : $(this).val();
        if (val == '') {
            if (this.tagName == 'textarea') {
                $(this).html(footerText[$(this).attr('name')]);
            } else {
                $(this).val(footerText[$(this).attr('name')]);
            }
            $(this).css('color', '#aaa');
        }
    });
}


$(document).ready(function() {
    menuCloud = (window["menuCloud"]) ? window["menuCloud"] : document["menuCloud"];
    setInterval(checkHistory, 100);
    $('.image-scroller li').each(function(i) {
        if (i > 0) {
            $(this).hide();
        }
    });
    setInterval(scrollerShowNext, 1000 * 10);
    bindGallery();
    initContact();
    $("a[rel^='prettyPhoto']").prettyPhoto({theme:'facebook'});

});

