/// Feature side scroller
if (!this.TS) { TS = function () { }; }


/* string functions */
TS.string = function () { };
TS.string.emptyDiv = $('<div/>');
TS.string.htmlEncode = function (value) {
    if (value == null) return '';
    return TS.string.emptyDiv.text(value).html();
}
TS.string.htmlQuot = function (value) {
    if (value == null) return '';
    return value.replace('\"', '&quot;');
}
TS.string.htmlDecode = function (value) {
    if (value == null) return '';
    return TS.string.emptyDiv.html(value).text();
}

// validation methods
TS.validation = function () { };

TS.validation.isValid = true;
TS.validation.messages = [];

TS.validation.clear = function () {
    TS.validation.isValid = true;
    TS.validation.messages = [];
}

TS.validation.addFailure = function (msg) {
    TS.validation.isValid = false;
    TS.validation.messages.push(msg);
}

TS.validation.alertFailure = function () {
    var msg = 'Please correct the following issues and resubmit: \n';
    for (var i = 0, ii = TS.validation.messages.length; i < ii; i++) {
        msg += '   - ' + TS.validation.messages[i] + '\n';
    }
    alert(msg);
}

TS.validation.formatFailures = function (messages) {
    var msg = '';
    for (var i = 0, ii = messages.length; i < ii; i++) {
        msg += messages[i] + '<br />';
    }
    return msg;
}

TS.validation.isSupplied = function (id) {
    return jQuery.trim($('#' + id).val()) != '';
}

TS.validation.isChecked = function (id) {
    return $('#' + id).is(':checked');
}

TS.validation.isGroupChecked = function (name) {
    var id = $("input[@name=" + name + "]:checked").attr('id');
    return id != null;
}

TS.validation.checkTextLength = function (id, max_length, min_length) {
    var val = jQuery.trim($('#' + id).val());
    if (val == '') return true;
    if (val.length > max_length) return false;
    if (min_length != undefined) return val.length > min_length;
    return true;
}

TS.validation.isPlainText = function (id, required, min_length, max_length) {
    var val = jQuery.trim($('#' + id).val());
    if (val == '') {
        if (required) return false;
        if (!required) return true;
    }
    if (min_length != undefined) {
        if (val.length < min_length) return false;
    }
    if (max_length != undefined) {
        if (val.length > max_length) return false;
    }
    return TS.validation.isPlainTextValue(val);
}

TS.validation.isPlainTextExtended = function (id, required, min_length, max_length) {
    var val = jQuery.trim($('#' + id).val());
    if (val == '') {
        if (required) return false;
        if (!required) return true;
    }
    if (min_length != undefined) {
        if (val.length < min_length) return false;
    }
    if (max_length != undefined) {
        if (val.length > max_length) return false;
    }
    return TS.validation.isPlainTextExtendedValue(val);
}

TS.validation.isPlainTextValue = function (val) {
    return TS.utils.doRegExp(val, "^[_a-zA-Z0-9 !`+=;:,%&{}\\*\\^\\r\\n\\t\\(\\)\\?\\-\\.\\$\\/\\[\\]'\"‘’“”]*$") != null;
}

TS.validation.isPlainTextExtendedValue = function (val) {
    return TS.utils.doRegExp(val, "^[_a-zA-Z0-9 !`+=;:,%&{}\\*\\^\\r\\n\\t\\(\\)\\?\\-\\.\\$\\/\\[\\]'\"‘’“”À-ÿ°]*$") != null;
}

TS.validation.testTextField = function (id, required, friendly_title, use_extended) {
    var val = jQuery.trim($('#' + id).val());
    if (val == '') {
        if (required) TS.validation.addFailure('\'' + friendly_title + '\' has not been supplied.');
    }
    else {
        var test = use_extended == true ? TS.validation.isPlainTextValue(val) : TS.validation.isPlainTextExtendedValue(val);
        if (!test) TS.validation.addFailure('\'' + friendly_title + '\' contains invalid characters.');
    }
}

TS.validation.isEmail = function (id, required) {
    var val = jQuery.trim($('#' + id).val());
    if (val == '') {
        if (required) return false;
        if (!required) return true;
    }
    return TS.utils.doRegExp(val, "^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$") != null;
}

