//<![CDATA[
$(document).ready(function () {

    // reset text in all input fields
    $('input[type=text], textarea').reSetValue();

    // lang overlay
    $('.overlay').hide();
    $('.lang-head').click(function () {
        toggleXtra($(this));
        return false;
    });

    // hide lang on click outside the element    
    $(document).click(function (event) {
        var t = $(event.target);
        if (!t.parent().hasClass('lang-head') && !t.parents().hasClass('overlay')) {
            $('.overlay').hide();
        }
    });

    // tip-friend modal function
    $('div.lightboxme').hide();
    $('.page-functions a.lightboxme').click(function () {
        var t = $(this).attr('rel');
        $('div.' + t).lightbox_me({
            centered: true,
            overlayCSS: { background: '#333', opacity: .5 },
            overlaySpeed: 300,
            lightboxSpeed: "normal"
        });
        return false;
    });

    $('a[rel=tipfriend]').click(function () {
        $('.lb_overlay, div.lightboxme').fadeOut(500);
        $('div.tipfriend').lightbox_me({
            centered: true,
            overlayCSS: { background: '#333', opacity: .5 },
            overlaySpeed: 300,
            lightboxSpeed: "normal"
        });
        return false;
    });

    $(".close").click(function () {
        $('.lb_overlay, div.lightboxme').stop();
    })

});

function toggleXtra(ref) {
  /* vars */
  var all = $('.overlay');
  var pre = all.prev();
  var el = ref.next();
  /* toggle class */
  pre.removeClass('active');
  $(ref).addClass('active');
  if (el.is(':visible')) {
    all.hide();
  } else if (el.is(':hidden')) {
    all.hide();
    el.show();
  }
  return false;
}

/* Slideshow running inside .load to get precaching of images
***********************************************************/
$(window).load(function () {

    // Content is hidden from start, precache here aswell
    $('#slideshow .slideshow-image').fadeIn();
    // Slideshow init
    var slideCount = 0;

    $('#slideshow .slideshow-wrapper').cycle({
        fx: 'fade', // Effect
        before: function (currSlideElement, nextSlideElement, options, forwardFlag) { // Fades backgroundimage out before next slide
            $('.slideshow-image').animate({ opacity: 0 });
        },
        after: function (currSlideElement, nextSlideElement, options, forwardFlag) { // Fades backgroundimage in for next slide
            var newBg = $(nextSlideElement).attr('data-style');
            $('.slideshow-image').css('opacity', '0').css('background-image', 'url(' + newBg + ')').animate({ opacity: 1 });
        },
        activePagerClass: 'activeSlideLink', // Sets active pager class
        pager: '#slideshow .pager', // Define id for pager to appear
        prev: '#slideshow .prev',
        next: '#slideshow .next',
        pagerAnchorBuilder: function (idx, slide) { // Builds the pager
            slideCount++;

            return '<li class="' + idx + '"><a href="#">&nbsp;</a></li>';
        },
        fit: 0,
        slideResize: 0,
        pause: 1, // Pause on hover
        pauseOnPagerHover: 1, // Pause on pagination hover
        timeout: 10000, // Sets timeout for slides
        speed: 1000 // Animation speed
    });
});


var agent = navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad|android)/);
if (agent) {
    $("#slideshow").touchwipe({
        wipeLeft: function () { $("#slideshow .slideshow-wrapper").cycle("next"); },
        wipeRight: function () { $("#slideshow .slideshow-wrapper").cycle("prev"); },
        min_move_x: 20,
        min_move_y: 20,
        preventDefaultEvents: true
    });
}



//]]>

