﻿
// Dojo Toolkit Included
dojo.require("dojo.fx");

// Event Handlers
window.onscroll = ScrollHandler;

// Initialize Page JavaScript Functions
function PageInit() {
    
    // Update Current Price / Current Configuration
    UpdateCurrentPrice();
    
    // Show Floating Dialog / Set Initial Position
	dojo.byId("currentConfig").style.visibility = "visible";
	dojo.byId("currentConfig").style.top = GetFloaterPosition + "px";
}

// Maximize Window
function maximizeWindow() {
    window.moveTo(0,0);
    window.resizeTo(screen.availWidth, screen.availHeight);
}

function ScrollHandler() {

    var top = GetFloaterPosition();

    // Set Floater Position
    if (dojo.byId("currentConfig").style.top != top + "px") {
        SetFloaterPosition();
    }        
}    

function GetFloaterPosition() {

    var scrollX, scrollY
    var sizeW, sizeH
    var eleTop;
    var retVal;
    
    var ele = dojo.byId("prodwrapper");
    
	// Get Position of Options Element
	if (ele.offsetParent) {
		eleTop = ele.offsetTop;
		
		// Add Parent Element Offsets
		while (ele = ele.offsetParent) {
			eleTop += ele.offsetTop;			
		}
	}

    // Get Page Scroll Position
    scrollX = document.body.scrollLeft;
    scrollY = document.body.scrollTop;

    // Determine Top Position
    if (scrollY <= eleTop) {
        retVal = eleTop;
    } else {
        retVal = scrollY;
    }  
    
    return retVal;    

}

// Set Position of Floating Dialog Box
function SetFloaterPosition() {

    var top = GetFloaterPosition();

    // Set Floater Position
    if (dojo.byId("currentConfig").style.top != top + "px") {
        var anim = dojo.fx.slideTo({
                node: "currentConfig",
                left: "700",
                top: top,
                unit: "px"});
        anim.play();
    }
}

// Update Totals & Current Configuration
function UpdateCurrentPrice() {

    // Get Base Product Value
    var productTotal = parseFloat(document.additem.baseprice.value);
    
    for (i = 0; i < document.additem.length; i++)
    {
        // Check Element Name
        if (document.additem.elements[i].name.indexOf("IDOption") >= 0)
        {
            // Check for Selected Element
            if (document.additem.elements[i].checked == true)
            {
                // Add Item Value to Total
                var item = document.additem.elements[i].value.split("|");
                productTotal += parseFloat(item[1]);                
            }
        }
    }
    
    // Update Total Price Element
    document.getElementById("totalprice").innerHTML = "Price With Selected Options: $" + productTotal.toFixed(2);                              
    document.getElementById("ccTotal").innerHTML = productTotal.toFixed(2);
    
    // Call Update Current Configuration Function
    UpdateCurrentConfiguration();
}

// Update Current Configuration Window
function UpdateCurrentConfiguration() {

    for (i = 0; i < document.additem.length; i++)
    {
        // Check Element Name
        if (document.additem.elements[i].name.indexOf("IDOption") >= 0)
        {
            // Check for Selected Element
            if (document.additem.elements[i].checked == true)
            {
                // Get Item Description
                var optionID = document.additem.elements[i].id;
                var eleTitle = document.additem.elements[i].name.replace(/IDOption/, "ct");
                var eleDesc = document.additem.elements[i].name.replace(/IDOption/, "cc");
                var itemDesc = document.getElementById("d" + optionID).innerHTML.split("[");
                var color;
                var weight;
                
                // Split at Parentheses
                if (itemDesc[0].indexOf("(") > 0)
                {
                    itemDesc = itemDesc[0].split("(");
                }
                
                // Limit Length
                if (itemDesc[0].length > 25)
                {
                    itemDesc[0] = itemDesc[0].substr(0,22) + "...";
                }
                
                if (itemDesc[0].indexOf("None") >= 0)
                {
                    itemDesc[0] = "None";
                    color = "#CCCCCC";
                    weight = "normal";
                } else {
                    color = "#333333";
                    weight = "bold";
                }                 
                
                // Update Current Configuration Element
                document.getElementById(eleTitle).style.color = color;
                document.getElementById(eleTitle).style.fontWeight = weight;
                
                document.getElementById(eleDesc).style.color = color;
                document.getElementById(eleDesc).innerHTML = itemDesc[0];
            }
        }
    }
}