TS.validation.isPhoneNumber = function (id, required) {
    var val = jQuery.trim($('#' + id).val());
    if (val == '') {
        if (required) return false;
        if (!required) return true;
    }
    return TS.utils.doRegExp(val, "^(\\+)?(\\(?\\d{1,4}\\)?[ -]?)?\\d{1,4}([ -]?\\d{2,4}){1,}$") != null;
}

TS.validation.isAlphaNumeric = function (id, required) {
    var val = jQuery.trim($('#' + id).val());
    if (val == '') {
        if (required) return false;
        if (!required) return true;
    }
    return TS.utils.doRegExp(val, "^[0-9a-zA-Z]{1,}$") != null;
}

/* utility methods */
TS.utils = function () { }
TS.utils.doRegExp = function (value, pattern, ignore_case) {
    var regXp;
    regXp = (ignore_case == true) ? new RegExp(pattern, "i") : new RegExp(pattern);
    regXp.global = true;
    return regXp.exec(value);
}

if (!this.TS.ui) { TS.ui = function () { }; }

TS.ui.dropDownMenu = function(id)
{	
	var me = this;
	this.ID = id;
	
	this.build = function() {		
		var firstMenuOpts = $('#'+ me.ID + ' > ul > li');
		firstMenuOpts.each(function(i) {
			var opt = $(this);			
			var ul = opt.find('ul');
			var isSel = false;
			opt.hover(
				function(e) {
					var f = $(this).children("a:eq(0)");
					if(f[0]) {
						f = f[0];
						isSel = f.className == 'selPerm';
						if(!isSel) f.className = 'selPerm';
					}
					var target = $(this).children("ul:eq(0)");
					target.animate({opacity:'toggle',height:'toggle'},500);
				},
				function() { 
					if(!isSel) {
						var f = $(this).children("a:eq(0)");
						if(f[0]) f[0].className = '';
					}
					var target = $(this).children("ul:eq(0)");
					target.fadeOut();
				});
		});
	}	
}

/* ---------------------------------------------------------------- *\
        Feature Scroller
\* ---------------------------------------------------------------- */
TS.ui.featureScroller = function (wrapId, prevId, nextId, num, width, spd) {
    var me = this;
    this.numFeatures = num || 3;
    this.featureWidth = width || 1000;
    this.speed = spd || 500;
    this.startLeft = 0;
    this.minLeft = 0 - ((this.numFeatures - 1) * this.featureWidth);
    this.id = wrapId;
    this.prevLink = prevId;
    this.nextLink = nextId;
    this.animating = false;
    this.next = function () {
        if (this.animating) return;
        var w = $('#' + this.id);
        var left = parseInt(w.css('margin-left'));
        if (left > this.minLeft && left <= this.startLeft) {
            this.animating = true;
            var amt = '-=' + this.featureWidth;
            w.animate({ marginLeft: amt }, me.speed, 'easeOutCirc', me.complete);            
        }
    };
    this.previous = function () {
        if (this.animating) return;
        var w = $('#' + this.id);
        var left = parseInt(w.css('margin-left'));
        if (left >= this.minLeft && left < this.startLeft) {
            this.animating = true;
            var amt = '+=' + this.featureWidth;
            w.animate({ marginLeft: amt }, me.speed, 'easeOutCirc', me.complete);            
        }
    };
    this.complete = function () {
        me.animating = false;
        var left = parseInt($('#' + me.id).css('margin-left'));
        if (left < me.startLeft) {
            $('#' + me.prevLink).attr('disabled', '');
            $('#' + me.prevLink).removeClass('fPrevDisabled');
        }
        else {
            $('#' + me.prevLink).attr('disabled', 'disabled');
            $('#' + me.prevLink).addClass('fPrevDisabled');
        }
        if (left > me.minLeft) {
            $('#' + me.nextLink).attr('disabled', '');
            $('#' + me.nextLink).removeClass('fNextDisabled');
        }
        else {
            $('#' + me.nextLink).attr('disabled', 'disabled');
            $('#' + me.nextLink).addClass('fNextDisabled');
        }
    };
    me.complete();
}
