﻿/// <reference path="~/script/libraries/jquery.js" />

(function ($) {

    $.fn.starRating = function(parameters) {
        
        var items,
        actualRating = 0,
        displayRating = 0,
        $starElement = null;

        var options = {
            currentRating: 1,
            ratingChanged: function(rating) {alert(rating)}
        };
            
        function createRatingElement() {
            $starElement = $('<span></span>')
                .css('cursor', 'pointer')
                .addClass('stars s1')
                .appendTo(items);
                
            refreshDisplay();                
        }
        
        function refreshDisplay() {
            $starElement.attr('class', 'stars s' + displayRating);
        }
        
        function ratingMouseMove(event) {
            
            var offset = $(this).offset();
            var mouseOffset = event.clientX - offset.left;
            
            if (mouseOffset <= 26) {
                displayRating = 1;
            } else if (mouseOffset <= 47) {
                displayRating = 2;
            } else if (mouseOffset <= 68) {
                displayRating = 3;
            } else if (mouseOffset <= 89) {
                displayRating = 4;
            } else {
                displayRating = 5;
            }
            
            refreshDisplay();
        }
        
        function ratingMouseOut(event) {
            displayRating = actualRating;
            refreshDisplay();
        }
        
        function ratingClick(event) {
            actualRating = displayRating;
            options.ratingChanged(actualRating);            
        }
        
        items = this;            
        options = $.extend(options, parameters);
        actualRating = displayRating = options.currentRating;
        createRatingElement();
        
        $starElement.mousemove(ratingMouseMove);
        $starElement.mouseout(ratingMouseOut);
        $starElement.click(ratingClick);

        return this;
    }

    $.fn.enableScroll = function(parameters) {
    
        var currentPos = 0,
            itemCount,
            $container = this,
            $prev,
            $next;
        
        var options = {
            itemWidth: 93,
            itemHeight: 66,
            itemsPerPage: 6
        };
        
        function setStyles() {

            $('div', $container).css({
                width: options.itemWidth * options.itemsPerPage + 'px',
                overflow: 'hidden',
                position: 'relative'});
            
            $('ul', $container).css({
                position: 'absolute',
                top: 0,
                left: 0,
                width: '9999px'});
        }
        
        function addScrollButtons() {
            
            if (itemCount > options.itemsPerPage) {
                
                $prev = $('<p></p>')
                    .addClass('prev')
                    .append($('<a></a>').attr('href', '#'))
                    .css('opacity', 0)
                    .insertAfter($('h2', $container))
                    .click(previousClick);
                    
                $next = $('<p></p>')
                    .addClass('next')
                    .append($('<a></a>').attr('href', '#'))
                    .css('opacity', 0)
                    .appendTo($container)
                    .click(nextClick);                    
            }
        }
        
        function previousClick(event) {
            event.preventDefault();
            
            currentPos = currentPos - options.itemsPerPage;
            
            if (currentPos - options.itemsPerPage < 0) {
                currentPos = 0;
            } 
            
            scrollList();
        }
                
        function nextClick(event) {
            
            event.preventDefault();
            
            currentPos = currentPos + options.itemsPerPage - 1;
            
            if (currentPos + options.itemsPerPage > itemCount - 1) {
                currentPos = itemCount - options.itemsPerPage;
            }
            
            scrollList();
        }
        
        function scrollList() {
            var left = - currentPos * options.itemWidth
            $('ul', $container)
                .stop()
                .animate({left: left}, 1100, 'easeInOutCubic');
                
            animateScrollButtons();
        }
        
        function animateScrollButtons() {
            
            var transition = 'easeInSine',
                period = 500;
                
            if ($prev) {                
                if (currentPos == 0) {
                    $prev.animate({opacity: 0.3}, period, transition);
                } else {
                    $prev.animate({opacity: 1}, period, transition);
                }
            }
            
            if ($next) {
                if (currentPos + options.itemsPerPage > itemCount - 1) {
                    $next.animate({opacity: 0.3}, period, transition);
                } else {
                    $next.animate({opacity: 1}, period, transition);
                }
            }                
        }
        
        $.extend(options, parameters);
        itemCount = this.find('li').length;
        
        setStyles();
        addScrollButtons();
        animateScrollButtons();
            
        return this;
    };
    
  
    
            
       
    
    $.fn.lightbox = function(parameters) {
    
        var $list = this,
            $mask,
            $box,
            $img,
            $thumb,
            $caption;
        
        function initialize() {
            
            $mask = $('<div></div>')
                .attr('id', 'lightboxMask')
                .css({
                    opacity: 0,
                    cursor: 'pointer'})
                .hide()
                .appendTo($('body'))
                .click(dismissClick);
            
            $box = $('<div></div>')
                .attr('id', 'lightbox')
                .appendTo($('body'))
                .css('opacity', 0)
                .hide();
            
            $img = $('<img />')
                .attr({
                    alt: '',
                    src: ''})
                .appendTo($box);                    
            
            $caption = $('<p></p>')
                .addClass('caption')
                .appendTo($box);                
            
            $('<p></p>')
                .addClass('close')
                .click(dismissClick)
                .append($('<a></a>').attr('href', '#').text('Close'))
                .appendTo($box);

            $('<p></p>')
                .addClass('prev')
                .click(previousClick)
                .append($('<a></a>').attr('href', '#').text('Previous'))
                .appendTo($box);
                

            $('<p></p>')
                .addClass('next')
                .click(nextClick)
                .append($('<a></a>').attr('href', '#').text('Next'))
                .appendTo($box);
                                
            $('img', $list)
                .css('cursor', 'pointer')
                .click(thumbClick);
            
            $(window)
                .resize(positionElements)
                .scroll(positionElements);
        }
        
        function positionElements() {

            var left = ($(window).width() - 520) / 2,
                top = $(window).scrollTop() + 163;
                
            $box.css({
                left: left + 'px',
                top: top + 'px'})
                
            $mask.css('top', $(window).scrollTop() + 'px');
        }
        
        function thumbClick(event) {
            
            $thumb = $(this);
            positionElements();
            updateImage();
        }
        
        function previousClick(event) {
            event.preventDefault();
            tracker1._trackPageview(document.location.href + 'picture-view');
            var li = $thumb.parents('li').prev();
            
            if (li.length == 0) {
                var $items = $thumb.parents('ul').children('li');
                li = $items.eq($items.length - 1);
            }
            
            if (li.length > 0) {
                $thumb = li.find('img');
                updateImage();
            }
        }
        
        function nextClick(event) {
            event.preventDefault();
            tracker1._trackPageview(document.location.href + 'picture-view');
            var li = $thumb.parents('li').next();
            
            if (li.length == 0) {
                li = $thumb.parents('ul').children('li').eq(0);
            }
            
            if (li.length > 0) {
                $thumb = li.find('img');
                updateImage();
            }
        }
        
        function updateImage() {
        
            $mask
                .show()
                .animate({opacity: 0.8}, 200, 'easeInSine');                
                
            $box
                .show()
                .animate({opacity: 1}, 500, 'easeInCubic');        
            
            $img.attr('src', $thumb.parents('li').find('input').val());                
            
            $caption.text($thumb.attr('alt'));                            
        }
        
        function dismissClick(event) {
            event.preventDefault();
            
            $mask.hide().css('opacity', 0);
            $box.hide().css('opacity', 0);
        }
        
        initialize();
        
        return this;
    };
    
})(jQuery);

