﻿var timer;
var numberOfSlides;

$(document).ready(function() {

    numberOfSlides = $('.gallery-holder .gallery .slidecontainer').length;

    prepareCarousels();
    prepareSearchForm();
    prepareCommentVisibility();
    prepareNeedToLogin();
    prepareSendToFriendVisibility();
    prepareHomePageFeatureImages();
    prepareExpandableBoxes();

    // lightbox initialisation
    $('a[rel*="lightbox"]').lightBox();
        
    // SLIDESHOW
    if ($('.gallery-holder .gallery').length) {
        skipToNextSlide(1);
    }
    
    $('div#postCommentForm').hide();
    $('div#send2friendsForm').hide();
    
    /* if the kit finder panel is open then unbind the click handler and close it */
    $('.frame .categorys-list .active .item a').click(function(){
        $(this).unbind().parent().parent().removeClass('active');
        return false;
        });

    $('.popup .row a').click(function() {
        $(this).parent().parent().parent().parent().parent().removeClass('active');
        return false;
    });

});

var activeSlide = 0;

function prepareHomePageFeatureImages()
{
    //$(".gallery .caption:gt(0)").hide();
    $(".gallery .caption .pagination li a").click(
        function(){
            var slideToSkipTo = $(this).find("span").html();
            skipTo(slideToSkipTo);
        }
    );
}
function skipTo(slide)
{
    if (slide > numberOfSlides) slide = 1;

    activeSlide = slide;
    var slideWidth = 638;
    var cnt = -(slideWidth*(slide-1));
    $('div.gallery').animate({ marginLeft: cnt+"px" },750);
    activeSlide = slide;
    
    /*reset the timer - otherwise you may skip by click then skip by timer */
    clearTimeout(timer);
    timer = setTimeout('skipToNextSlide()', 10000);
}

function skipToNextSlide() {
    clearTimeout(timer);
    timer = setTimeout('skipToNextSlide()', 10000);
       
    activeSlide ++;
    if (activeSlide > numberOfSlides) {activeSlide = 1}

    skipTo(activeSlide);
}

function prepareNeedToLogin()
{
  if ( $('div.divNeedToLogin').length )
  {
    $('div.divNeedToLogin').hide();
    
    $('div.rating-holder strong.stars0').bind(
      'mouseover',function(){
        $('div.divNeedToLogin').show();
      }
    ).bind(
      'mouseout',function(){
        $('div.divNeedToLogin').hide();
      }
    );
    
    $('div.divNeedToLogin').bind(
      'mouseover',function(){
        $('div.divNeedToLogin').show();
      }
    ).bind(
      'mouseout',function(){
        $('div.divNeedToLogin').hide();
      }
    );
  }
}

function prepareExpandableBoxes()
{
  $('div#emailBestDealsForm').hide();
  
  $('a#emailBestDeals').click(function()
  {
    $('div#emailBestDealsForm').slideToggle();
    return false;
  });
}

function prepareSendToFriendVisibility()
{
    $("ul.menu li.send a").click( function(){
            if ( $(this).hasClass('active') )
            {
                $(this).removeClass('active');
                $('div#send2friendsForm').slideUp();
            }
            else
            {
                $(this).addClass('active');
                $('div#postCommentForm').hide();
                $('div#send2friendsForm').slideDown();
            }  
        return false;
    });
}

function prepareCommentVisibility()
{
    $(".add-comment a").click( function(){
         if ( $(this).hasClass('active') )
            {
              $(this).removeClass('active');
              $('.list-comments').slideUp();
            }
            else
            {
              $(this).addClass('active');
              $('.list-comments').slideDown();
            }         
          return false;   
    });
}


function prepareSearchForm() {
    $('#ctl00_Searchform1_txtSearchKeywords').click(function() {
        if (this.value == this.defaultValue) this.value = '';
    });

    $('#ctl00_Searchform1_btnSubmitSearchForm').click(function() {
        if ($('#ctl00_Searchform1_txtSearchKeywords').val().trim() == '') {
            BlockAlert('Enter your search term');
            $('#ctl00_Searchform1_txtSearchKeywords').focus();
            return false;
        }
        else {
            redirectToSearch();
            return false;
        }
    });

    $("#ctl00_Searchform1_txtSearchKeywords").keypress(
    function(e) {
      if (e.which == 13) {
          redirectToSearch();
          return false;
      }
  }
  );
}

function redirectToSearch() {
    document.location.href = '/site-search-results?q=' + $('#ctl00_Searchform1_txtSearchKeywords').val();
}

function prepareCarousels()
{
  if ( $('#imageCarousel').length )
  {
    // Make sure there are more than 4 images...
    if ( $('#imageCarousel li').length > 5 )
    {
      $('#imageCarousel').jcarousel({ scroll:5,visible:5 });
    }
    else
    {
      $('#imageCarousel').replaceWith('<div class="carouselContainer"><ul id="imageCarousel">'+$('#imageCarousel').html()+'</ul></div>');
    }
    
    $('#imageCarousel a').click(function(){
      var src  = $(this).attr('href');;
      $('img#mainImage').attr('src',src);
      return false;
    });
  }
}

function BlockAlert(aMessage) {
    $.blockUI({
        message: '<h1>' + aMessage + '</h1>',
        css:
    {
        border: 'none',
        padding: '15px',
        backgroundColor: '#000',
        '-webkit-border-radius': '10px',
        '-moz-border-radius': '10px',
        opacity: '.5',
        color: '#fff'
    }
    });

    setTimeout($.unblockUI, 2000);
}

/*
function hideFormOptions(value) {
    // Toogle visibility of search form option panel
    // dependant on search for Compact Cameras under £250
    if (value == '0,250') {
        $("#cscSearch_CompactForm_featureslist").css("display", "none");
        $(".CameraFinderForm .optional").css("display", "none");
        $(".compactsearchmessage").css("display", "block");
    }
    else {
        $("#cscSearch_CompactForm_featureslist").css("display", "block");
        $(".CameraFinderForm .optional").css("display", "block");
        $(".compactsearchmessage").css("display", "none");
    }
}
*/

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}

function rate(rating) {
    jQuery.ajax(
    {
        url: '/assets/handlers/rate.ashx?rating=' + rating + '&type=' + ratingType + '&idToRate=' + idToRate,
        type: 'GET',
        dataType: 'text',
        cache: true,
        async: true,
        timeout: 120000,
        error: function() {
            alert('Error rating, please try again later');
        },
        success: function(somehtml) {
            if (somehtml == 'OK') {
                //set all rating displays to the rated amount
                //$(".rating:eq(0)").replaceWith('<span class="stars' + rating + '">' + rating + ' Stars</span>');
                //$(".rating:eq(0)").replaceWith('<strong class="stars' + rating + '">' + rating + ' Stars</strong>');

                $(".star-rating").replaceWith('<strong class="stars' + rating + '">' + rating + ' Stars</strong>');
            }
        }
    });
}
