var CARD_REVIEWER_URL = "planReviewerForm.php";
var SEATING_PLAN_TITLE = "2 - Seating Planning";

var layerCard;
var layerCardBackground;
var layerPrimaryBorder;
var layerSecondaryBorder;
var layerPrimaryText;
var layerSecondaryText;
var topPosition;
var leftPosition;

var dataChanged = false;

// Card designer scripts
function saveCard() {
	dataChanged = false;
    document.cardDesign.submit();
}

function changeCardColor() {
    var color = document.cardDesign.cardColor.options[document.cardDesign.cardColor.selectedIndex].value;
    var colorCode = translateCardColorToHex(color);
    if (colorCode) {
        globalCardColor = colorCode;
        setBgColor(layerCardBackground, colorCode);
        dataChanged = true;
    }
}

function changeBorderStyle() {
    selectedBorder = document.cardDesign.BorderStyle.options[document.cardDesign.BorderStyle.selectedIndex].value;
    changePrimaryBorderInk();
    changeSecondaryBorderInk();
}

function changePrimaryBorderInk() {
    var imgSource;
    selectedBorderPrimary = document.cardDesign.DesignPrimaryInk.options[document.cardDesign.DesignPrimaryInk.selectedIndex].value;
    selectedBorderPrimaryText = document.cardDesign.DesignPrimaryInk.options[document.cardDesign.DesignPrimaryInk.selectedIndex].text;
    imgSource = getBorderSource(selectedBorderPrimary, selectedBorder);
    changeCardImage(layerPrimaryBorder, "cardBorderPrimaryImage", imgSource);
    dataChanged = true;
}

function changeSecondaryBorderInk() {
    var imgSource;
    selectedBorderSecondary = document.cardDesign.DesignSecondaryInk.options[document.cardDesign.DesignSecondaryInk.selectedIndex].value;
    selectedBorderSecondaryText = document.cardDesign.DesignSecondaryInk.options[document.cardDesign.DesignSecondaryInk.selectedIndex].text;
    imgSource = getBorderSource(selectedBorderSecondary, selectedBorder);
    changeCardImage(layerSecondaryBorder, "cardBorderSecondaryImage", imgSource);
    dataChanged = true;
}

function changeTypeFace() {
    selectedFont = document.cardDesign.TypeStyle.options[document.cardDesign.TypeStyle.selectedIndex].value;
    selectedFontText = document.cardDesign.TypeStyle.options[document.cardDesign.TypeStyle.selectedIndex].text;
    changeTypePrimaryInk();
    changeTypeShadowInk();
}

function changeTypePrimaryInk() {
    var imgSource;
    selectedFontPrimary = document.cardDesign.TypePrimaryInk.options[document.cardDesign.TypePrimaryInk.selectedIndex].value;
    var imgName
    if (selectedFontPrimary.length == 4) {
        imgName = selectedFontPrimary.substr(1, 3);
    } else {
    	imgName = selectedFontPrimary;
	}
    if (singleLine) {
        imgSource = "images/" + selectedFont + "1" + imgName + ".gif";
    } else {
        imgSource = "images/" + selectedFont + imgName + ".gif";
    }
    changeCardImage(layerPrimaryText, "cardTextPrimaryImage", imgSource);
    var colorCode = getColorCode(imgName);
    dataChanged = true;
}

function getColorCode(colorIn) {
    var colorCode;
    if ((colorIn == "YEL") || (colorIn == "MYEL")) {
        colorCode = "#FFEE00";
    } else if ((colorIn == "SIL") || (colorIn == "MSIL")) {
        colorCode = "#C0C0C0";
    } else if ((colorIn == "MAG") || (colorIn == "MMAG")) {
        colorCode = "#DD0066";
    } else if ((colorIn == "CYN") || (colorIn == "MCYN")) {
        colorCode = "#00AAFF";
    } else if (colorIn == "BLK") {
        colorCode = "#000000";
    } else {
        colorCode = globalCardColor;
    }
    return colorCode;
}

