$(document).ready(function() {
    //Start code ******************************************

    //make the tops only round
    jQuery(".topRound a").append("<span class='tl'></span><span class='tr'></span>");
    //make the bottoms only round
    jQuery(".bottomRound a").append("<span class='bl'></span><span class='br'></span>");

    //make tops and bottoms round on solo links
    jQuery(".allRound a").append("<span class='tl'></span><span class='tr'></span><span class='bl'></span><span class='br'></span>");


    //add class of last for the second in a row
    //jQuery(".nestedRoundObjectContainer:odd").addClass("last");

    //target links in jobs table
    jQuery("#pageProfile .jobsTable tr").find("td:eq(0)").addClass("center");
    jQuery("#pageProfile .jobsTable tr").find("td:eq(3)").addClass("position");
    jQuery("#pageProfile .jobsTable tr").find("td:eq(4)").addClass("unpaid");
    jQuery("#pageProfile .jobsTable tr").find("td:eq(5)").addClass("unpaid");
    jQuery("#pageProfile .jobsTable tr").find("td:eq(8)").addClass("center");

    //home page jobs table
    jQuery("#pageHomepage .jobsTable tr").find("td:eq(0)").addClass("first");
    jQuery("#pageHomepage .jobsTable tr").find("td:eq(1)").addClass("position");

    jQuery("#pageFindAJob .jobsTable tr").find("td:eq(0)").addClass("first");
    jQuery("#pageFindAJob .jobsTable tr").find("td:eq(2)").addClass("position");

    jQuery("#pageFindAJobListing .jobsTable tr").find("td:eq(0)").addClass("first");
    jQuery("#pageFindAJobListing .jobsTable tr").find("td:eq(2)").addClass("position");
    jQuery("#pageFindAJobListing .jobsTable tr").find("td:eq(3)").addClass("date");

    //generic zebra
    jQuery(".genericZebra tr:odd").addClass("odd");


    /**
     * form character input counter;
     */
    (function(jq) {
		function Counter(max, input, label){
			this.label = jq('.'+label);
			this.input = jq('.'+input);
			this.max = max;
		}
		
		Counter.prototype.bind = function(){
			var self = this;
			this.input.keypress(function(e) {
				var length = this.value.length;
				// allow for backspaces(keyCode 8);
				if (self.selectedText(this).length == 0) {
					if (length >= this.max && e.keyCode !== 8) {
						return false;
					}
				}
				return true;
			});
			
			this.input.bind('paste', function(e){
				self.update.call(self, this);
				//return false;
			});
			
			this.input.keyup(function(e) {
				if(e.keyCode == 86 && e.ctrlKey){
					return false;
				}
				self.update.call(self, this);
			});
		};
		
		Counter.prototype.update = function(elem){
			var value = elem.value;
			var count = (jq.browser.mozilla || jq.browser.safari) ? 
				value.length + this.lineCount(value) : value.length;
			if (count > this.max) {
				value = value.substring(0, this.max);
			}
			this.setLabel(count);	
		};
		
		Counter.prototype.setLabel = function(count){
			this.label.html("(" + (this.max - count) + " characters)");
		};
		
		Counter.prototype.selectedText = function(el) {
            if (el.selectionStart == undefined) {
                if (document.selection) {
                    return document.selection.createRange().text;
                }
                else {
                    return "";
                }
            }
            else {
                return el.value.substring(el.selectionStart, el.selectionEnd);
            }
        };
        
        Counter.prototype.lineCount = function(str){
			try {
				return((str.match(/[^\n]*\n[^\n]*/gi).length));
			} catch(e) {
				return 0;
			}
		}
		var sumCount = new Counter(100, 'txtSummary', 'charCountSummary');
		sumCount.bind();
		var desCount = new Counter(5000, 'txtDescription', 'charCountSummary2');
		desCount.bind();
    })(jQuery);
    
    
    (function(jq) {
        // PB - 29/01/10 hack to align images in job table nicely;
        var table = jq('#pageHomepage .jobsTable').eq(0);
        jq('img', table).css({visibility: 'hidden'}).each(function() {;
            if (this.complete){
                var el = jq(this);
                var td = el.parents('td').eq(0);
                var h = parseInt(td.siblings('.position').eq(0).outerHeight())-2; 
                var p = Math.floor((h - el.height())/2); 
                el.parent().css({
                    paddingTop: p+'px', height: h - p
                });
                el.css({visibility: 'visible'});      
            };
        });
        
        
    })(jQuery);
});
