jQuery(function ($) {
    $("a.sub-menu-activate").bind("click mouseover mouseout", function(e) {
        var $el = $(e.target).next().next();
        if($el.is("ul")) {
            $el.toogle();
        }
    });
    
    if(0)
    if($.cookie("inited")) {
        $("body").scrollTo("#content", 1000);
    } else {
        $.cookie("inited", true);
    }

    $.fn.gblSlider = function (options) {
        var defaults = {
            direction: "horizontal",
            step_delay: 5000,
            animation: true,
            animation_speed: "fast",
            next_selector: false,
            prev_selector: false,
            status_selector: false
        };
        options = $.extend(defaults, options);

        return this.each(function () {
            var t = $(this),
            slides_holder = t.find("ul:first"),
            slides = t.find("li"),
            pos = 1,
            slides_no = slides.length,
            first = slides.first(),
            w = 0,
            h = 0,
            step_size = 0,
            scroll_func = "scrollLeft",
            timeout_id = -1,
            step_delay = options.step_delay,
            playing = false,
            status = $(options.status_selector);

            t.scrollLeft(0);
            t.scrollTop(0);

            slides.last().after(slides.first().clone());
            slides.first().before(slides.last().clone());
            
            t.css({
                "overflow": "hidden"
            });
            w = first.innerWidth();
            h = first.innerHeight();
            t.css({
                "width": w //,             "height": h
            });
            if(options.direction === "horizontal") {
                scroll_func = "scrollLeft",
                step_size = w;
                slides_holder.width((slides_no + 3) * step_size).find("li").css({"float": "left"});
            } else {
                scroll_func = "scrollTop",
                step_size = h;
            }
            t[scroll_func](step_size);

            t.parent().hover(function() {
               status.text("oprit");
               pause();
            }, function (e) {
                status.text("animat");
                play();
            });

            if(options.next_selector) {
                $(options.next_selector).bind("click", function () {
                    pause();
                    next();
                })
            }
            if(options.prev_selector) {
                $(options.prev_selector).bind("click", function () {
                    prev();
                })
            }

            /*function scroll (animated, param) {
                var o = {};
                if(options.animation && animated && typeof param === "function") {
                    o[scroll_func] = pos * step_size;
                    t.animate(o, options.animation_speed, param);
                } else {
                    t[scroll_func](param);
                }
            }

            function adjust () {
                if(pos > slides_no) {
                    pos = 1;
                    scroll(false, step_size);
                } else if (pos <= 0) {
                    pos = slides_no;
                    scroll(false, pos * step_size);
                }
            }

            function go(diff, func, time) {
                pos = pos + diff;
                //scroll(true, adjust);
                if(func && time) {
                    scroll(true, function () {
                        adjust();
                        if(playing) {
                            //timeout_id = setTimeout(func, time);
                            t.delay(time, func);
                        }
                    });
                } else {
                    scroll(true, adjust);
                }
            }

            function next() {
                go(1);
            }

            function prev() {
                go(-1);
            }

            function run() {
                if(playing) {
                    go(1, run, step_delay);
                    //timeout_id = setTimeout(run, step_delay);
                }
            }

            function play() {
                if(!playing) {
                    playing = true;
                    run();
                }
            }

            function pause() {
                clearTimeout(timeout_id);
                timeout_id = -1;
                playing = false;
            }*/

            function pause() {
                t.clearQueue();
                playing = false;
                clearTimeout(timeout_id);
            }

            function go(diff) {
               pos = pos + diff;
            }

            //adjust the scroll skip to the first or the second item
            function adjust () {
                if(pos >= slides_no) {
                    pos = 0;
                    t[scroll_func](pos * step_size);
                } else if (pos <= 0) {
                    pos = slides_no;
                    t[scroll_func](pos * step_size);
                }
            }
            
            

            function step(diff) {
                var o = {};
                adjust();
                go(diff);
                o[scroll_func] = pos * step_size;
                t.stop().animate(o, options.animation_speed);
            }

            function next() {
                step(1);
            }

            function prev() {
                step(-1);
            }

            function run() {
                if(playing) {
                    var o = {};
                    o[scroll_func] = pos * step_size;
                    t.clearQueue().delay(step_delay).animate(o, options.animation_speed, function() {
                       adjust();
                       go(1);
                       run();
                    });
                }
            }

            function play() {
                if(!playing) {
                    t.stop().clearQueue();
                    playing = true;
                    clearTimeout(timeout_id);
                    timeout_id = setTimeout(run, 1000);
                }
            }

            play();
        });

    };

$("#slider").gblSlider({
    next_selector: "#next",
    prev_selector: "#prev",
    status_selector: "#status"
});

/*$("#slider_").easySlider({
    auto: true,
    continuous: true,
    nextId: "prev",
    prevId: "next",
    controlsShow: false
});*/


$("#addresses > .address").each(function(i) {
    var el = $(this),
    lnk = el.find("a"),
    cnv = $("div.map_canvas");
    if (cnv[i] && GBrowserIsCompatible() && lnk.length > 0) {
        var map = new GMap2(cnv[i]);
        map.setCenter(new GLatLng(el.data("lat"), el.data("lon")), 17);
        map.openInfoWindow(map.getCenter(), $("<span><a href=\"" + lnk.attr("href") + "\">" + el.text() + "</a></span>")[0]);
      }
});
   

$("table table tr:even").addClass("even");
$("table.prices tr:even").addClass("even");
if("datepicker" in $) {
	$.datepicker.regional['ro'] = {
			closeText: 'Închide',
			prevText: '&laquo; Luna precedentă',
			nextText: 'Luna următoare &raquo;',
			currentText: 'Azi',
			monthNames: ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie',
			'Iulie','August','Septembrie','Octombrie','Noiembrie','Decembrie'],
			monthNamesShort: ['Ian', 'Feb', 'Mar', 'Apr', 'Mai', 'Iun',
			'Iul', 'Aug', 'Sep', 'Oct', 'Noi', 'Dec'],
			dayNames: ['Duminică', 'Luni', 'Marţi', 'Miercuri', 'Joi', 'Vineri', 'Sâmbătă'],
			dayNamesShort: ['Dum', 'Lun', 'Mar', 'Mie', 'Joi', 'Vin', 'Sâm'],
			dayNamesMin: ['Du','Lu','Ma','Mi','Jo','Vi','Sâ'],
			weekHeader: 'Săpt',
			dateFormat: 'yy-mm-dd',
			timeFormat: 'hh:mm',
			firstDay: 1,
			isRTL: false,
			showMonthAfterYear: false,
			yearSuffix: ''
			
	};
}

});
