//LIGHTBOX
$(document).ready(function(){
    $(".table_02 a:has(img)").lightBox();    
    $(".table_01 .block-img:has(img)").lightBox();    
    $("a.lightbox").lightBox();
    $("div.lightbox.img-left,div.lightbox.img-right").each(function(){
        $(this).find("a").lightBox();
        $(this).find("a").not(":first").hide();
    });
});

//SLIDER 01
$(document).ready(function(){
    if($("#slider_01 ul li").length>3){
        var container_width=parseFloat($("#slider_01 div").width())+13;
        var slider_width=0;
        var page=0;
        $("#slider_01 div ul li").each(function(){
            slider_width = slider_width+(150+13);
        });
    
        var page_end=slider_width/container_width;
        if(page_end==parseInt(page_end)){
            page_end = page_end-1;
        } else {
            page_end = parseInt(page_end);
        }
        
        //insert pagination
        $("#slider_01 ul").css({width:slider_width+"px"});
        if(slider_width>parseFloat($("#slider_01 div").width())+13){
            $("#slider_01").append('<img src="'+phrasebook.URL_ROOT+'/images/button-01-prev.png" title="prethodna" class="prev off" /><img src="'+phrasebook.URL_ROOT+'/images/button-01-next.png" title="sljedeća" class="next" />');
        }
        
        //slide on click
        $("#slider_01 .prev, #slider_01 .next").click(function(){
            if($(this).is(".off")){ return false; }
            $("#slider_01 .prev, #slider_01 .next").removeClass("off");
            if($(this).is(".prev")){
                page = page-1;
            } else {
                page = page+1;
            }
            if(page==0){
                $("#slider_01 .prev").addClass("off");
            } else if(page==page_end){
                $("#slider_01 .next").addClass("off");
            }
            $("#slider_01 ul").stop().animate({marginLeft:(-container_width*page)},"slow");
        });
    }
});

//HOME VIDEO
$(document).ready(function(){
    if($("#video").html()!=null){
        var params = {
            wmode: "transparent",
            allowscriptaccess: "always",
            allowfullscreen: "true"
        };
        var attributes = false;
        swfobject.embedSWF($("#video").attr("rel"), "video", "394", "319", "9.0.0", "", "", params, attributes);       
    }
});

//HEADER FLASH
$(document).ready(function(){
    if($("#flash-slideshow").html()!=null){
        var flashvars = {
            url_root: phrasebook.URL_ROOT+"/",
            lang_code: $("body").attr("class")
        };
        var params = {
            wmode: "transparent"
        };
        var attributes = false;
        swfobject.embedSWF(phrasebook.URL_ROOT+"/flash/header.swf", "flash-slideshow", "954", "230", "9.0.0", "", flashvars, params, attributes);       
    }
});

//TEXT GALLERY
/*
$(document).ready(function() {
    $(".image_block").each(function(){
        $(".image_block li:first").css("opacity","0.4");
        $(".image_block li").click(function(){
            $(this).css("opacity","0.4").siblings("li").css("opacity","1");
            $(this).closest(".image_block").find("img:first").attr("src",$(this).find("img").attr("rel"));
            $(this).closest(".image_block").find("a:first").attr("href",$(this).find("a").attr("href")).attr("title",$(this).find("a").attr("title"));
            return false;
        });        
        $(this).closest(".image_block").find("a:first").lightBox();
    });
});
*/

$(document).ready(function() {
    $(".image_block").each(function(){
		$(this).find("a.mid").lightBox();
        $(this).find('a.mid:first').addClass('active');
		$(this).find('li:first').css("opacity","0.4");
        $(".image_block li").click(function(e){
			e.preventDefault();
            $(this).css("opacity","0.4").siblings("li").css("opacity","1");
            $(this).closest(".image_block").find("a.mid[href="+$(this).find("a").attr("href")+"]").addClass('active').siblings().removeClass('active');
        });        
    });
});

//CONTACT FORM SUBMIT
$(document).ready(function() {
    $(".form_01").submit(function(){
        $(this).find(".error").removeClass("error");
        $(this).find("label:has(em)").each(function(){
            if($(this).next().val()==""){
                $(this).next().addClass("error");
            }
        });
        if($(this).find(".error").length!=0){ return false; }
    });
});

