﻿var lenart = 0;
var lenevents = 0;
var direvent = "rtl";
var regDate = /^([0-9]{2})\/[0-9]{2}\/([0-9]{4})$/;
$(document).ready(function() {
    $(".button").mousedown(function() { $(this).addClass("mousedown"); }).mouseup(function() { $(this).removeClass("mousedown"); });
    $("#calender").datepicker({
        dayNamesMin: ['Sö', 'Må', 'Ti', 'On', 'To', 'Fr', 'Lö'],
        dayNamesShort: ['Sön', 'Mån', 'Tis', 'Ons', 'Tor', 'Fre', 'Lör'],
        monthNames: ['Januari', 'Februari', 'Mars', 'April', 'Maj', 'Juni', 'Juli', 'Augusti', 'September', 'Oktober', 'November', 'December'],
        firstDay: 1,
        onSelect: function(dateText, inst) {
            $(document).trigger('dateselected', [dateText]);
            var array = dateText.toString().match(regDate);
            if (array) {
                $(document).trigger('monthchanged', [array[1], array[2]]);
            }
        },
        onChangeMonthYear: function(year, month, inst) {
            $(document).trigger('monthchanged', [month, year]);
        },
    });
    $(document).bind('eventspopulated', function(e, info) {
        if (!info.arr) return;
        for (var k in info.arr) {
            $(".ui-datepicker-calendar td a.ui-state-default").each(function() {
                var html = $(this).html();
                var item = info.arr[k].toString();
                if (html == item) {
                    $(this).parent().addClass("artist-selected");
                }
            });
        }
    });
    $(window).load(function(){
        var mydate = new Date();
        $(document).trigger('monthchanged', [mydate.getMonth()+1, mydate.getFullYear()]); 
    });
    lenart = $(".artist-slider .faces div").length;
    lenevents = $(".events .event").length;

    var artistChanged = function(){
        $(document).trigger('artistChanged');
    };

    $(".artist-slider").each(function() {
        if (lenart < 1) {
            $(this).find(".prev, .next").hide();
        } else {
            var i = 0;
            $(this).find(".faces div").each(function(i) {
                if (i == 0) $(this).addClass("first");
                else if (i == 1) $(this).addClass("second");
                else if (i == 2) $(this).addClass("third");
                i++;
            });
            artistChanged();
        }
    });
    $(".artist-slider .next").click(function() {
        var inx = $(this).parent().find(".faces .second").index();
        if (inx + 1 == lenart) {
            return false;
        }
        $(this).parent().find(".faces .first").removeClass("first");

        $(this).parent().find(".faces .second").addClass("first").removeClass("second");
        $(this).parent().find(".faces .third").removeClass("third").addClass("second");
        $(this).parent().find(".faces .second").next().addClass("third");

        artistChanged();
    });
    $(".artist-slider .prev").click(function() {
        var inx = $(this).parent().find(".faces .second").index();
        if (inx == 0) {
            return false;
        }
        $(this).parent().find(".faces .third").removeClass("third");
        $(this).parent().find(".faces .second").removeClass("second").addClass("third");
        $(this).parent().find(".faces .first").removeClass("first").addClass("second");
        $(this).parent().find(".faces .second").prev().addClass("first");

        artistChanged();
    });
    $(".events .event:first").each(function() {
        $(this).addClass("show");
        $(this).next().addClass("show");
        $(this).next().next().addClass("show");
    });
    if (lenevents > 3) {
        setInterval("slideEvents()", 5000);
        $(".einfo .prev").click(function() {
            $(".events .event.show:last").each(function() {
                ind = $(this).index();
                if (ind > 2) {
                    $(this).removeClass("show");
                    $(this).prev().prev().prev().addClass("show");
                }
            });
        });
        $(".einfo .next").click(function() {
            $(".events .event.show:first").each(function() {
                ind = $(this).index();
                if (ind < lenevents - 3) {
                    $(this).removeClass("show");
                    $(this).next().next().next().addClass("show");
                }
            });
        });
    } else {
        $(".einfo .prev, .einfo .next").hide();
    }
});
function slideEvents() {
    ind = $(".events .event.show:first").index();
    if (ind == lenevents-3) direvent = "ltr"; else if (ind == 0) direvent = "rtl";
    if (direvent == "rtl") {
        $(".events .event.show:first").each(function(){
            $(this).removeClass("show");
            $(this).next().next().next().addClass("show");
        }); 
    } else {
        $(".events .event.show:last").each(function(){
            $(this).removeClass("show");
            $(this).prev().prev().prev().addClass("show");
        }); 
    }
}
function mousePageXY(e)
{
  var x = 0, y = 0;

  if (!e) e = window.event;

  if (e.pageX || e.pageY)
  {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }

  return {"x":x, "y":y};
}
