﻿

$(document).ready(function() {
    SwapImage(0);
    ThumbSizer();

    $('#Dialog').dialog({ autoOpen: false, title: 'Shifting System Information' });
    $('#Dialog').dialog('option', 'buttons', { "Close": function() { ShiftSystemClose(); }, "View Details": function() { window.location = $('#DialogLink').attr('href'); } });
    $('#Dialog').dialog('option', 'height', 300);
    $('#Dialog').dialog('option', 'width', 425);

    $('#VerifyDialog').dialog({ autoOpen: false, title: 'Component Photo Verification' });
    $('#VerifyDialog').dialog('option', 'buttons', { "Cancel": function() { $('#VerifyDialog').dialog('close') }, "Verify": function() { confirmVerify(); } });
    $('#VerifyDialog').dialog('option', 'width', 425);

    $('#' + prefixPgControl('WeightDialog')).dialog({ autoOpen: false, title: 'Component Weight List', resizable:false, maxHeight:500 });
    $('#' + prefixPgControl('WeightDialog')).dialog('option', 'buttons', { "Close": function() { $('#' + prefixPgControl('WeightDialog')).dialog('close') } });
    $('#' + prefixPgControl('WeightDialog')).dialog('option', 'width', 525);

    $('#' + prefixPgControl("stars_wrapper1")).stars({ split: 2, captionEl: $("#stars-cap") });
    $('#' + prefixPgControl("stars_wrapper2")).stars({ split: 2, captionEl: $("#stars-cap2") });
    $('.ttip').tooltip({ fade: 200, showURL: false });
    SetQualityRarity();
});


// Photo Handling

var PhotoDataString;
var PhotoData;

function SwapImage(selIndex) {

    if (PhotoData == null) {
        var strData = $('#' + prefixPgControl('hfPhotoData')).val();
        if (strData.length == 0) { return false; }
        PhotoData = JSON.parse(strData);
    }

    $('#' + prefixPgControl('MainPhoto')).attr('src', PhotoData[selIndex].URL);
    var pData = PhotoData[selIndex].Source;
    if (PhotoData[selIndex].Data.length)
    { pData += '<br/>' + PhotoData[selIndex].Data; }
    $('#' + prefixPgControl('SourceText')).html(pData);
}

function ThumbSizer() {

    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    if (myWidth < 1030) 
    {
        $('.PhotoThumb').addClass('PhotoThumbReduced').removeClass('PhotoThumb'); 
    }
}

// Quality / Rarity
function SubmitQualityRarity() {
    var hfQ = $('#' + prefixPgControl("hfQuality"));
    var hfR = $('#' + prefixPgControl("hfRarity"));

    var Qinstance = $('#' + prefixPgControl("stars_wrapper1")).data("stars");
    var QNVal = parseInt(Qinstance.options.value);

    var Rinstance = $('#' + prefixPgControl("stars_wrapper2")).data("stars");
    var RNVal = parseInt(Rinstance.options.value);

    var sVal = $('#' + prefixPgControl("hfQuality")).val();
    var sSplit = sVal.split('|');
    var fRate = parseFloat(sSplit[0]);
    var cRate = parseInt(sSplit[1]);
    if (QNVal > 0) {
        fRate = fRate * cRate;
        fRate = fRate + QNVal;
        cRate = cRate + 1;
        fRate = fRate / cRate;
        $('#' + prefixPgControl("hfQuality")).val(fRate + '|' + cRate);
    }

    sVal = $('#' + prefixPgControl("hfRarity")).val();
    sSplit = sVal.split('|');
    fRate = parseFloat(sSplit[0]);
    cRate = parseInt(sSplit[1]);
    if (RNVal > 0) {
        fRate = fRate * cRate;
        fRate = fRate + RNVal;
        cRate = cRate + 1;
        fRate = fRate / cRate;
        $('#' + prefixPgControl("hfRarity")).val(fRate + '|' + cRate);
    }
    $('#' + prefixPgControl("stars_wrapper1")).stars("selectID", -1);
    $('#' + prefixPgControl("stars_wrapper2")).stars("selectID", -1);
    SetQualityRarity();

    var cbItem = JSON.parse($('#' + prefixPgControl('hfCBTemplate')).val());
    cbItem.Event = "vote";
    cbItem.Arguments = QNVal + "|" + RNVal;

    ViewComponentCallback(JSON.stringify(cbItem), 0);
    $('#Button1').attr("disabled", "disabled").addClass('disabled');
}

