var containerArray = [

    'container-lunchbox',
    'container-cooler',
    'container-diaperbag',
    'container-charlie',
    'container-coolerbag-2',
    'container-girls',
    'container-lunchbox-2',
    'container-reindeer',
    'container-stroller'
];

var chooser = parseInt((Math.random()*containerArray.length));

$(function(){

    var maxOffset;

   $('#container').removeClass('container-lunchbox').addClass(containerArray[chooser]);

   $('.navbutton').click(
                        function () {

                                location.href = $(this).attr("id") +".php";
                        }) ;

    $(".nav-button").hover(
                        function () {

                                $(this).stop().animate({opacity: 1}, 500);
                        },
                        function () {

                                $(this).stop().animate({opacity: 0.5}, 500);
                        });

    $(".backToCart").hover(
                        function () {

                                $(this).stop().animate({opacity: 1}, 500);
                        },
                        function () {

                                $(this).stop().animate({opacity: 0.5}, 500);
                        });

    $(".shopMore").hover(
                        function () {

                                $(this).stop().animate({opacity: 1}, 500);
                        },
                        function () {

                                $(this).stop().animate({opacity: 0.5}, 500);
                        });


    $(".shopping-cart").hover(
                        function () {

                                $(this).stop().animate({opacity: 1}, 500);
                        },
                        function () {

                                $(this).stop().animate({opacity: 0.5}, 500);
                        });


    $(".header-button").hover(
                        function () {

                                $(this).stop().animate({opacity: 1}, 500);
                        },
                        function () {

                                $(this).stop().animate({opacity: 0.5}, 500);
                        });

    $(".link-button").hover(
                        function () {

                                $(this).stop().animate({opacity: 1}, 500);
                        },
                        function () {

                                $(this).stop().animate({opacity: 0.5}, 500);
                        });

   $(".header-button").click(
                        function () {

                                $("#"+ $(this).attr("id")+"-content").slideToggle('Medium');
                        });

   $(".sub-header-button").click(
                        function () {

                                $("#"+ $(this).attr("id")+"-content").slideToggle('Medium');

                        });

   $(".link-button").not(".link-button#brochure").click (
                        function() {

                           $('.main-content').load($(this).attr("title")+".php");
                        });

   $(".nav-button").click (
                        function () {

                              location.href = $(this).attr("id") +".php"
                        });

   $(".nav-button#shop, div.sidebar div#products-content a").click (
                        function () {

                              $.cookie('lastPage', '');
                        });


   $('.shopping-cart').click (
                        function () {

                              location.href = 'cart.php'
                        });

   $('.backToCart').click (
                        function () {

                              location.href = 'cart.php'
                        });

   $('.shopMore').click (
                        function () {

                              location.href = 'shop.php'
                        });

   $("#logo").click (
                        function () {

                              location.href = "index.php"
                        });

   $(".sidebar-link").click (
                        function() {

                           $('.main-content').load($(this).attr("id")+".php",
                           {},
                           function() { maxOffset = $(this).find("div.product-views").length - 4; });
                        });

   $('.product-views a, a.read-more').livequery(
                        function () {
                            $('.product-views a').fancybox({
                                'frameWidth': 600,
                                'frameHeight': 590,
                                hideOnContentClick: false,
                                callbackOnShow:
                                    function () {

                                    $('input').click(function(){
                                           var productType = $('div.popup-content').attr("id");
                                           $.cookie('lastPage', productType);
                                           });

                                    $('div.tooltip').css('display', 'none');

                                    $('div.popup-thumb').click(function(){
                                        var clickedId = $(this).attr('id');
                                        $('div.popup-picture-window').css('background-image', 'url(images/popups/large/'+clickedId+'.jpg)')
                                    });

                                    var productType = $('.popup-content').attr('id');

                                    if (productType == 'lunchbox') {
                                        $.getJSON("product-features.json",
                                        function(data){
                                          for (var i = 0; i < data.lunchbox.features.length; i++) {
                                          $('.popup-product-highlights').append('<li>'+data.lunchbox.features[i]+'</li>');
                                          }
                                          });
                                    }
                                    else if (productType == 'coolerbag') {
                                        $.getJSON("product-features.json",
                                        function(data){
                                          for (var i = 0; i < data.coolerbag.features.length; i++) {
                                          $('.popup-product-highlights').append('<li>'+data.coolerbag.features[i]+'</li>');
                                          }
                                          });
                                    }
                                    else if (productType == 'emily') {
                                        $.getJSON("product-features.json",
                                        function(data){
                                          for (var i = 0; i < data.emily.features.length; i++) {
                                          $('.popup-product-highlights').append('<li>'+data.emily.features[i]+'</li>');
                                          }
                                          });
                                    }
                                    else if (productType == 'charlie') {
                                        $.getJSON("product-features.json",
                                        function(data){
                                          for (var i = 0; i < data.charlie.features.length; i++) {
                                          $('.popup-product-highlights').append('<li>'+data.charlie.features[i]+'</li>');
                                          }
                                          });
                                    }

                                    }
                            });
 
                        });

   $('.press-thumbnail').livequery(
                        function () {
                            $('a.article').fancybox({'hideOnContentClick': false});
                        });


   $('.product-features').livequery (

                        function () {
                            
                            var productType = $('.product-header').attr('id');


                            if (productType == 'lunchbox') {
                                $.getJSON("product-features.json",
                                function(data){
                                  for (var i = 0; i < 4; i++) {
                                  $('.product-features').append('<li>'+data.lunchbox.features[i]+'</li>');
                                  }
                                  if (data.lunchbox.features.length > 4) {
                                    $('.product-features').append('<a class="read-more" href="#">Read More...</a>')
                                  }
                                  for (var j = 4; j < data.lunchbox.features.length; j++) {
                                      $('.tooltip-content').append('<li>'+data.lunchbox.features[j]+'</li>');
                                  }
                                  });
                            }
                            else if (productType == 'coolerbag') {
                                $.getJSON("product-features.json",
                                function(data){
                                  for (var i = 0; i < 4; i++) {
                                  $('.product-features').append('<li>'+data.coolerbag.features[i]+'</li>');
                                  }
                                  if (data.coolerbag.features.length > 4) {
                                    $('.product-features').append('<a class="read-more" href="#">Read More...</a>')
                                  }
                                  for (var j = 4; j < data.coolerbag.features.length; j++) {
                                      $('.tooltip-content').append('<li>'+data.coolerbag.features[j]+'</li>');
                                  }
                                  });
                            }
                            else if (productType == 'emily') {
                                $.getJSON("product-features.json",
                                function(data){
                                  for (var i = 0; i < 5; i++) {
                                  $('.product-features').append('<li>'+data.emily.features[i]+'</li>');
                                  }
                                  if (data.emily.features.length > 5) {
                                    $('.product-features').append('<a class="read-more" href="#">Read More...</a>')
                                  }
                                  for (var j = 5; j < data.emily.features.length; j++) {
                                      $('.tooltip-content').append('<li>'+data.emily.features[j]+'</li>');
                                  }
                                  });
                            }
                            else if (productType == 'charlie') {
                                $.getJSON("product-features.json",
                                function(data){
                                  
                                  for (var i = 0; i < 9; i++) {
                                  $('.product-features').append('<li>'+data.charlie.features[i]+'</li>');
                                  }
                                  if (data.charlie.features.length > 9) {
                                    $('.product-features').append('<a class="read-more" href="#">Read More...</a>');
                                    for (var j = 9; j < data.charlie.features.length; j++) {
                                      $('.tooltip-content').append('<li>'+data.charlie.features[j]+'</li>');
                                  }
                                  }
                                  
                                  });
                            }
    });

    $('a.read-more').livequery(
    function () {
        
        $(this).click(
            function (e) {
                    $('div.tooltip').css({
                    
                    "top": e.pageY - $('div.tooltip').height(),
                    "left": e.pageX - 345});
                    $('div.tooltip').fadeIn('slow');
            });

    $('div.tooltip').click(function() {
            $('div.tooltip').css('display', 'none')});
    });

    $('.sidebar-link').livequery(
    function() {
        $(this).click(function() {
            $('ul.tooltip-content li').remove();
            $('div.tooltip').css('display', 'none')
        });
    });

    $('.nav-button').livequery(
    function() {
        $(this).click(function() {
            $('ul.tooltip-content li').remove();
            $('div.tooltip').css('display', 'none')});
    }
    );

var offset = 0;

var slideInProgress = false;

   $('.product-view-scroll-right').livequery('click', function() {

                        if (offset == maxOffset || slideInProgress == true) return false;

                        slideInProgress = true;

                        var marginString = $('.product-view-container').css('marginLeft');
                        var subStringIndex = marginString.indexOf('px');
                        var marginValue = parseInt(marginString.slice(0,subStringIndex));
                        $('.product-view-container').stop().animate({ margin: marginValue-153 }, 750,
                            "swing",
                            function ()
                            {
                                slideInProgress = false;
//                                alert('slideInProgress = ' + slideInProgress);
                            });
//                        alert(offset);
//                        alert(maxOffset);
                        offset++;

                       });


   $('.product-view-scroll-left').livequery('click', function() {

                        if (offset == 0 || slideInProgress == true) return false;

                        slideInProgress = true;

                        var marginString = $('.product-view-container').css('marginLeft');
                        var subStringIndex = marginString.indexOf('px');
                        var marginValue = parseInt(marginString.slice(0,subStringIndex));

                        $('.product-view-container').stop().animate({ margin: marginValue+153 }, 750,
                            "swing",
                            function ()
                            {
                                slideInProgress = false;
//                                alert('slideInProgress = ' + slideInProgress);
                            });
//                        alert(offset)
//                        alert(maxOffset)
                        offset--;
                        });
});

