﻿//dependent on EvokeScriptResource.axd?evokeUtility
$(document).ready(function() {
    $(".scrollToISI").each(function() {
        $(this).click(function() {
            $.scrollBodyTo("#footer");
            return false;
        });
    });
}); 

var printPage = function()
{
    window.print();
}

var textSize = (function() {
    var container = "wrapper";
    var currentSize = "1em";
    function persist(size) {
        currentSize = size;
        evokeUtility.setCookie("textSize", size, null);
    }
    function _this() { }
    _this.setFontSize = function(size) {
        var maincol = document.getElementById("mainColContent");
        var rightcol = document.getElementById("rightCol");
        
        if (maincol != null) maincol.style.fontSize = size;
        if (rightcol != null) rightcol.style.fontSize = size;
        $('ul.sliderValues a').css('font-size','1em');
        $('ul.sliderValues a').css('line-height','17px');
        
        
        if (size == "1em") {
            $('div#pageContent div.calloutHomeTop div.calloutContent').height(260);  
            $('div#pageContent div.calloutHomeTall div.calloutContent').height(370);
            $('div#pageContent div.calloutHomeMid div.calloutContent').height(165);
        } else {
            $('div#pageContent div.calloutHomeTop div.calloutContent').height(280);                
            $('div#pageContent div.calloutHomeTall div.calloutContent').height(410);
            $('div#pageContent div.calloutHomeMid div.calloutContent').height(185);
        }
        persist(size);
    };
    _this.small = function() {
        _this.setFontSize("1em");
    };
    _this.medium = function() {
        _this.setFontSize("1.1em");
    };
    _this.large = function() {
        _this.setFontSize("1.2em");
    };
    _this.change = function() {
        if (currentSize == "1em")
            textSize.medium();
        else if (currentSize == "1.1em")
            textSize.large();
        else
            textSize.small();
    }
    return _this;
})();

$(document).ready(function() {
    var cookie = evokeUtility.getCookie("textSize");
    if (cookie != null) {
        textSize.setFontSize(cookie);
    }
});


function openAndTrack(url, trackingFunction) {
    window.open(url);
    trackingFunction();
}

function trackDoubleClick(cat) {
    /*var click1 = new Image();
    var axel = Math.random() + "";
    var a = axel * 10000000000000;
    click1.src = 'https://ad.doubleclick.net/activity;src=2189081;type=intun161;cat=' + cat + ';ord=' + a + '?';
    */
    var pixel = $("#imgDartTracking");
    var url = document.location.protocol + "//ad.doubleclick.net/activity;src=2189081;type=intun161;cat=" + cat + ";ord=" + new Date().getTime() + "?";//;~oref="+encodeURIComponent(document.location.toString())+"
    if (pixel.get().length > 0) {
        pixel.attr("src", url);
    } else {
        $(document.body).append("<img id=\"imgDartTracking\" src=\"" + url + "\" height=\"1\" width=\"1\" border=\"0\" alt=\"\" />");
    }
}

function trackImsPixel(desc) {
    var pixel = $("#imgIMSTracking");
    var url = document.location.protocol + "//shire.imswebmktg.com/shire/pixel/index.php?desc=";
    if (pixel.get().length > 0) {
        pixel.attr("src", url + desc);
    } else {
        $(document.body).append("<img id=\"imgIMSTracking\" src=\""+url+desc+"\" height=\"1\" width=\"1\" border=\"0\" alt=\"\" />");
    }
}

var pageTracker = { //old ga interface
    _trackEvent: function(category, action, label, value) {
        _gaq.push(['_trackEvent', category, action, label, value]);
    },
    _setVar: function(label) {
        _gaq.push(['_setVar', label]);
    },
    _setCustomVar: function(index, name, value, opt_scope) {
        _gaq.push(['_setCustomVar', index, name, value, opt_scope]);
    }
};

