var baseUri;
var view;
var ajaxSpinner = true;

function animationInProgress(){
    return $(":animated").size() == 0 ? false : true;
}

String.prototype.endsWith = function(str)
{
    return (this.match(str+"$")==str)
}

function hashCheck(){
    var hash = (window.location.hash).substring(1, (window.location.hash).length);
    return (hash == "contact" || hash == "workshop");
}

//contacts and workshop - people info

$("#bottomGallery img").live("click",function(){
    if(hashCheck()){
        var el = $(this);
        if(!el.hasClass("selected")){
            var html = $("div[rel=" + $(this).attr("rel") + "]").html();

            if(!$("#description").hasClass("hidden")){
                $("#description p").fadeOut(300,function(){
                    $("#description p").html(html).fadeIn(300);
                });
            }
            else{
                $("#description p").html(html);
                $("#description").removeClass("hidden",600);
            }

            $("#bottomGallery img.selected").removeClass("selected").fadeTo(300,0.6);
            $(this).addClass("selected");
        }
    }
}).live("mouseover",function(){
    if(hashCheck()){
        $(this).stop().fadeTo(300,1);
    }
}).live("mouseout",function(){
    if(hashCheck()){
        if(!$(this).hasClass("selected")){
            $(this).stop().fadeTo(300,0.6);
        }
    }
});

$("#description").live("click",function(){
    if(hashCheck()){
        $(this).addClass("hidden",300);
        $("#bottomGallery img.selected").removeClass("selected").fadeTo(300,0.6);
    }
});

// hide flash messages
$("div.flash").livequery(function () {
    var el = $(this);
    setTimeout(function () {
        el.fadeOut();
    }, 3000);
});

$("#menu a.eshop").live("click",function(){
    alert("E-shop still in (re)construction.");
    return false;
});

$("a").live("click",function(){

    var el = $(this);

    if ($(":animated").size() != "0" || el.hasClass("selected")){
        return false;
    }

    if(el.hasClass("ajax")){

        var href = this.href;
        var hash = (window.location.hash).substring(1, (window.location.hash).length);

        if(el.hasClass("language")){
            $.get(el.attr("href"),{
                language:el.attr("rel"),
                hash:window.location.hash
            });
            return false;
        }

        if(el.attr("id") == "logo"){
            if((href.endsWith("gallery/") && hash != "gallery") || (hash == "gallery" && !href.endsWith("gallery/"))){
                galleryChanges(el,600,true);
            }
            else{
                $("#menu a.selected").removeClass("selected",600,function(){
                    $("#menu a.first").addClass("selected",600);
                });
            }
        }

        if(el.hasClass("randomPortfolio")){
            $("#menu a.selected").removeClass("selected",600,function(){
                $("#menu a.portfolio").addClass("selected",600);
            });
        }

        if(el.parents("#menu").size() > 0){
        
            /*
             * when redirecting to default, ie8 not handling ajax (not updating snippets)
             * with url without the 'default/' bit at the end
             */
            /*if(href.endsWith(baseUri)){
            href += "default/";
        }*/

            //going from gallery to other pages or vice-versa

            if((href.endsWith("gallery/") && hash != "gallery") || (hash == "gallery" && !href.endsWith("gallery/"))){
                galleryChanges(el,600,true);
            }
            else{
                $("#menu a.selected").removeClass("selected",600,function(){
                    el.addClass("selected",600);
                });
            }

            $(this).blur();
        }

        $.get(this.href);
        return false;
    }

});

//vypnout aj
//zmenit barvu spodniho ramecku ve fotogalerii
//odstranit konzoli
//pismo v menu ve fotogalerii

function galleryChanges(el,speed,all){

    //click on home
    if(el.attr("id") == "logo"){
        el = $("#menu a.first");
    }

    $("body").toggleClass("gallery",speed*2);
    if(all){
        $("#menu").toggleClass("gallery",speed*2);
        $("#menu a").each(function(){
            var cur = $(this);
            if(el.attr("href") == cur.attr("href")){
                cur.toggleClass("gallery",speed,function(){
                    cur.addClass("selected",speed);
                });
            }
            else if(cur.hasClass("selected")){
                cur.removeClass("selected",speed,function(){
                    cur.toggleClass("gallery",speed);
                });
            }
            else{
                cur.toggleClass("gallery",speed*2);
            }
        });
    }
    $("#leftColumn").toggleClass("gallery",speed*2);
    $("#content").toggleClass("gallery",speed*2);
    $("#bottomGallery").toggleClass("gallery",speed*2);
}

$("a.mail").live("click",function(){

    //parseInt($(document).scrollTop()) +
    var top = 100;
    var left = Math.round((parseInt(screen.width)-parseInt($("#mailForm").outerWidth()))/2);

    // clear form fields (input, textarea)
    $("#mailForm").find('input[type!="submit"], textarea').val("");

    // remove messages
    $("#mailForm span.message").removeClass().addClass("message").text('');

    //to whom the message will be sent
    var to = this.href.split(":")[1];
    $("#mailFormTo").find("span").text(to);
    $('#mailForm input[name="to"]').val(to);
    // position
    $("#mailForm").css("top",top + "px").css("left",left + "px");    

    // if not visible - fade it in!
    if($("#mailForm:visible").size() <= 0){
        $("#mailForm").fadeIn("fast");
    }

    return false;
});

$("#mailFormClose").live("click",function(){
    $("#mailForm").fadeOut("medium");
    return false;
});

$(function(){
    // ajax spinner
    $('<div id="ajaxSpinner"></div>').hide().ajaxStart(function () {
        if(ajaxSpinner){
            $(this).show();
        }
    }).ajaxStop(function () {
        $(this).hide();
    }).appendTo("body");
});


