/* ****************************************** CONTENT QUERYING ****************************************** */

function querySubTopicContent(contentUri, parentContentUri) {
    //check to see if the function exists, if so then set link to the contenturi
    if (setEmailFriendLink) {
        setEmailFriendLink(contentUri, parentContentUri);
    }

    contentService.getSubTopicContentByContentURI(contentUri, parentContentUri, updateSubTopicPageContent);
}

function updateSubTopicPageContent(results) {

    var contentAsString = results[0];
    var title = results[1];
    var subtopicNavigationHTML = results[2];
    var rightModuleContentArray = results[3];
    var leftModuleContentArray = results[4];
    var backToTopMessage = results[5];

    var sectionTitle = document.getElementById("top");
    var subtopicContent = document.getElementById("centerColumn");
    var subtopicNavigation = document.getElementById("subtopicLIs");
    var rightModuleContentArea = document.getElementById("rightModulesContentArea");
    var leftModuleContentArea = document.getElementById("leftModulesContentArea");

    if (rightModuleContentArea != null) {
        rightModuleContentArea.innerHTML = "";
    }
    if (leftModuleContentArea != null) {
        leftModuleContentArea.innerHTML = "";
    }

    for (var i = 0; i < rightModuleContentArray.length; i++) {
        if (rightModuleContentArea != null) {
            rightModuleContentArea.innerHTML = rightModuleContentArea.innerHTML + rightModuleContentArray[i];
        }
    }

    for (var j = 0; j < leftModuleContentArray.length; j++) {
        if (leftModuleContentArea != null) {
            leftModuleContentArea.innerHTML = leftModuleContentArea.innerHTML + leftModuleContentArray[j];
        }
    }

    subtopicContent.innerHTML = contentAsString + "<a href='#top' class='backTop'>" + backToTopMessage + "</a>";

    if (sectionTitle != null) {
        sectionTitle.innerHTML = title;
    }

    subtopicNavigation.innerHTML = subtopicNavigationHTML;

    roundedCorners();
    toolTip();

}

function displayProduct(productId) {
    contentService.getFeaturedProductContentByObjectName(productId, displayFeaturedProductContent);
}

function displayFeaturedProductContent(results) {
    var featuredProductContent = document.getElementById("featuredProductContent");
    featuredProductContent.innerHTML = results;
}

/* ****************************************** EMAIL A FRIEND ****************************************** */

$(document).ready(function() {

    $("#emailBoxContent a.submit").click(function() {
        //alert('Submit 1');
        $(this).parents().filter("form").trigger("submit");
        //alert('Submit 2');
    });

    $("#emailBoxContent a.close").click(function() {
        closeEmailBox();
        var validator = $("#emailAFriendForm").validate();
        validator.resetForm();
    });
});

//static global variable to store the link used for email friend.  It can change by clicking subheadings in the page.
var EMAIL_FRIEND_LINK;
var EMAIL_FRIEND_PARENT_URI;

function emailAFriend() {

    //alert('emailAFriend()');
    var fromName = ''; //document.getElementById('fromName');
    var fromEmail = document.getElementById('senderEmail').value;

    var toName = ''; //document.getElementById('toName');
    var toEmail = document.getElementById('receiverEmail').value;

    var subject = document.getElementById('emailSubject').value;
    var message = document.getElementById('emailMessage').value;
    var copyMe = document.getElementById('emailCopyMe').checked;

    var link = document.getElementById('emailLink').value;

    //alert('link: ' + link + '\n' + 'EMAIL_FRIEND_LINK: ' + EMAIL_FRIEND_LINK + ' \n' + 'EMAIL_FRIEND_PARENT_URI: ' + EMAIL_FRIEND_PARENT_URI);

    if (EMAIL_FRIEND_LINK && EMAIL_FRIEND_PARENT_URI) {
        link = link.replace(EMAIL_FRIEND_PARENT_URI, EMAIL_FRIEND_LINK);
    }

    //alert('link: ' + link + '\n' + 'EMAIL_FRIEND_LINK: ' + EMAIL_FRIEND_LINK + ' \n' + 'EMAIL_FRIEND_PARENT_URI: ' + EMAIL_FRIEND_PARENT_URI);

    var subtopic = document.getElementById('emailSubtopic').value;
    var description = document.getElementById('emailDescription').value;

    
    trackEvent('EmailFriend', 'COMPLETE'); 
    emailAFriendService.emailAFriend(fromName, fromEmail, toName, toEmail, subject, message, link, subtopic, description, copyMe, closeEmailBox);
}