function SetQualityRarity() {
    var QCell = $('#' + prefixPgControl("QualityCell"));
    var RCell = $('#' + prefixPgControl("RarityCell"));

    var sVal = $('#' + prefixPgControl("hfQuality")).val();
    var sSplit = sVal.split('|');
    QCell.empty();
    if (parseInt(sSplit[1]) > 0)
    { QCell.append(GetRatingString(Math.floor(parseFloat(sSplit[0])), 'Q') + ', ' + (parseFloat(sSplit[0]) / 2).toFixed(2) + '/5 (' + sSplit[1] + ' vote' + (parseInt(sSplit[1]) > 1 ? 's' : '') + ')'); }
    else
    { QCell.append('Not Rated'); }

    sVal = $('#' + prefixPgControl("hfRarity")).val();
    sSplit = sVal.split('|');
    RCell.empty();
    if (parseInt(sSplit[1]) > 0)
    { RCell.append(GetRatingString(Math.floor(parseFloat(sSplit[0])), 'R') + ', ' + (parseFloat(sSplit[0]) / 2).toFixed(2) + '/5 (' + sSplit[1] + ' vote' + (parseInt(sSplit[1]) > 1 ? 's' : '') + ')'); }
    else
    { RCell.append('Not Rated'); }
}

function GetRatingString(val, ctx) {
    var QArray = new Array();
    QArray[1] = "Very Poor";
    QArray[2] = "Poor";
    QArray[3] = "Not that bad";
    QArray[4] = "Fair";
    QArray[5] = "Average";
    QArray[6] = "Above Average";
    QArray[7] = "Good";
    QArray[8] = "Very Good";
    QArray[9] = "Excellent";
    QArray[10] = "Perfection";

    var RArray = new Array();
    RArray[1] = "Extremely Common";
    RArray[2] = "Very Common";
    RArray[3] = "Common";
    RArray[4] = "Fairly Common";
    RArray[5] = "Average";
    RArray[6] = "Uncommon";
    RArray[7] = "Somewhat Rare";
    RArray[8] = "Rare";
    RArray[9] = "Exceedingly Rare";
    RArray[10] = "Legendary";

    if (ctx == "Q")
    { return QArray[val]; }
    else { return RArray[val]; }
}

function ViewComponentCallbackHandler(arg, ctx) {
    if (ctx == 0)
    { }
    //else if (ctx == 1) {
    //    $('#watchTxt').html(arg);
    //}
    else if (ctx == 2) {
        // remove verification link
        $('#' + prefixPgControl("ConfirmRow")).empty();
    }
    return;
}

function ShiftingSystemInfo(id) {
    $('#Dialog').dialog('option', 'height', 300);
    $('#Dialog').dialog('option', 'width', 425);
    var dataStr = $('#' + prefixPgControl('hf_' + id));
    var data = dataStr.val();
    $('#DialogInner').empty();
    $('#DialogInner').append(data);
    $('#Dialog').dialog('open');
}

function ShiftSystemClose() {
    $('#Dialog').dialog('close');
}

function verifyPhotos() {
    $('#VerifyDialog').dialog('open');
}

function confirmVerify() {
    $('#VerifyDialog').dialog('close');
    var cbItem = JSON.parse($('#' + prefixPgControl('hfCBTemplate')).val());
    cbItem.Event = "photoverify";
    cbItem.Arguments = "confirm";
    ViewComponentCallback(JSON.stringify(cbItem), 2);
}


function OpenWeightList() {
    $('#' + prefixPgControl('WeightDialog')).dialog('open');
}


