﻿$(document).ready(function () {
    //PRICE HISTORY CHART
    $("#priceHistoryChartThumb").click(function () {
        var state = jQuery($("#priceHistoryChartWrapper")).is(":hidden") ? "show" : "hide";
        if (state == "show") {
            $("#priceHistoryChartWrapper").fadeIn('fast');
        }
        else {
            $("#priceHistoryChartWrapper").hide();
        }
        return false;
    });

    $("#priceHistoryChartWrapperCancel").click(function () {
        $("#priceHistoryChartWrapper").hide();
    });

    //CAROUSEL
    $('#thumbnails').jcarousel({
        buttonPrevHTML: "<a class='prev right'></a>",
        buttonNextHTML: "<a class='next right'></a>",
        wrap: 'circular'
    });

    $('.relatedList').jcarousel({
        buttonPrevHTML: "<a class='prev right'></a>",
        buttonNextHTML: "<a class='next right'></a>"
    });

    //NOTIFICATIONS DIALOGUE
    $('.notificationRequest input').watermark('Email Address');

    $('.notificationRequest a.confirm').click(function () {
        $('.notificationRequest,.notificationOn').toggle('fast', imageAlign);
    });

    $('.notificationRequest a.cancel,.stockNotification').click(function () {
        $('.notificationRequest,.stockNotification').toggle('fast', imageAlign);
    });

    //TABS
    $('.uiTabs').tabs({ selected: 0, cache: true, spinner: '' });

    //MORE DESCRIPTION
    $('.moreButton').click(function () {
        $('#pnlExtraInfo').tabs('select', '#productDescription');
    });

    $('a#hlMore').click(imageAlign);

    //ACCORDIONS
    $("* h3.inactive").next("div").hide();

    $(".accordion h3").click(function () {
        $(this).next(".sectionContent").slideToggle("fast");
        if ($(this).hasClass("active")) {
            $(this).removeClass("active").addClass("inactive");
        }

        else if ($(this).hasClass("inactive")) {
            $(this).removeClass("inactive").addClass("active");
        }

        else {
            $(this).addClass("active");
        }
    });

    //WIDGET
    $.tablesorter.addWidget({
        id: "zebra",
        format: function (table) {
            $("tr:visible", table.tBodies[0])
	            .filter(':even')
	            .removeClass(table.config.widgetZebra.css[1]).addClass(table.config.widgetZebra.css[0])
                .end().filter(':odd')
	            .removeClass(table.config.widgetZebra.css[0]).addClass(table.config.widgetZebra.css[1]);
        }
    });

    $("#tradePrices").tablesorter({ widgets: ['zebra'] });

    //MARKETING SIDECOLUMN
    $('.serviceTabs ul').superfish({
        autoArrows: false,
        animation: { opacity: 'show' }
    });
});

function imageAlign() {
    var containerHeight = $('.productOverview div.body').height();
    var imagesHeight = $('.productOverview div.images').height();
    var imagesMargin = (containerHeight - imagesHeight) / 2;
    $('.productOverview div.images').animate({ 'margin-top': imagesMargin + 'px' }, 'fast');
}

function scrollClick(imgClicked, newImage) {
    if ($(imgClicked).hasClass("active")) {
        return;
    }

    var wrap = $(".mainImage").fadeTo("medium", 0.5, function () {
        var img = new Image();

        img.onload = function () {
            wrap.find("img").attr("src", newImage);
            wrap.fadeTo("slow", 1);
            imageAlign();
        };

        img.src = newImage;
    });

    $(".items img").removeClass("active");
    $(imgClicked).addClass("active");
}

$(window).load(function () {
    imageAlign();
});

function addCategory(control) {
    $(control).next('div').show('fast');
    $(control).hide('fast');
    return false;
}

function closeCategory(control) {
    $('#hlAddCategory,.categoryEdit').toggle('fast');
    return false;
}

function showEditPrice(divID) {
    $("#" + divID).show();
}

function cancelEditPrice(divID) {
    $("#" + divID).hide();
}

function GetGoogleData(drawControl, searchTerm) {
    var searchControl = new GSearchControl();
    searchControl.setResultSetSize(GSearch.LARGE_RESULTSET);

    var searchOptions = new google.search.SearcherOptions();
    searchOptions.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);

    var webSearch = new google.search.WebSearch();
    var extendedArgs = google.search.Search.RESTRICT_EXTENDED_ARGS;
    webSearch.setRestriction(extendedArgs, { gl: 'uk' });
    searchControl.addSearcher(webSearch, searchOptions);

    var drawOptions = new GdrawOptions();
    drawOptions.setDrawMode(GSearchControl.DRAW_MODE_LINEAR);
    searchControl.draw(document.getElementById(drawControl, drawOptions));
    searchControl.execute(searchTerm);
}

function togglePrivateNotes(showEdit) {
    if (showEdit) {
        $("#privateNotesEdit").show(500);
        $("#privateNotes").hide(500);
    }
    else {
        $("#privateNotesEdit").hide(500);
        $("#privateNotes").show(500);
    }
}

function savePrivateNotes() {
    var notes = $('#tbPrivateNotes').val();

    if (notes.length == 0) {
        $('#privateNotes p').html("Add some notes");
    }
    else {
        $('#privateNotes p').html(notes);
    }

    $("#privateNotesEdit").hide(500);
    $("#privateNotes").show(500);
}