function confirmEmail() {
    closeEmailBox();
    $('#emailConfirm').fadeIn();
}

function closeEmailBox() {
    $('#emailBox').fadeOut();
    var validator = $("#emailAFriendForm").validate();
    validator.resetForm();
    $.emailFriendValidator.resetForm();
    $("#emailAFriendForm").resetForm();
    $('#emailConfirm').fadeIn();
}

/**
 * Used to set global variables used to create email friend link.
 * @param uri
 * @param parentUri
 */
function setEmailFriendLink(uri, parentUri) {
    EMAIL_FRIEND_LINK = uri;
    EMAIL_FRIEND_PARENT_URI = parentUri;
}

/* ****************************************** UTILITY FUNCTIONS ****************************************** */

/** Standard popup window open
 * The first parameter will be the new URL.
 * Subsequent parameters will be (if supplied) the width and height.
 * If not specified the width and height will be 500 and 200 respectively.
 */
function openWin() {
    var url, width = 500, height = 200, menu = "";
    args = arguments;
    url = args[0];
    if (args.length > 1) {
        width = args[1];
    }
    if (args.length > 2) {
        height = args[2];
    }
    if (args.length > 3) {
        if (args[3]) {
            menu = ",menubar=yes,toolbar=yes";
        }
    }
    var newwin = window.open(url, 'newWin' + Math.floor(Math.random() * 1000), 'width=' + width + ',height=' + height + ',scrollbars,resizable' + menu);
}

function replaceValueOnInputElement(elementObj, originalValueToReplace) {

    var inputValue = elementObj.value;

    if (inputValue == originalValueToReplace) {
        elementObj.value = '';
    }

    return false;
}

/* ****************************************** ONLINE ORDERING ****************************************** */

function addItemToCartByProductCodeCallback(resultArray) {

    var productCode = resultArray[0];
    var itemsInCart = resultArray[1];

    var cartLink = document.getElementById("cartLink");
    cartLink.style.display = "inline";

    updateHeaderCartQty(itemsInCart);
    updateQuantityValues('qtyval', itemsInCart);
    totalCartItems = itemsInCart;

    jQuery(function($) {$('#addToCart' + productCode).hide();});
}

function updateHeaderCartQty(qty) {
    var cartQtyHTML = document.getElementById("cartqty");
    cartQtyHTML.innerHTML = "(" + qty + ")";
}

function updateQtyByProductCodeCallback(resultArray) {

    var productCode = resultArray[0];
    var itemsInCart = resultArray[1];

    updateHeaderCartQty(itemsInCart);
    updateQuantityValues('qtyval', itemsInCart);
    //This is kind of a hack until I can find a better way
    selectOption('quantity' + productCode, itemsInCart - 1);
    hideBox('#qtychange' + productCode);

}

function selectOption(elementId, num) {
    var selObj = document.getElementById(elementId);
    selObj.selectedIndex = num;
}

function hideBox(code) {
    jQuery(function($) {$(code).hide();});
}

function updateQuantityValues(idVal, qty) {

    var qtyElements = $("[id*=" + idVal + "]");

    for (var i = 0; i < qtyElements.length; i++) {
        qtyElements[i].innerHTML = qty;
    }
}


/* ##################################### FONT SIZE CHANGER ############################### */

function setFontSizeFromCookie() {
    var fontSize = $.cookie('font-size');
    //alert('Font Size: ' + fontSize + ' null? ' + (fontSize != null) + ' blank? ' + (fontSize != ''));
    if (fontSize != null) {
        //alert('Font Size: settign to ' + fontSize );
        var currentFontSize = $('body').css('font-size');
        //alert('Before: ' + currentFontSize);

        var fontSizeNum = parseFloat(fontSize, 10);

        $('body').css('font-size', fontSizeNum);

        currentFontSize = $('body').css('font-size');
        //alert('After: ' + currentFontSize);
    }
}