var tracker = {
    trackRegistered: function() {
        pageTracker._setVar("Registered");
        pageTracker._setCustomVar(1, "User", "Registered", 1);
    },
    trackSignIn: function() {
        pageTracker._setCustomVar(2, "User", "Authenticated", 2);
    },
    trackEmailFriend: function() {
        pageTracker._trackEvent("Email Friend", "Send URL", document.location.pathname);
    },
    /*trackShare: function(site) {
    tracker.Google.trackEvent("Social Share", site, document.location.pathname);
    },*/
    trackBrandonSlideShow: function(slideNum) {
        pageTracker._trackEvent("Slide Shows", "Brandon", "Slide " + slideNum);
    },
    trackPI: function() {
        trackDoubleClick("fullp087");
    },
    trackLeaflet: function() {
        trackDoubleClick("intun348");
    },
    trackCouponCopay: function() {
        trackDoubleClick("trial111");
    },
    trackCouponVoucher: function() {
        trackDoubleClick("trial504");
    },
    trackCouponFreeTrial: function() {
        trackDoubleClick("freet333");
        pageTracker._trackEvent("Coupons", "Download", "Free Trial Voucher");
        trackImsPixel("intun_coupon");
    },
    trackFAQ: function(map) {
        if (map != undefined && map != null) {
            if (map.cat != undefined)
                trackDoubleClick(map.cat);
            if (map.id != undefined) {
                var id = map.id.split("_");
                pageTracker._trackEvent("FAQs", "Click", id[id.length-1]);
            }
        }
    },
    trackHomeCalloutClick: function(callout, placement) {
        pageTracker._trackEvent("Home Callout Clicks", callout, placement, 1);
    },
    trackHowIntunivHomeCallout: function(placement) {
        this.trackHomeCalloutClick("How Intuniv Works", placement);
    },
    trackFreeTrialHomeCallout: function(placement) {
        this.trackHomeCalloutClick("Free Trial & Support", placement);
    },
    trackDownloadHomeCallout: function(placement) {
        this.trackHomeCalloutClick("Download Your Free Trial", placement);
        this.trackCouponFreeTrial();
    },
    trackSymptomAssessment: function(page) {
        pageTracker._trackEvent("Symptom Assessment", "Page View", page, 1);
    }
};

function toggleShare() {
}

function toggleSignIn(panel, btn, focusInputID) {
    $('.selectedPopupBtn:not(:#' + btn + ')').removeClass("selectedPopupBtn");
    var b = $('#' + btn + ' > a');
    if ($("#" + panel).css("display") != "block") {
        $("#" + panel).css("display", "block");
        b.addClass("selectedPopupBtn");
        if (focusInputID != undefined) {
            document.getElementById(focusInputID).focus();
        }
    } else {
        $("#" + panel).css("display", "none");
        b.removeClass("selectedPopupBtn");
    }
}
function toggleSignInPanel() {
    toggleSignIn('signinPanel', 'headerAccountSignin', undefined);
}
function togglePopup(panel, btn) {
    $('.selectedPopup:not(:#'+panel+')').removeClass("selectedPopup");
    $('.selectedPopupBtn:not(:#'+btn+')').removeClass("selectedPopupBtn");
    
    var p = $('#'+panel);
    var b = $('#'+btn+' > a');
    if (p.hasClass("selectedPopup")) {
        p.removeClass("selectedPopup");
        b.removeClass("selectedPopupBtn");
    } else {
        p.addClass("selectedPopup");
        b.addClass("selectedPopupBtn");
    }
}
function toggleSharePanel() {
}
function toggleEmailPanel() {
    togglePopup('emailPanel','utilEmail');
}

function showHelpTerm(termID, obj) {
    $("#glossaryPopupTitle").html("");
    $("#glossaryPopupText").html("Loading...");
    $("#glossaryPopup").show();
    evokeUtility.attachEvent(document.forms[0].id, "click", closeGlossaryPopup);
    $.getJSON(commonProps.helpTerm, { id: termID },
        function(json) {
            $("#glossaryPopupTitle").html(json.title);
            $("#glossaryPopupText").html(json.text);
            $("#glossaryPopupPlayer").hide();
            $("#glossaryPopup").position({
                of: $(obj),
                my: "left bottom",
                at: "center top",
                offset: "-77 -5"
            });
        }
    );
}

function closeGlossaryPopup() {
    $("#glossaryPopup").hide();
    evokeUtility.detachEvent(document.forms[0].id, "click", closeGlossaryPopup);
}