$(document).ready(function () {

    var current = $('#yourRating select').val();
    $('#rating input, #rating select').hide();

    var currentAverage = $('.hiddenAverage').val();
    $('div#red').css('height', currentAverage * 32);

    $('#yourRating').starRating({
        currentRating: current,
        ratingChanged: function (rating) {

            $.post('/Ajax/StorePageRating.ashx',
                {
                    pageId: $('#pageId').val(),
                    categoryId: $('#ratingCategory').val(),
                    score: rating
                },
                function (data) {

                    var average;
                    if (data.average == 0) {
                        average = 2.5;
                    }
                    else {
                        average = data.average;
                    }
                    $('#averageRatingText')
                        .removeClass()
                        .addClass('s' + average);

                    $('#voteCountText').text('(' + data.total + ' votes)');

                    $('#yourRating label')
                        .text('Your rating:')
                        .css('opacity', 0)
                        .animate({ opacity: 1 }, 400, 'easeInSine');

                    $('.hiddenAverage').attr('value', average);

                    $('div#red').css('height', average * 32);

                },
                'json');
        }
    });

    var currentTopRated = $('#TopRatedRating select').val();
    $('#ratedRating input, #ratedRating select').hide();

    $('#TopRatedRating').starRating({
        currentRating: currentTopRated,
        ratingChanged: function (rating) {

            $.post('/Ajax/StorePageRating.ashx',
                {
                    pageId: $('#topRatedPageId').val(),
                    categoryId: $('#topRatedRatingCategory').val(),
                    score: rating
                },
                function (data) { },
                'json');
        }
    });

    var currentTopPopular = $('#TopPopularRating select').val();
    $('#popularRating input, #popularRating select').hide();

    $('#TopPopularRating').starRating({
        currentRating: currentTopPopular,
        ratingChanged: function (rating) {

            $.post('/Ajax/StorePageRating.ashx',
                {
                    pageId: $('#topPopularPageId').val(),
                    categoryId: $('#topPopularRatingCategory').val(),
                    score: rating
                },
                function (data) { },
                'json');
        }
    });

    $('#gallery').enableScroll();
    var quotient = ($('#currentImageNumber').val() - 1) / 6;
    var navPageNumber;
    // In JavaScript, dividing integer values yields a floating point result (unlike in Java, C++, C)
    // To find the integer quotient, Convert quotient to an integer by truncating toward 0.
    if (quotient >= 0) {
        navPageNumber = Math.floor(quotient);
    }
    else {  // negative
        navPageNumber = Math.ceil(quotient);
    }

    for (var i = 0; i < navPageNumber; i++) {
        $('.next').click();
    }

    // $('#gallery').lightbox(); // No more lightbox, DR 841

    //    bauer.video.writeSidebarWidget({
    //        siteId: "HeatV2",
    //        keywords: $('#brightcoveKeywords').val(),
    //        parent: "video"
    //    });
});

