/* ****************************************** 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);
}


/* ##################################### 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);
    }
}






/* ****************************************** 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);

    //alert('Copy me: ' +  copyMe + '\ncopyMeBoolean:  ' + copyMeBoolean + '\ncopyMeBoolean (alt): ' + (copyMe == '1'));

    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;
}


