$(document).ready(function(){

    $('#msg1.en').stop().animate(
        {width:'601px'},
        2500,
        function(){
            $('#msg2.en').stop().animate({width:'298px'}, 1500);
        }
    );

    $('#msg1.el').stop().animate(
        {width:'637px'},
        2500,
        function(){
            $('#msg2.el').stop().animate({width:'360px'}, 1500);
        }
    );

    var $premSlides = $("#premSlides");
    var $premSlidesImages = $premSlides.find("img");
    var premZIndex = 5002;
    var animations = {
        large: {
            width: "301",
            height: "23",
            fontSize: "21px"
        },
        normal: {
            width: "270",
            height: "24",
            fontSize: "17px"
        }
    };
    $('#smenu li a').each(function(n){
        $(this).bind({
            mouseenter: function() {
                premZIndex++;
                $premSlidesImages.eq(n).css('z-index', premZIndex).fadeIn();
                $(this).stop(false, true).animate(animations.large, "fast", function(){
                    //$premSlides.css('background-image', 'url('+$premSlidesImages.eq(n).attr('src')+')');
                });
            },
            mouseleave: function() {
                premZIndex--;
                $(this).stop().animate(animations.normal, "fast", function(){
                    $premSlidesImages.eq(n).css('z-index', premZIndex).fadeOut();
                    //$premSlides.removeAttr('style');
                });
            }
        });
    });

	// setup ul.tabs to work as tabs for each div directly under div.panes
	$("ul.tabs").tabs("div.panes > div");

    $('#slider').nivoSlider( {
        manualAdvance:false
    });

    $('#a1').click(function() {
        map_create();
    });

    $('#a2').click(function() {
        map_destroy();
    });

    $('.toggle-next').bind('click', function() {
        toggleAll = true;
        $next = $(this).next('.toggleable');
        $flag = $('span:first', this);
        if ($next.is(':visible')) {
            $next.slideUp();
            $flag.text('\u25BA');
        } else {
            $next.slideDown();
            $flag.text('\u25BC');
            if (toggleAll) {
                $('.toggleable').not($next).slideUp();
            }
        }
        return false;
    });

    $('.trigger-send-page').click(function(){
        $(this).sendPage();
        return false;
    });

    $('input[placeholder]').inputPlaceholder();
});

(function($){
    $.fn.sendPage = function() {
        var $this = $(this);
        var pageData = {
            title: $('[itemprop="title"]').text(),
            summary: $('[itemprop="summary"]').text(),
            url: document.URL
        };
        var updateDialog = function(dlg) {
            var f = $('form', dlg);
            $.ajax({
                url: $this.attr('href'),
                type: 'post',
                dataType: 'html',
                data: f.length ? f.serialize() : pageData,
                success: function(response) {
                    $(dlg).html(response);
                    $('form', dlg).submit(function(){
                        updateDialog(dlg);
                        return false;
                    });
                    $('.trigger-close', dlg).click(function(){
                        $(dlg).dialog('close');
                        return false;
                    });
                }
            });
        }
        $('<div></div>').dialog({
            width: 450,
            height: 'auto',
            position: ['center', 64],
            modal: true,
            dialogClass: 'ui-no-title',
            //show: 'easeOutBounce',
            title: this.title ? this.title : $this.text(),
            open: function() {updateDialog(this)},
            close: function() {$(this).dialog('destroy').remove()}
        });
    }

    $.fn.inputPlaceholder = function() {
        if (!$.support.placeholder) {
            this.each(function(){
                var $this = $(this);
                if ($this.val() === '') {
                    $this.val($this.attr('placeholder'));
                }
                $this.focus(function() {
                    if($this.val() === $this.attr('placeholder')) {
                        $this.val('');
                    }
                });
                $this.blur(function() {
                    if($this.val() === '') {
                        $this.val($this.attr('placeholder'));
                    }
                });
            });
        }
        return this;
    }
})(jQuery);

jQuery.support.placeholder = (function(){
    var i = document.createElement('input');
    return 'placeholder' in i;
})();

