﻿
/*** Floating ISI variables ***/
var expcolSpeed = 500;
var minISIHeight = 60;
var maxISIHeight = 447;
var spaceHeight = 150;

/*** Text Size variables ***/
var textStatus = "small",
textSizeArray = [12,14,16];
defaultSizeIndex = 0,
containerDiv = "container";

function initISI() {
    $('#btnIsiExpCol').click( function(e) {
        e.preventDefault();
        
        if ($('#floating_isi').hasClass('open')) {
            $('#isi_content').animate({
                height: minISIHeight
            }, expcolSpeed, function() {
                $('#floating_isi').removeClass('open')
            });
        } else {            
            $('#isi_content').animate({
                height: getOpenISIHeight()
            }, expcolSpeed, function() {
                $('#floating_isi').addClass('open')
                $(window).resize(resizeISI);                
            });           
        }
    });
}

var getOpenISIHeight = function() {
    var windowHeight = $(window).height();    
    var isiHeight = (windowHeight - spaceHeight < maxISIHeight) ? windowHeight - spaceHeight : maxISIHeight;
    return isiHeight;
}

function resizeISI() {
    if ($('#floating_isi').hasClass('open')) {
        var isiHeight = getOpenISIHeight();
        $('#isi_content').css('height', isiHeight);    
    }
}

function initUtility() {
    $('#email').click(function (e) {
        e.preventDefault();
        jQuery.modalDialog.show('#popupEmailPage', document.forms[0], { backgroundColor: "#ffffff" });
    });

    $('div.popup a.close').click( function(e) {
        e.preventDefault();
        jQuery.modalDialog.hide(); 
    });

    $('#btnSend').click( function(e) {
        e.preventDefault();
        if ( validateForm() ) {
            $('#popupEmailPage').hide();
            $('#popupEmailPageSuccess').show();
        };
    });
    
    $('#btnSendAnother').click( function(e) {
        e.preventDefault();
        $('#popupEmailPage').show();
        $('#popupEmailPageSuccess').hide();
        $('#txtToEmail').val('');
        $('#txtToName').val('');
    });

	$('.textSize a').each(function(index, el) {	
		$(el).bind("click", function(event) {
            event.preventDefault();
            setTextSize(index);
        });
		//textSize("up");
	});
	
	size = getCookieValue("textSizeCookie");
	if(isNaN(size)) {
		currentSizeIndex = defaultSizeIndex;
	} else {
		size = parseInt(size);
		//alert (isNaN(0));
		if(isNaN(size)) {
			currentSizeIndex = defaultSizeIndex;
		} else {
			currentSizeIndex = size;
		}
	}
	setTextSize(currentSizeIndex);
}


function textSize(direction) {
	if(direction == 'up') {
		setTextSize(currentSizeIndex + 1);
	} else if(direction == 'down') {
		setTextSize(currentSizeIndex - 1);
	}
}

function setTextSize( sizeIndex ) {
	if (sizeIndex > textSizeArray.length - 1) {
		sizeIndex = 0;
	} else if (sizeIndex < 0) {
		sizeIndex = 0;
	}
	var textContainer = false;
	var textContainer = $('#' + containerDiv);
	if (textContainer.length > 0) {
	    textContainer.css('font-size', textSizeArray[sizeIndex] + 'px');	
        currentSizeIndex = sizeIndex;
    	document.cookie = "textSizeCookie=" + sizeIndex + "; 0; path=/";
        $('.textSize a.active').removeClass("active");
        $($('.textSize a')[sizeIndex]).addClass("active");
	}
}

function getCookieValue(cookieName) {
    var cookieVal = '';
    var startIndex = document.cookie.indexOf(cookieName+"=");
    
    if (startIndex != -1) {    // if cookie has been set...
    
        startIndex += cookieName.length + 1;     // start index is now after the cookie name and = sign
        var endIndex = document.cookie.indexOf(";", startIndex);

        if (endIndex == -1) {   // if cookie value is the last in the list, make endIndex the end of the string
            endIndex = document.cookie.length;
        }
        
        cookieVal = document.cookie.substring(startIndex, endIndex);
    
    } else {    // cookie has not been set
        cookieVal = null;
    }
    
    return cookieVal;
};

var formValidation = {
    fieldFocusID: null,
    validateField: function(arrValidatorIDs, mapIDs) {
        var isValid = true;
        if (arrValidatorIDs != undefined) {
            for (var i = 0; i < arrValidatorIDs.length; i++) {
                var validator = document.getElementById(arrValidatorIDs[i]);
                if (validator.enabled == undefined || validator.enabled == true) {
                    ValidatorValidate(validator);
                    if (validator.isvalid == false) {
                        isValid = false;
                        if (this.fieldFocusID == null) {
                            this.fieldFocusID = validator.controltovalidate;
                        }
                        break;
                    }
                }
            }
        }
        if (mapIDs != undefined) {
            if (mapIDs.checkID != undefined) {
                if (isValid) {
                    $("#" + mapIDs.checkID).removeClass("errorOn");
                } else {
                    $("#" + mapIDs.checkID).addClass("errorOn");
                }
            }
        }
        return isValid;
    },
    validate: function(mapValidator) {
        var isValid = true;
        this.fieldFocusID = null;
        for (var memName in mapValidator) {
            var memObj = mapValidator[memName];
            if (typeof (memObj) == "function") {
                var flag = memObj();
                if (flag == false)
                    isValid = false;
            }
        }
        if (!isValid) {
            $("#" + this.fieldFocusID).focus();
        }
        return isValid;
    }
};

var getHelp = {
    position: function (questionMark) {
        
        $("div.answer").hide();

        //var pos = $(questionMark).css("left");
        var pos = $(questionMark).position();

        var answer = $(questionMark).parent().find("div.answer");
        //$(answer).css("left", pos);
        $(answer).css("left", pos.left + 'px');

        $(answer).fadeIn();
        return true;
    },
    close: function (help) {
        $(help).parent().fadeOut();
        return true;
    }
};

function mainInit() {
  //initISI();
  initUtility();
  $('a.ctaLink').append(' &raquo;');
}
$(document).ready(mainInit);