//GOOGLE MAP
$(document).ready(function() {
    if ($("#contact_map").html()!=null){
        DGoogleMap2.setOption( 'zoom', 10 );
        DGoogleMap2.setOption( 'center', new GLatLng(44.5, 16.2) );
        DGoogleMap2.mapDefaultUI.zoom.scrollwheel=false;
        var s = DGoogleMap2.displayMap("#contact_map");
        DGoogleMap2.displayMarkers();
        DGoogleMap2.fitMapToMarkers();
    }
    
    if ($("#about_map").html()!=null){
        DGoogleMap2.setOption( 'zoom', 15 );
        DGoogleMap2.setOption( 'center', new GLatLng(46.164034901455, 15.8692145347595) );        
        DGoogleMap2.mapDefaultUI.zoom.scrollwheel=false;
        DGoogleMap2.mapDefaultUI.controls.overviewmapcontrol=false;
        var s = DGoogleMap2.displayMap("#about_map");
        DGoogleMap2.mapObject.setMapType(G_HYBRID_MAP);
        DGoogleMap2.displayMarkers();
        DGoogleMap2.fitMapToMarkers();
    }
});

//E-POSTCARD POPUP
$(document).ready(function() {
    $(".table_02 .send").click(function(){
        $.get(addAjaxVar($(this).attr("href")), function(data){
            $("select").css("visibility","hidden");
            $("body").append('<div id="popup-overlay" title="zatvori"></div><div id="popup">'+data+'</div>');
            
            var overlayEl = $("#popup-overlay");
            var popupEl = $("#popup");
            var closeButton = popupEl.find(".close");
            
            $(window).resize(function(){ position_popup(overlayEl,popupEl); });
            overlayEl.click(function(){ close_popup(overlayEl,popupEl); });
            closeButton.click(function(){ close_popup(overlayEl,popupEl); });
            
            position_popup(overlayEl,popupEl);
        });
        return false;
    });
    
    function position_popup(overlayEl,popupEl){       
        if(($(window).width()+50)<$(document).width()){
            var width = $(document).width();
        } else {
            var width = $(window).width();
        }         
        overlayEl.css({ height:$(document).height(), width:width, opacity: "0.8" }).fadeIn("slow");
        var left = ($(window).width() - popupEl.width())/2;
        var top = $(window).scrollTop()+50;
        popupEl.css({ left: left, top: top }).fadeIn("slow");
        $("#popup form input:visible:first").focus();
        
        popupEl.find("img").load(function(){
            if ( parseFloat(overlayEl.css("height"))!=parseFloat($(document).height()) ){
              overlayEl.css({ height:(parseFloat($(document).height())+20)+"px" });
            }
        });       
        
    }
    
    function close_popup(overlayEl,popupEl){       
        popupEl.fadeOut("slow",function(){
            popupEl.remove();
        });
        overlayEl.fadeOut("slow",function(){
            $("select").css("visibility","visible");        
            overlayEl.remove();
        });
        $(window).unbind("resize");
    }
    
    $("#popup form").live("submit",function(){
        $(this).find(".error").removeClass("error");
        $(this).find("label:has(em)").each(function(){
            if($(this).next().val()==""){
                $(this).next().addClass("error");
            }
        });
        if($(this).find(".error").length==0){
            $.get($(this).attr("action")+"&"+$(this).serialize(), function(data){
                if(data=="wrong_captcha"){
                    $("#popup form input[name=captcha]").addClass("error");
                }
                if(data=="wrong_email"){
                    $("#popup form input[name=reciver_email]").addClass("error");
                } 
                
                if($("#popup form .error").length==0){
                    $("#popup form fieldset").html("<p><br /><br />"+data+"</p>");
                }
            });
        }
        return false;
    });
});

//OTHER FUNCTIONS
function addAjaxVar(url){
    var timestamp = new Date().getTime();
    if(url.search(/\?/)==-1){
        url=url+"?ajax="+timestamp;
    } else {
        url=url+"&ajax="+timestamp;
    }    
    return url;
}

function scrollbarWidth() {
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
    $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}

