﻿/// <reference path="jquery-1.3.2.min.js" />

$(document).ready(function() {

    $('.freebieTitle').click(function(event) {
        event.preventDefault();

        var parentLi = $(this).parents('li');
        var freebieId = parentLi.attr('id');

        var voteCountItem = parentLi.children('.freebieVoteBlock').children('.freebieVoteArrows').children('.freebieVoteNum');

        window.open($(this).attr('href'));

        $.ajax({
            url: "/Services/FreebieWebService.asmx/Click",
            type: "POST",
            data: ({ id: freebieId }),
            success: function() {
                voteCountItem.html((voteCountItem.html() * 1) + 1);
            }
        });

    });

    $('.uppic').click(function(event) {
        event.preventDefault();

        var parentLi = $(this).parents('li');
        var freebieId = parentLi.attr('id');

        var voteCountItem = parentLi.children('.freebieVoteBlock').children('.freebieVoteArrows').children('.freebieVoteNum');
        var downItem = parentLi.children('.freebieVoteBlock').children('.freebieVoteArrows').children('.downpic');

        if ($(this).attr('class').indexOf('up-pic-gray') >= 0) {

            if (downItem.attr('class').indexOf('down-pic-gray') == -1) {
                downItem.removeClass('down-pic')
                        .addClass('down-pic-gray');

                voteCountItem.html((voteCountItem.html() * 1) + 10);

                $.ajax({
                    url: "/Services/FreebieWebService.asmx/VoteUp",
                    type: "POST",
                    data: ({ id: freebieId })
                });
            }

            $(this).removeClass('up-pic-gray')
                   .addClass('up-pic');

            voteCountItem.html((voteCountItem.html() * 1) + 10);

            $.ajax({
                url: "/Services/FreebieWebService.asmx/VoteUp",
                type: "POST",
                data: ({ id: freebieId })
            });

        }

    });

    $('.downpic').click(function(event) {
        event.preventDefault();

        var parentLi = $(this).parents('li');
        var freebieId = parentLi.attr('id');

        var voteCountItem = parentLi.children('.freebieVoteBlock').children('.freebieVoteArrows').children('.freebieVoteNum');
        var upItem = parentLi.children('.freebieVoteBlock').children('.freebieVoteArrows').children('.uppic');

        if ($(this).attr('class').indexOf('down-pic-gray') >= 0) {

            if (upItem.attr('class').indexOf('up-pic-gray') == -1) {
                upItem.removeClass('up-pic')
                        .addClass('up-pic-gray');

                voteCountItem.html((voteCountItem.html() * 1) - 10);

                $.ajax({
                    url: "/Services/FreebieWebService.asmx/VoteDown",
                    type: "POST",
                    data: ({ id: freebieId })
                });
            }

            $(this).removeClass('down-pic-gray')
                   .addClass('down-pic');

            voteCountItem.html((voteCountItem.html() * 1) - 10);

            $.ajax({
                url: "/Services/FreebieWebService.asmx/VoteDown",
                type: "POST",
                data: ({ id: freebieId })
            });

        }

    });

    $('#sortList').change(function() {

        var hashes = window.location.href.slice(window.location.href.indexOf('//') + 2).split('/');
        if (hashes[1] == 'site') {
            window.location = 'http://' + hashes[0] + '/' + hashes[1] + '/' + hashes[2] + '/1/' + this.value + '/';
        }
        else {
            window.location = 'http://' + hashes[0] + '/' + hashes[1] + '/1/' + this.value + '/';
        }

    });

    $('.searchButton').click(function(e) {
        e.preventDefault();
        var hashes = window.location.href.slice(window.location.href.indexOf('//') + 2).split('/');
        window.location = 'http://' + hashes[0] + '/search/' + $('#searchTextBox').val();
    });

});