function changeTypeShadowInk() {
    var imgSource;
    selectedFontSecondary = document.cardDesign.TypeShadowInk.options[document.cardDesign.TypeShadowInk.selectedIndex].value;
    selectedFontSecondaryText = document.cardDesign.TypeShadowInk.options[document.cardDesign.TypeShadowInk.selectedIndex].text;
   	if (selectedFontSecondary == "None") {
      	imgSource = "images/spacer.gif";
    } else {
        var imgName;
        if (selectedFontSecondary.length == 4) {
            imgName = selectedFontSecondary.substr(1, 3);
        } else {
       	    imgName = selectedFontSecondary;
        }
        if (singleLine) {
            imgSource = "images/" + selectedFont + "1" + imgName + ".gif";
        } else {
            imgSource = "images/" + selectedFont + imgName + ".gif";
        }
    }
    changeCardImage(layerSecondaryText, "cardTextSecondaryImage", imgSource);
    var colorCode = getColorCode(selectedFontSecondary);
    dataChanged = true;
}

function changeCardLines() {
    var noLines = document.cardDesign.cardLines.options[document.cardDesign.cardLines.selectedIndex].value;
    if (noLines == "One") {
        singleLine = true;
    } else {
        singleLine = false;
    }
    changeTypeFace();
}

function init() {
   	layerCard = getLayer("card");
    layerCardBackground = getLayer("cardBackground");
    layerPrimaryBorder = getLayer("cardBorderPrimary");
    layerSecondaryBorder = getLayer("cardBorderSecondary");
    layerPrimaryText = getLayer("cardTextPrimary");
    layerSecondaryText = getLayer("cardTextSecondary");
    leftPosition = getPageLeft(layerCard) + 2;
    topPosition = getPageTop(layerCard) + 2;
    moveLayerTo(layerCardBackground, leftPosition, topPosition);
    moveLayerTo(layerPrimaryBorder, leftPosition, topPosition);
    moveLayerTo(layerSecondaryBorder, leftPosition, topPosition);
    moveLayerTo(layerPrimaryText, leftPosition, topPosition);
    moveLayerTo(layerSecondaryText, leftPosition+1, topPosition+1);
    initSettings();
    changeCardColor();
    changeBorderStyle();
    changeTypeFace();
    showLayer(layerCardBackground);
    showLayer(layerPrimaryBorder);
    showLayer(layerSecondaryBorder);
    showLayer(layerPrimaryText);
    showLayer(layerSecondaryText);
//    window.onresize = restorePage;
    ready = true;
    dataChanged = false;
}

function hideLayer(currentLayer) {
   	currentLayer.style.visibility = "hidden";
}

function showLayer(layer) {
    layer.style.visibility = "visible";
}


function restorePage() {
    window.location.href = window.location.href;
}

function initSettings() {
    changeCardColor();
	// Set the border type
    for (var i=0; i<document.cardDesign.BorderStyle.length ; i++) {
        if (document.cardDesign.BorderStyle.options[i].value == selectedBorder) {
           	// This is the selected border
           	document.cardDesign.BorderStyle.options[i].selected = true;
            break;
        }
    }
    for (var i=0; i<document.cardDesign.DesignPrimaryInk.length ; i++) {
        if (document.cardDesign.DesignPrimaryInk.options[i].value == selectedBorderPrimary) {
            // This is the selected border primary ink type
            document.cardDesign.DesignPrimaryInk.options[i].selected = true;
            break;
        }
    }
    for (var i=0; i<document.cardDesign.DesignSecondaryInk.length ; i++) {
        if (document.cardDesign.DesignSecondaryInk.options[i].value == selectedBorderSecondary) {
            // This is the selected border secondary ink type
            document.cardDesign.DesignSecondaryInk.options[i].selected = true;
            break;
        }
    }
    changeBorderStyle();
    for (var i=0; i<document.cardDesign.TypeStyle.length ; i++) {
         if (document.cardDesign.TypeStyle.options[i].value == selectedFont) {
             // This is the selected font
             document.cardDesign.TypeStyle.options[i].selected = true;
             break;
         }
    }
    for (var i=0; i<document.cardDesign.TypePrimaryInk.length ; i++) {
        if (document.cardDesign.TypePrimaryInk.options[i].value == selectedFontPrimary) {
            // This is the selected font
            document.cardDesign.TypePrimaryInk.options[i].selected = true;
            break;
        }
    }
    for (var i=0; i<document.cardDesign.TypeShadowInk.length ; i++) {
        if (document.cardDesign.TypeShadowInk.options[i].value == selectedFontSecondary) {
            // This is the selected font
            document.cardDesign.TypeShadowInk.options[i].selected = true;
            break;
        }
    }
    changeTypeFace();
    if (singleLine) {
        document.cardDesign.cardLines.options[0].selected = true;
    } else {
        document.cardDesign.cardLines.options[1].selected = true;
    }
    changeCardLines();
    for (var i=0; i<document.cardDesign.cardColor.length ; i++) {
         if (document.cardDesign.cardColor.options[i].value == selectedCardColor) {
             // This is the selected font
             document.cardDesign.cardColor.options[i].selected = true;
             break;
         }
    }
    if (customMessage1 != "") {
    	document.cardDesign.line1.value = customMessage1;
    }
    if (customMessage2 != "") {
    	document.cardDesign.line2.value = customMessage2;
    }
}

