var activeslide = 0;

function onBefore() {
    var slides = $(this).parents(".slideshow").eq(0).find("div.slide");
    var idx = slides.index(this);

    var currentclass = parseInt($('.slideshow .slide').eq(activeslide).attr('class').match(/slide\_\d?/).toString().split('_')[1]);
    var newclass = currentclass;

    $(this).removeClass('slide_0 slide_1 slide_2 slide_3 slide_4 slide_5');
    
    if (currentclass-1 < idx) {
        newclass = currentclass + 1;
    } else if (currentclass-1 > idx) {
        newclass = currentclass - 1;
    }

    $(this).addClass( 'slide_'+newclass);

    activeslide = idx;

    $('#homepage_slider .navigation a').removeClass('selected');
    $('#homepage_slider .navigation a:eq('+idx+')').addClass('selected');

    $('#slider_number p').text((idx+1 < 10 ? '0' : '') + (idx+1));

}

function onAfter() {

}

$(function() {

    if ($('.slideshow').length > 0 || $('.coverImages').length > 0) {
    $('.slideshow').cycle({
        fx:      'scrollLeft',
        speed:   1200,
        easeIn:  'easeInOutExpo',
        easeOut: 'easeInOutExpo',
        timeout: 6000,
        before:  onBefore,
        after:   onAfter,
        pause:   true
    });

    $('.coverImages').cycle({
        fx:      'fade',
        speedO:  8000,
     //   easeIn:  'easeInOutExpo',
     //   easeOut: 'easeInOutExpo',
        timeout: 3000,
        pause:   true
    });

    }

    $('#main_menu a').click(function() {
        $('#main_menu a').removeClass('selected');
        $(this).addClass('selected');
    })

    $('#homepage_slider .navigation a').bind('click',function() {
       if ($('div.slide:animated').length > 0) return false;
       var links = $(this).parents("ul").eq(0).find("a");
       var lx = links.index(this);
       $('.slideshow').cycle(lx, (activeslide > lx) ? 'scrollRight' : 'scrollLeft');
       return false;
    });

    $('.grad').css('position','relative').prepend('<span class="ol"></span>');

    $('#page_footer textarea').focus(function() {
       $(this).animate({'height': '300px'}, 600);
    }).blur(function() {
        if ($(this).val()=='') $(this).animate({'height': '60px'}, 1000);
    });

  //  $('.product_list').accordion({autoHeight: false, event: 'mouseover'});

  $('input.cal').each(function(i,el) {
     try{
       //sometimes this causes the "unexpected literal at position nn" as described at http://docs.jquery.com/UI/Datepicker/parseDate if the format is not yy-mm-dd, hence the try catch
       var ndate = $.datepicker.formatDate('dd M yy', $.datepicker.parseDate('yy-mm-dd', $(el).val(),0,10));
       $(el).attr('autocomplete','off').val(ndate).datepicker({dateFormat: 'dd M yy', altFormat: 'yy-mm-dd', numberOfMonths: 3}).after('<span class="cal">...</span>');
     } catch(err) {
       $(el).attr('autocomplete','off').datepicker({dateFormat: 'dd M yy', altFormat: 'yy-mm-dd', numberOfMonths: 3}).after('<span class="cal">...</span>');
     }
   });


});
