﻿var interval;
var clientsInterval;
var pauseClientsInterval = 0;
var lngInterval;


// starting the script on page load
$(document).ready(function () {

  if ($.browser.msie) {

    if ($.browser.version = '8.0') {
      // Workaround for IE for certain elements to get rounded corners
      $('#download').append('<div style="position:absolute; display:inline-block; top:0px; left:0px; background:url(/img/top-left.png); width:6px; height:6px;"></div>');
      $('#download').append('<div style="position:absolute; display:inline-block; top:0px; right:0px; background:url(/img/top-right.png); width:6px; height:6px;"></div>');
      $('#download').append('<div style="position:absolute; display:inline-block; bottom:0px; left:0px; background:url(/img/bottom-left.png); width:6px; height:6px;"></div>');
      $('#download').append('<div style="position:absolute; display:inline-block; bottom:0px; right:0px; background:url(/img/bottom-right.png); width:6px; height:6px;"></div>');

      $('#download .hilite').append('<div style="position:absolute; display:inline-block; top:0px; left:0px; background:url(/img/top-left-c.png); width:6px; height:6px;"></div>');
      $('#download .hilite').append('<div style="position:absolute; display:inline-block; top:0px; right:0px; background:url(/img/top-right-c.png); width:6px; height:6px;"></div>');
      $('#download .hilite').append('<div style="position:absolute; display:inline-block; bottom:0px; left:0px; background:url(/img/bottom-left-c.png); width:6px; height:6px;"></div>');
      $('#download .hilite').append('<div style="position:absolute; display:inline-block; bottom:0px; right:0px; background:url(/img/bottom-right-c.png); width:6px; height:6px;"></div>');

      $('#banner div').append('<div style="position:absolute; display:inline-block; top:309px; left:0px; background:url(/img/bottom-left.png); width:6px; height:6px;"></div>');
      $('#banner div').append('<div style="position:absolute; display:inline-block; top:309px; left:894px; background:url(/img/bottom-right.png); width:6px; height:6px;"></div>');

      $('#clients>div>div').append('<div style="position:absolute; display:inline-block; top:0px; left:0px; background:url(/img/top-left.png); width:6px; height:6px;"></div>');
      $('#clients>div>div').append('<div style="position:absolute; display:inline-block; top:0px; right:0px; background:url(/img/top-right.png); width:6px; height:6px;"></div>');
      $('#clients>div>div').append('<div style="position:absolute; display:inline-block; bottom:0px; left:0px; background:url(/img/bottom-left.png); width:6px; height:6px;"></div>');
      $('#clients>div>div').append('<div style="position:absolute; display:inline-block; bottom:0px; right:0px; background:url(/img/bottom-right.png); width:6px; height:6px;"></div>');

      $('#content span.explain').append('<div style="position:absolute; display:inline-block; top:0px; left:0px; background:url(/img/top-left.png); width:6px; height:6px;"></div>');
      $('#content span.explain').append('<div style="position:absolute; display:inline-block; top:0px; right:0px; background:url(/img/top-right.png); width:6px; height:6px;"></div>');
      $('#content span.explain').append('<div style="position:absolute; display:inline-block; bottom:0px; left:0px; background:url(/img/bottom-left.png); width:6px; height:6px;"></div>');
      $('#content span.explain').append('<div style="position:absolute; display:inline-block; bottom:0px; right:0px; background:url(/img/bottom-right.png); width:6px; height:6px;"></div>');

      $('#showclients>ul>li>a').append('<div style="position:absolute; display:inline-block; top:0px; left:0px; background:url(/img/top-left-c.png); width:6px; height:6px;"></div>');
      $('#showclients>ul>li>a').append('<div style="position:absolute; display:inline-block; top:0px; right:0px; background:url(/img/top-right-c.png); width:6px; height:6px;"></div>');
      $('#showclients>ul>li>a').append('<div style="position:absolute; display:inline-block; bottom:0px; left:0px; background:url(/img/bottom-left-c.png); width:6px; height:6px;"></div>');
      $('#showclients>ul>li>a').append('<div style="position:absolute; display:inline-block; bottom:0px; right:0px; background:url(/img/bottom-right-c.png); width:6px; height:6px;"></div>');

      $('#subbanners li').append('<div style="position:absolute; display:inline-block; top:0px; left:0px; background:url(/img/top-left.png); width:6px; height:6px;"></div>');
      $('#subbanners li').append('<div style="position:absolute; display:inline-block; top:0px; right:0px; background:url(/img/top-right.png); width:6px; height:6px;"></div>');
      $('#subbanners li').append('<div style="position:absolute; display:inline-block; bottom:0px; left:0px; background:url(/img/bottom-left.png); width:6px; height:6px;"></div>');
      $('#subbanners li').append('<div style="position:absolute; display:inline-block; bottom:0px; right:0px; background:url(/img/bottom-right-c.png); width:6px; height:6px;"></div>');

      $('#banners li>div').append('<div style="position:absolute; display:inline-block; top:0px; left:0px; background:url(/img/top-left.png); width:6px; height:6px;"></div>');
      $('#banners li>div').append('<div style="position:absolute; display:inline-block; top:0px; right:0px; background:url(/img/top-right.png); width:6px; height:6px;"></div>');
      $('#banners li>div').append('<div style="position:absolute; display:inline-block; bottom:0px; left:0px; background:url(/img/bottom-left.png); width:6px; height:6px;"></div>');
      $('#banners li>div').append('<div style="position:absolute; display:inline-block; bottom:0px; right:0px; background:url(/img/bottom-right.png); width:6px; height:6px;"></div>');
    }
  }

  interval = setInterval("slideShow()", 7000);

  $("a.showimage").fancybox({
    'titlePosition': 'inside'
  });

  $("a.showmovie").fancybox({
    'titlePosition': 'inside'
  });

  $('#banners div').click(function () {
    $(this).parent().find('a').click();
  });

  $('#bannernav a').click(function () {
    showSlide($(this).attr('id'));
    $(this).blur();
  });

  $('#bannernav a').focus(function () {
    $(this).blur();
  });

  $('.inputfield').focus(function () {
    $(this).addClass('infocus');
  });

  $('.inputfield').blur(function () {
    $(this).removeClass('infocus');
  });

  $('#download .hilite').click(function () {
    if (jQuery.support.opacity) {
      $('#download>div').fadeIn('slow');
    } else {
      $('#download>div').show();
    }
  });
  $('#canceldownload').click(function () {
    if (jQuery.support.opacity) {
      $('#download>div').fadeOut('slow');
    } else {
      $('#download h3+p+div').hide();
    }
  });

  $('#dictionary a.help').click(function () {
        if ($('#content span.explain').hasClass('on')) {
          $(this).text($('#dictionaryon').val());
        } else {
          $(this).text($('#dictionaryoff').val());
        }
        $('#content span.explain').toggleClass('on');
  });

  $('#content span.on').live('mousemove mouseout', function (event) {
    if (event.type == 'mousemove') {
      $("#tooltip").css('left', event.pageX + 10);
      $("#tooltip").css('top', event.pageY - 70);
      $("#tooltip").text($(this).attr('alt'));
      $('#tooltip').show();
    } else {
      $("#tooltip").text('');
      $('#tooltip').hide();
    }
  });

  // voting
  $('a.voting').click(function () {
    var selectedItem = $(this).parent().find(':radio:checked');

    if (selectedItem.length > 0) {
      vote($(this).parent(), $(this).attr('id'), selectedItem.val());
    }
  });

  // rating
  $('#ratethis a').hover(function () {
    $(this).prevAll('a').addClass('hilite');
  }, function () {
    $(this).prevAll('a').removeClass('hilite');
  });

});

