/**
 * @author ronzilca
 * @copyright 2011
 */

function loadLightbox(url)
{
    $.colorbox({
        top: 50,
        open: true,
        href: url,
        opacity: 0.3
    });
}

$(function(){
    $("#dialog").dialog({ 
        autoOpen: false,
        height: "auto",
        modal: true,
        resizable: false
    });
});

$(function(){
    $("* [title]").tipsy(); 
});

$(function(){
    if (window.location.port == "869") {
        _basepath   = "http://" +  window.location.hostname + ":" + window.location.port + "/offpiste/";
    } else {
        _basepath   = "http://" +  window.location.hostname + "/";
    }
    
    $("textarea.tinymce").tinymce({
        script_url: _basepath + 'lib/js/tinymce/tiny_mce.js',
        theme: "advanced",
        theme_advanced_toolbar_location: "top",
        theme_advanced_buttons1: "bold,italic,underline,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,cut,paste,undo,redo,separator,link,unlink,separator,fontsizeselect,forecolor,image,code,cleanup,removeformat",
        theme_advanced_buttons2: "",
        theme_advanced_buttons3: "",
        directionality : "rtl",
        language : "he",
        force_p_newlines: false,
        force_br_newlines: true
    });
});

function updateMCE() {
    tinyMCE.triggerSave();
}

(function($){  
    $.fn.simpleSpy = function (limit, interval) {
        limit = limit || 1;
        interval = interval || 4000;
        
        return this.each(function () {
            var $list = $(this),
                items = [], 
                currentItem = limit,
                total = 0,
                height = $list.find('> li:first').height();
                
            $list.find('> li').each(function () {
                items.push('<li>' + $(this).html() + '</li>');
            });
            
            total = items.length;
            
            $list.wrap('<div class="spyWrapper" />').parent().css({ height : height * limit });
            
            $list.find('> li').filter(':gt(' + (limit - 1) + ')').remove();
           
            function spy() {
                var $insert = $(items[currentItem]).css({
                    height : 0,
                    opacity : 0
                }).prependTo($list);
                            
                $list.find('> li:last').animate({ opacity : 0}, 1000, function () {
                    $insert.animate({ height : height }, 1000).animate({ opacity : 1 }, 1000);
                    
                    $(this).animate({ height : 0 }, 1000, function () {
                        $(this).remove();
                    });
                });
                
                currentItem++;
                if (currentItem >= total) {
                    currentItem = 0;
                }
                
                setTimeout(spy, interval);
            }
            spy();
        });
    };
})(jQuery);