function retrieveCard() {
    try {
    	location.assign(baseURL + "retrieve_card.php");
    }
    catch(e) {
    	message = "There was an error in the retrieveCard function - baseURL = " 
    		+ baseURL + "; error message = " + e.message;
    	fileLocation = "http://www.place-cards.com/js/cardDesigner.js";
    	reportError(message, fileLocation, 275);
    	try {
    		window.location.assign(baseURL + "retrieve_card.php");
    	}
    	catch(e) {
     		message = "window.location also fails"; 
	    	fileLocation = "http://www.place-cards.com/js/cardDesigner.js";
 		   	reportError(message, fileLocation, 275);
    	}	
    }
}

function getLayer(layerID) {
    if (document.all) {
    	return eval("document.all." + layerID);
  	} else if (document.getElementById) {
    	return document.getElementById(layerID);
  	}
  	return null;
}

function moveLayerTo(layer, x, y) {
 	if (document.all) {
    	layer.style.pixelLeft = x;
    	layer.style.pixelTop  = y;
  	} else if (document.getElementById) {
    	layer.style.left = x;
    	layer.style.top  = y;
  	}
}

function getLeft(layer) {
  if (document.layers) {
    return layer.left;
  } else if (document.all) {
    return layer.style.pixelLeft;
  } else if (document.getElementById) {
    return parseInt(layer.style.left);
  }
  return -1;
}

function getTop(layer) {
  if (document.layers) {
    return layer.top;
  } else if (document.all) {
    return layer.style.pixelTop;
  } else if (document.getElementById) {
    return parseInt(layer.style.top);
  }
  return -1;
}

function getPageLeft(layer) {
  var x;
  if (document.layers) {
    return layer.pageX;
  } else if (document.all || document.getElementById) {
    x = 0;
    while (layer.offsetParent != null) {
      x += layer.offsetLeft;
      layer = layer.offsetParent;
    }
    x += layer.offsetLeft;
    return x;
  }
  return -1;
}

function getPageTop(layer) {
  var y;
  if (document.layers) {
    return layer.pageY;
  } else if (document.all || document.getElementById) {
    y = 0;
    while (layer.offsetParent != null) {
      y += layer.offsetTop;
      layer = layer.offsetParent;
    }
    y += layer.offsetTop;
    return y;
  }
  return -1;
}

function setBgColor(layer, color) {
  if (document.layers) {
    layer.bgColor = color;
  } else if (document.all || document.getElementById) {
    layer.style.backgroundColor = color;
  }
}

function changeCardImage(layerObj, imageName, source) {
  var imgObj;
  if (document.layers)
    imgObj = layerObj.document.images[imageName];
  else if (document.all || document.getElementById)
    imgObj = document.images[imageName];
  imgObj.src = source;
}

addEvent(window, "load", init, false);

// Error test - inject an error
// window_height.style = 1;