function slideShow() {

  var $active = $('#banner div.selected');
  var $next = $active.next().length ? $active.next() : $('#banner div:first');

  $active.css({ opacity: 1.0 })
        .animate({ opacity: 0.0 }, 1000, function () {
          $active.removeClass('selected');
        });

  $next.css({ opacity: 0.0 })
        .addClass('selected')
        .animate({ opacity: 1.0 }, 1000, function () {
        });

  var $activecount = $('#bannernav li.selected');
  var $nextcount = $activecount.next().length ? $activecount.next() : $('#bannernav li:first');

  $activecount.removeClass('selected');
  $nextcount.addClass('selected');
  if (jQuery.support.opacity) {
    $nextcount.hide();
    $nextcount.fadeIn('medium');
  }

}

function showSlide(index) {
  clearInterval(interval);

  var $active = $('#banner div.selected');
  var $next = $('#banner>div').eq(index);

  if (!$next.hasClass('selected')) {

    $active.css({ opacity: 1.0 })
        .animate({ opacity: 0.0 }, 1000, function () {
        $active.removeClass('selected');
      });

    $next.css({ opacity: 0.0 })
        $next.addClass('selected')
        $next.animate({ opacity: 1.0 }, 1000, function () {
                
    });


    var $activecount = $('#bannernav li.selected');
    var $nextcount = $('#bannernav li').eq(index);

    $activecount.removeClass('selected');
    $nextcount.addClass('selected');

    if (jQuery.support.opacity) {
      $nextcount.hide();
      $nextcount.fadeIn('medium');
    }
  }
}