function showTerm(termID, obj) {
    $("#glossaryPopupTitle").html("");
    $("#glossaryPopupText").html("Loading...");
    
    $("#glossaryPopup").show();
    evokeUtility.attachEvent(document.forms[0].id, "click", closeGlossaryPopup);
    $.getJSON(commonProps.glossaryTerm, { id: termID },
        function(json) {
            $("#glossaryPopupTitle").html(json.title);
            $("#glossaryPopupText").html(json.definition);
            var audiofile = json.audio;
            if (json.audio != "") {
                $("#glossaryPopupText").attr("class", "gpopText");
                $("#glossaryPopupPlayer").show();
                var flashvars = {
                    file: json.audio,
                    skin: commonProps.flpath + "/SkinOverPlayStopSeekMuteVol.swf",
                    video: audiofile
                };
                var params = {
                    wmode: "transparent"
                };
                if (evokeUtility.browser.isIDevice()) {
                    $("#mediaspace").html("<a href=\""+json.audio+"\"><img src=\""+commonProps.glosspath+"/audio-controls.png\" border=\"0\" /></a>");
                } else {
                    swfobject.embedSWF(commonProps.flpath + "/glossaryPlayer-091021.swf", "mediaspace", "280", "36", "8", commonProps.flpath + "/expressInstall.swf", flashvars, params, null);
                }
            } else {
                $("#glossaryPopupPlayer").hide();
                $("#glossaryPopupText").attr("class", "gpopTextNoAudio");
            }
            $("#glossaryPopup").position({
                of: $(obj),
                my: "left bottom",
                at: "center top",
                offset: "-85 -5"
            });
            document.getElementById("glossaryPopup").focus();
        }
    );
}
function hideTerm() {}

function validateFormFields(fieldPropsArray) {
    var flag = true;
    var obj = null;
    for (var i in fieldPropsArray) {
        checkFieldValid(fieldPropsArray[i]);
        if (!fieldPropsArray[i].isValid) {
            flag = false;
            if (obj == null) obj = document.getElementById(fieldPropsArray[i].inputID);
        }
    }
    if (obj != null) {
        obj.focus();
    }
    return flag;
}
function checkFieldValid(props) {
    var wasValidated = false;
    var flag = true;
    var validatorID = null;
    for (var i=0; i<props.validatorIDs.length; i++) {
        validator = document.getElementById(props.validatorIDs[i]);
        if (validator.enabled == undefined || validator.enabled) {
            wasValidated = true;
            ValidatorValidate(validator);
            if (!validator.isvalid) {
                flag = false;
                break;
            }
        }
    }
    if (flag && props.additional != undefined) {
        var newFlag = props.additional();
        if (newFlag != undefined)
            flag = newFlag;
    }
    if (wasValidated) {
        var objBad = document.getElementById(props.badID);
        var objGood = document.getElementById(props.goodID);
        if (flag) {
            if (objBad != null) objBad.style.display = "none";
            if (objGood != null) objGood.style.display = "block";
        } else {
            if (objBad != null) objBad.style.display = "block";
            if (objGood != null) objGood.style.display = "none";
        }
    }
    props.isValid = flag;
}

var formValidation = {
    isFullValidation: false,
    fieldFocusID: null,
    displayIndicators: function(isValid, mapIDs) {
        if (isValid != undefined && mapIDs != undefined) {
            if (mapIDs.bad != undefined) {
                $("#" + mapIDs.bad).css("display", (isValid ? "none" : "block"));
            }
            if (mapIDs.good != undefined) {
                $("#" + mapIDs.good).css("display", (isValid ? "block" : "none"));
            }
        }
    },
    validateField: function(arrValidatorIDs, mapIDs) {
        var isValid = true;
        var wasValidated = false;
        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);
                    wasValidated = true;
                    if (validator.isvalid == false) {
                        isValid = false;
                        if (this.isFullValidation && this.fieldFocusID == null) {
                            if (mapIDs.focusID != undefined) {
                                this.fieldFocusID = mapIDs.focusID;
                            } else if (validator.controltovalidate != undefined) {
                                if (document.getElementById(validator.controltovalidate).tagName.toLowerCase() == "span") {
                                    this.fieldFocusID = $("#" + validator.controltovalidate + " > input:first").attr("id");
                                } else {
                                    this.fieldFocusID = validator.controltovalidate;
                                }
                            }
                        }
                        break;
                    }
                }
            }
        }
        if (wasValidated) this.displayIndicators(isValid, mapIDs);
        return isValid;
    },
    validate: function(mapValidator) {
        this.isFullValidation = true;
        var isValid = true;
        for (var memName in mapValidator) {
            var memObj = mapValidator[memName];
            if (typeof memObj == "function") {
                var flag = memObj();
                if (flag == false)
                    isValid = false;
            }
        }
        this.isFullValidation = false;
        if (this.fieldFocusID != null) {
            $("#" + this.fieldFocusID).focus();
            this.fieldFocusID = null;
        }
        return isValid;
    }
};


