﻿var selectedIndex = -1;
var expireTimer;
var carouselItems = 6;
var speed = 10000;
var next = 0;
var itvl = null;

$(document).ready(function() {

    var suggest = $('#suggestiveSearchResults');
    var searchTimer = null;
    var searchPlc = $('#searchplc').offset();
    var hdnDownloadStoreSiteValue = $('#ctl00_hdnDownloadStoreSite').val();

    function getSearchResults() {
        if ($('.txtSearch').val() != "") {
            var searchTerm = encodeURIComponent($('.txtSearch').val()); // SPECIAL CHARACTER ENCODING [ ,/?:@&=+$# ]
            $.get("/Controls/SearchSuggestions.aspx?searchTerm=" + searchTerm, function(data) {
                suggest.show().css({ width: '650px', top: searchPlc.top, left: searchPlc.left - 61, position: 'absolute' });
                suggest.html(data);
            });
        }
    }

    $('#suggestiveSearchResults div a').click(function() {
        var a = $(this);
        window.location = hdnDownloadStoreSiteValue + "Search.aspx?searchTerm=" + a.html().replace("&amp;", "&");
        alert("a");

    });

    $('.txtSearch').attr('autocomplete', 'off').keyup(function(ev) {
        if (ev.which != 40 && ev.which != 38) {
            if ($(this).val() != "") {
                if (searchTimer != null) {
                    clearTimeout(searchTimer);
                }
                searchTimer = setTimeout(getSearchResults, 500);
            } else {
                $('#suggestiveSearchResults').fadeOut('fast');
                selectedIndex = -1;
            }
        }

    });

    $('.txtSearch').keyup(function(e) {


        if (e.which == 40 || e.which == 38) {

            var itemsInSearch = $("#suggestiveSearchResults div").length - 1;

            if (itemsInSearch >= 0) {
                if (e.which == 38) {
                    selectedIndex = selectedIndex - 1;

                    if (itemsInSearch >= 0 && selectedIndex < 0) {
                        selectedIndex = itemsInSearch;
                    } else {
                        if (selectedIndex > 0) {

                        } else {
                            selectedIndex = 0;
                        }
                    }
                }

                if (e.which == 40) {

                    selectedIndex = selectedIndex + 1;

                    if (selectedIndex > itemsInSearch) {
                        selectedIndex = 0;
                    }
                }

                $("#suggestiveSearchResults div").each(function(i) {
                    $(this).removeClass('keySelected');
                });

                $("#suggestiveSearchResults div:eq(" + selectedIndex + ")").each(function(i) {
                    $(this).addClass('keySelected');
                });
            }
        }
    });

    $("#suggestiveSearchResults div a").hover(
      function() {
          alert("dd");
          $(this).html("asdfasdf");
      },
      function() {
          $(this).find("span:last").remove();
      }
    );



      $('.txtSearch').keyup(function(e) {
          if (e.which == 13) {
              var obj = $('#suggestiveSearchResults .keySelected a');
              //alert("d");
              //alert(obj.attr('href'));
              if (obj.attr('href') != "undefined" && obj.attr('href') != null) {
                  window.location = obj.attr('href');
              } else {
                  if ($("#suggestiveSearchResults div:eq(" + selectedIndex + ") a") == null) {
                      //alert("d");
                      //alert($(this));
                      window.location = hdnDownloadStoreSiteValue + "Search.aspx?searchTerm=" + $(".txtSearch").val();
                  } else {
                      $("#suggestiveSearchResults div:eq(" + selectedIndex + ") a").html()
                  }
              }

          }
      });

    $('.txtSearch').blur(function() {
        $('#suggestiveSearchResults').fadeOut('fast');
        selectedIndex = -1;
    });

    $('.variantExpanation').click(function() {
        return false;
    });

    $('.variantExplanation').hover(function(ev) {
        var left = ($(this).offset().left - 125);
        $('.variantExplanationText').show().css({ width: '250px', top: $(this).offset().top + 20, left: left, position: 'absolute' });
    }, function() {
        $('.variantExplanationText').fadeOut('fast');

    });

    $('.checkoutButton').hover(function() {

    });

    $('.pricing .changeVariant').click(function() {
        $(this).parent().parent().parent().children('.type').fadeIn("fast");
        return false;
    });

    $('.type .cancel').click(function() {
        $(this).parent().parent().parent().fadeOut("fast");
        return false;
    });

    // carousel bindings
    $('#carouselIndex ul li').mouseenter(
        function(){
            carouselStop();
            highlight(this);
        }
    );
    $('#carouselIndex ul li').mouseleave(
        function(){
            carouselAnimate();
        }
    );
    $('#carousel ul li').mouseenter(
        function(){
            carouselStop();
        }
    );
    $('#carousel ul li').mouseleave(
        function(){
            carouselAnimate();
        }
    );
    carouselAnimate();
    
    // albums and singles charts
    $('#charts_download_albums_tab').click(function(){
        $('#charts_download_albums_tab').addClass('active');
        $('#charts_download_tab').removeClass('active');
        $('#charts_download_albums').show();
        $('#charts_download').hide();
    });
    
    $('#charts_download_tab').click(function(){
        $('#charts_download_tab').addClass('active');
        $('#charts_download_albums_tab').removeClass('active');
        $('#charts_download_albums').hide();
        $('#charts_download').show();
    });
    
    $('#charts_download').hide();
});

function searchMouseOver() {
    $(document).ready(function() {
        $("#suggestiveSearchResults div a").hover(
            function() {
                $("#suggestiveSearchResults div").each(function(i) {
                    $(this).removeClass('keySelected');
                });

                $(this).parent().addClass('keySelected');

                var index = $("#suggestiveSearchResults div a").index(this);
                selectedIndex = index;
            },
            function() { }
        );
    });
}

/*
    Homepage Carousel functions
==================================
*/
function carouselAnimate()
{
    carouselStop();
    itvl = window.setInterval(function(){ carousel(next); }, speed);
}

function carouselStop()
{
    window.clearInterval(itvl); 
}

function carousel(upto)
{
    next = next + 1;
    
    if(next >= carouselItems){
        next = 0;
    }
    if($('#li' + next).length)
    {
        $('#li' + next).siblings().hide();
        $('#li' + next).show();
        $('#carouselindex' + next).addClass('currentindex');
        $('#carouselindex' + next).removeClass('normalindex');
        $('#carouselindex' + next).siblings().removeClass('currentindex');
        $('#carouselindex' + next).siblings().addClass('normalindex');
    }
    else
    {
        next = 0;
        carouselAnimate();
    }
}

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

function highlight(selected)
{
    var selectedId = selected.id;
    selectedId = selectedId.replace('carouselindex', '');
    
    if(isNumber(selectedId))
    {
        var selectedIndex = parseInt(selectedId);
        next = selectedIndex;
        if($('#li' + next).length)
        {
            $('#li' + next).siblings().hide();
            $('#li' + next).show();
            $('#carouselindex' + next).addClass('currentindex');
            $('#carouselindex' + next).removeClass('normalindex');
            $('#carouselindex' + next).siblings().removeClass('currentindex');
            $('#carouselindex' + next).siblings().addClass('normalindex');
        }
        else    // (hopefully) overzealous error handling code.
        {
            next = 0;
            carouselAnimate();
        }
    }
    else
    {
        next = 0;
        carouselAnimate();
    }
}