function startClients() {
  var bolPlay = false;
  lngInterval = setInterval("showClients('next')", clientsInterval);

  $('#forward').click(function () {
    $('#pause').removeClass('play');
    clearInterval(lngInterval);
    showClients('next');
    lngInterval = setInterval("showClients('next')", clientsInterval);
  });

  $('#backwards').click(function () {
    $('#pause').removeClass('play');
    clearInterval(lngInterval);
    showClients('previous');
    lngInterval = setInterval("showClients('next')", clientsInterval);
  });

  $("#pause").toggle(function () {
    clearInterval(lngInterval);
    $(this).addClass('play');
  }, function () {
    clearInterval(lngInterval);
    lngInterval = setInterval("showClients('next')", clientsInterval);
    $(this).removeClass('play');
  });
}

function showClients(strAction) {

  var $active = $('#clients>div.selected');

  if (strAction == 'next') {
    var $next = $active.next('div').length ? $active.next('div') : $('#clients>div:first');
  } else if (strAction == 'previous') {
    if ($active.prev('div').length != 0) {
      var $next = $active.prev('div');
    } else {
      var $next = $('#clients>div:last');
    }
  }
 
 if (jQuery.support.opacity) {
    $active.fadeOut('slow', function () {
      $active.removeClass('selected');
    });
    $next.fadeIn('slow', function () {
      $next.addClass('selected')
    });
  } else {
      $active.removeClass('selected');
      $active.hide();

    $next.addClass('selected')
    $next.show();
  }
  
}

function filterClients(tags, languageid, parentid, phrase) {

  clearInterval(lngInterval);
  var root = $('#clients');
  $.each(root.find('div'), function (key, value) {
    $(value).remove();
  });

  $.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    url: 'loyaltygroup/api/clients/apifilter?tags=' + encodeURIComponent(tags) + '&languageid=' + languageid + '&parentid=' + parentid,
    dataType: 'json',
    cache: false,
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
    },
    success: function (data, textStatus) {
      if (data.Status == 'error') {
        alert(data.Message);
      } else if (data.Items.length > 0) {

        $('#clients').show();
        $('#noclients').hide();

        $.each(data.Items, function (i, item) {

          var container = $('<div />');
          var itemContainer = $('<div />');

          // item
          if (i == 0) {
            container.addClass('selected');
          }
          container.css('background-image', 'url(' + item.Logo + ')');

          itemContainer.append('<h2>' + item.Title + '</h2>');
          itemContainer.append(item.Text);

          if (item.Link != '') {
            itemContainer.append('<a class="readmore" href="' + item.Link + '">' + phrase + '</a>');
          }
          container.append(itemContainer);
          root.append(container);
        });

        if (data.Items.length > 1) {
          startClients();
        }
      } else {
        $('#clients').hide();
        $('#noclients').show();
      }
    }
  });
}

function vote(container, pollID, voteIndex) {

  $.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    url: 'loyaltygroup/api/poll/vote?pollID=' + pollID + '&votedIndex=' + voteIndex,
    dataType: 'json',
    cache: false,
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      alert(textStatus);
    },
    success: function (data, textStatus) {
      if (data.Status == 'error') {
        alert(data.Message);
      } else if (data.length > 0) {
        container.empty();

        $.each(data, function (i, item) {
          if (item.Text != '') {
            container.append($('<span class="voteresult"><span style="width:' + item.Width + 'px">&nbsp;</span>' + item.Text + ' (' + item.Percent + ')</span>'));
          }
        });
      }
    }
  });
}