function warnExternal(url) {
    document.getElementById("intstURL").href = url;
    evokeModalDialog.show("intstPopup");
}

function showVideoPopup() {
    showModalDialog("videoPopup");
}
var modalTimeout = 0;
function showModalDialogDelay(id, delay) {
    clearTimeout(modalTimeout);
    modalTimeout = window.setTimeout(function() { showModalDialog(id); }, delay);
}
function hideModalDialog() {
    clearTimeout(modalTimeout);
    evokeModalDialog.hide();
}
function showModalDialog(id) {
    evokeModalDialog.show(id, document.forms[0].id);
}

function showConfirmationPopup(objIdWithDisplayContent) {
    $("#confirmationPopupText").html($("#" + objIdWithDisplayContent).html());
    evokeModalDialog.show("confirmationPopup");
}

function showGlossaryPopup(term) {
    evokeModalDialog.show("glossaryPopup");
}

function sifrPageRate() {
    sIFR.replace(vag, {
    selector: '#pageRateText',
        css: '.sIFR-root { font-size:12px; color:#005581; text-align:center;}',
        transparent: true,
        tuneHeight: -5,
        offsetTop: 1
    });
}

function removeWatermark(objID) {
    var obj = document.getElementById(objID);
    if (obj.value == obj.watermark) {
        obj.value = "";
        obj.className = obj.className.replace(" watermark", "");
    }
}
function showWatermark(objID) {
    var obj = document.getElementById(objID);
    if (obj.value.trim() == "") {
        obj.value = obj.watermark;
        obj.className += " watermark";
    }
}

var setBtnClickAreas = function(){

    var i = 0;
    var j = 0;
    var k = 0;
    var m = 0;
    
    var btnArray = new Array();
    $(".btnSifr").each(
        function(i) {        
            btnArray[i] = this.offsetWidth;
            $(this).mouseover(function() {
                $(this).addClass('btnhover');
            });
            $(this).mouseout(function() {
                $(this).removeClass('btnhover');
            });
        }
    );
    
    $(".btnClickArea").each(
        function(j) {       
            this.style.width = btnArray[j]+'px';
            $(this).css({'cursor':'pointer'}); 
        }
    );

    var btnGlowArray = new Array();
    $(".btnSifrGlow").each(
        function(k) {        
            btnGlowArray[k] = this.offsetWidth;
            $(this).mouseover(function() {
                $(this).addClass('btnhover');
            });
            $(this).mouseout(function() {
                $(this).removeClass('btnhover');
            });
        }
    );
    
    $(".btnClickAreaGlow").each(
        function (m) {                   
            this.style.width = btnGlowArray[m]+'px';
            $(this).css({'cursor':'pointer'}); 
        }
    );

}
$(window).load(setBtnClickAreas);


//*** jQuery Extensions
jQuery.scrollBodyTo = function(expression, fn, extraY) {
    var b = window.navigator.userAgent.toLowerCase();
    var jBody = this("html,body");
    var bodyOffset = 0;
    if (/applewebkit/.test(b)) {
        bodyOffset = document.body.scrollTop;
    } else if (/opera/.test(b)) {
        bodyOffset = 0;
    } else {
        bodyOffset = document.documentElement.scrollTop;
    }
    var elOffset = jBody.find(expression).offset().top;
    var y = elOffset - bodyOffset;

    var pageHeight = jQuery(document).height();
    var winHeight = jQuery(window).height();

    if (elOffset + winHeight > pageHeight) {
        y -= (winHeight - (pageHeight-elOffset));
    }

    if (extraY != undefined && extraY != null)
        y += extraY;
    jBody.animate({ scrollTop: '+=' + y + "px" }, "slow", "easeInOutExpo", fn);
};

jQuery.fn.evokeAutoTab = function(objNextExpression) {
    jQuery(this).bind("keydown", function() {
        this.evokeAutoTabLength = jQuery(this).attr("value").length;
    });
    jQuery(this).bind("keyup", function() {
        if (jQuery(this).attr("value").length != this.evokeAutoTabLength) {
            this.evokeAutoTabLength = jQuery(this).attr("value").length;
            if (this.evokeAutoTabLength == jQuery(this).attr("maxlength")) {
                jQuery(objNextExpression).focus();
            }
        }
    });
}


