﻿var timerMilliseconds = 4000;
var prodMatch = {
    mot:0, mnt:0,
    mdt:1, fot:1, fdt:1, fnt:1, fft:1, fct:1,
    mdw:2, mft:2,
    mpt:3,
    mpf:4, mpw:4, fpt:4, fpf:4, fpw:4,
    mof:5, 
    mow:6, mnw:6, fdw:6, fnw:6, fow:6,
    mdf: 7, fdf:7,
    mct:8, mcf:8, mcw:8, ftc:8, fcf:8, fcw:8,
    mnf:9, fnf:9,
    mff:10, mfw:10,
    fof:11, fff:11, ffw:11
};

var mainImages = ["/x.jpg", "/play.jpg"];
var mainImageCurrent = 0;
var currentIndex = 0;
var selectRandom = true;
var eventByCode = true;

$(document).ready(function(){
    attachEvents();
    startTimer();
});

function startTimer() {
    $(document).everyTime(timerMilliseconds, function(i) {
      swapProducts();
    }, 0);
}

function swapProducts() {
    if (selectRandom)
    {
        var index = pickRandom();
        var imgIndex = 0;
        
        if (index == 0 || index == 1 || index == 2) {
            imgIndex = 0;
        } else {
            imgIndex = 1;
        }
        
        $('#mainImage').attr("src", mainImages[imgIndex]);
        
    } else {
        // rotate all main images each time the timer fires
        $('#mainImage').attr("src", mainImages[mainImageCurrent]);

        if (mainImageCurrent == mainImages.length - 1) {
            mainImageCurrent = 0;
        } else {
            mainImageCurrent += 1;
        }
    }
}

function getProductIndex(arr, key) {
    var index = arr[key];
    return index;
}

function buildProductCode() {
    var code = "";
    
    if ($('#genderM').is(':checked')) { code += "m"; };
    if ($('#genderF').is(':checked')) { code += "f"; };
    if ($('#typeP').is(':checked')) { code += "p"; };
    if ($('#typeO').is(':checked')) { code += "o"; };
    if ($('#typeD').is(':checked')) { code += "d"; };
    if ($('#typeC').is(':checked')) { code += "c"; };
    if ($('#typeN').is(':checked')) { code += "n"; };
    if ($('#typeF').is(':checked')) { code += "f"; };
    if ($('#styleT').is(':checked')) { code += "t"; };
    if ($('#styleF').is(':checked')) { code += "f"; };
    if ($('#styleW').is(':checked')) { code += "w"; };

    return code;
}

function attachEvents() {
    $('#genderM').click(lookupProduct);
    $('#genderF').click(lookupProduct);
    $('#typeP').click(lookupProduct);
    $('#typeO').click(lookupProduct);
    $('#typeD').click(lookupProduct);
    $('#typeC').click(lookupProduct);
    $('#typeN').click(lookupProduct);
    $('#typeF').click(lookupProduct);
    $('#styleT').click(lookupProduct);
    $('#styleF').click(lookupProduct);
    $('#styleW').click(lookupProduct);
}

function lookupProduct() {
    selectRandom = false;
    
    var code = buildProductCode();
    
    if (code.length == 3)
    {
        var index = getProductIndex(prodMatch, code)
        setFlashIndex(index)
    }
}

function pickRandom() {
    var productCode = "";
    
    var count = 0; 
    for (k in prodMatch) {
        if (prodMatch.hasOwnProperty(k)) {
            count += 1;
        }
    }
   
    //var random = Math.floor(Math.random() * count)
    var random = currentIndex;

    var position = 0;
    for (k in prodMatch) {
        if (random == position) {
            productCode = k;
            break;
        }
        position += 1;
    }
    
    currentIndex += 1
    if (currentIndex >= count) {
        currentIndex = 0;
    }
    
    var index = getProductIndex(prodMatch, productCode);
    
    selectMatrix(index, productCode);
    
    return index;
}

function selectMatrixByIndex(index) {

    var position = 0;
    for (k in prodMatch) {
        if (index == position) {
            productCode = k;
            break;
        }
        position += 1;
    }
    
    selectMatrix(index, productCode);
}

function selectMatrix(index, productCode) {
    clearAllSelectors();
    
    var props = productCode.split("");
    
    switch(props[0]) {
        case 'm':
          $('#genderM').attr('checked', true); 
          break;
        case 'f':
          $('#genderF').attr('checked', true); 
          break;
    }

    switch(props[1]) {
        case 'p':
          $('#typeP').attr('checked', true); 
          break;
        case 'o':
          $('#typeO').attr('checked', true); 
          break;
        case 'd':
          $('#typeD').attr('checked', true); 
          break;
        case 'c':
          $('#typeC').attr('checked', true); 
          break;
        case 'n':
          $('#typeN').attr('checked', true); 
          break; 
        case 'f':
          $('#typeF').attr('checked', true); 
          break; 
    }
    
    switch(props[2]) {
        case 't':
          $('#styleT').attr('checked', true); 
          break;
        case 'f':
          $('#styleF').attr('checked', true); 
          break;
        case 'w':
          $('#styleW').attr('checked', true); 
          break;
    }
    
    //if (setFlash) {
        setFlashIndex(index);
    //}
}

function clearAllSelectors() {
    $('#genderM').attr('checked', false); 
    $('#genderF').attr('checked', false); 
    $('#typeP').attr('checked', false); 
    $('#typeO').attr('checked', false); 
    $('#typeD').attr('checked', false); 
    $('#typeC').attr('checked', false); 
    $('#typeN').attr('checked', false); 
    $('#typeF').attr('checked', false); 
    $('#styleT').attr('checked', false); 
    $('#styleF').attr('checked', false); 
    $('#styleW').attr('checked', false); 
}

function setFlashIndex(index) {
    if (window.setFlashValue)
    {
        eventByCode = true;
	    window.setFlashValue(index);
    }
}