
RS = {};

$(function(){
	$('.mailer').mailBit();
	//Sets up matched element pairs for the moreLink Function. if there are indeed moreLinks on the page, this will assign unique id's to each and their corresponding moreText element.
	if($('.moreLink')){		
		$('.moreLink').attr("id", function(e) {return "mLink" + e;}).moreLink('mText');
		$('.moreText').attr("id", function(e) {return "mText" + e;});
	}
	
	
	
	if($('.opinion')){
		var len = $('.opinion').length;
		$('.opinion').attr("id", function(e) {return "opinion" + e;});
		$('#opinion0').addClass('current');
		RS.clikThru('rr', 'ff', 'opinion');
		if(len < 2)$('#clientScr').hide();
	}


	if($('.stfQuote')){
		var len = $('.stfQuote').length;
		$('.stfQuote').attr("id", function(e) {return "stfQuote" + e;});
		$('#stfQuote0').addClass('current');
		RS.clikThru('qr', 'qf', 'stfQuote');
		if(len < 2)$('#staffScr').hide();
	}

	if($('.pEntry')){
		var pW = $('.pEntry').width();
		var pN = $('.pEntry').length;
		var vW = $('#inner_scroll').width() -3;
		var p8 = $.makeArray($('.pEntry:nth-child(8n)'));
		//var p7 = $.makeArray($('.pEntry:nth-child(8n)'));
		//alert(p7);
		if($.browser.msie){var hScrollWid = (pW + 24) * pN;
			}else{
			var hScrollWid = (pW + 12) * pN;
		}
		if($.browser.version != '1.9.0.1' && $.browser.mozilla){
			$('#hor_scroll').addClass('ff2');	
		}
		$('.pEntry').attr("id", function(e) {return "port" + e;} ).ports('port', 'pd', 'fr');
		//$('.pEntry:nth-child(8n)').mapper();
		$('.scrollDrop').attr("id", function(e) { return "pd" + e;} ).slinky('accHead', 'p').mapper(p8);
		$('.accHead').fakeLink('portHover');
		$('.frame').attr("id", function(e) { return "fr" + e;} );
		$('.thumb').attr("id", function(e) { return "th" + e;} );
		//$('.slat').attr("id", function(e) { return "sl" + e;} );
		$('#hor_scroll').css('width', hScrollWid );
		RS.portScroll('scrBack', 'scrFor', parseFloat(hScrollWid), vW);
		$('#pd0').addClass('show'); $('#fr0').addClass('show');
	}
	if($('#teamAjax')){
		$('.teamster').attr('id', function(e){return "salary" +e;});
		$('.hiddenBubble').attr('id', function(e){return "bubble" +e;});
		$('.person').attr('id', function(e){return "named" +e;});
		//$('.teamster').hover(function(e){$('.profiler', this).fadeIn(100); }, function(e){$('.profiler', this).fadeOut(100); }).teamAjax();
		$('.teamster').teamAjax();
	}
	//window.resizeTo(1200, 1050);
		   
});
$.fn.mailBit = function(){	
	return this.each(function(){
		var tld_ = new Array()
		tld_[0] = "com";
		tld_[1] = "org";
		tld_[2] = "net";
		tld_[3] = "ws";
		tld_[4] = "info";
		tld_[10] = "co.uk";
		tld_[11] = "org.uk";
		tld_[12] = "gov.uk";
		tld_[13] = "ac.uk";
		var topDom_ = 13;
		var m_ = "mailto:";
		var a_ = "@";
		var d_ = ".";
		var href = $(this).attr('title').split('/');
		$(this).attr({'href': m_+href[0]+a_+href[1]+d_+tld_[href[2]], 'title': ''});
	});
};
$.fn.fakeLink = function(newClass){
	return this.each(function(){
	$(this).hover(function(e){$(this).addClass(newClass);}, function(e){$(this).removeClass(newClass);});
	});
};
$.fn.fadeLink = function(newClass){
	return this.each(function(){
	$(this).hover(function(e){$('span', this).fadeIn(100);  }, function(e){$('span', this).fadeOut(20);});						  
	});
};

//slightly different from my mooTools implementation in that it will work on an array of elements on the page, attach a click event to each el, the event syncs to the 1st el's matched pair containing the additional text that is initially hidden.
$.fn.moreLink = function(target){
	return this.each(function(e){
		var el = e;	
		
		//alert($(this).text());
		$(this).click(function(e){
			var initText = $(this).text();
			$('#' + target + el).toggle('drop', {direction: 'up'}, 400); 
			if($(this).text() == 'more + -'){
				$(this).text('less + -');
			}else{
				$(this).text('more + -');
			}
		});
	});

};

$.fn.pAcc = function(el, target){
	return this.each(function(e){
				var content = $.makeArray($('p', this));
				var header = $.makeArray($('.'+el, this));
				var h = $('h3', this);
				$('.'+el, this).click(function(){
					$.each(content, function(i, e){$(this).removeClass('active'); });
					$.each(header, function(i, e){$(this).removeClass('active').fakeLink('portHover'); });
					h.fakeLink('portTitleHover');
					$('p', this).addClass('active');
					$(this).addClass('active');
				});
							  
	});
};
$.fn.slinky = function(el, target){
	return this.each(function(e){
				var content = $.makeArray($('p', this));
				var header = $.makeArray($('.'+el, this));
				var h = $('h3', this);
				$('.'+el, this).click(function(){
					$.each(content, function(i, e){$(this).hide().removeClass('active'); });
					$.each(header, function(i, e){$(this).removeClass('active').fakeLink('portHover'); });
					h.fakeLink('portTitleHover');
					$('p', this).show('drop', {direction:'up'}, 400).addClass('active');
					$(this).addClass('active');
				});
							  
	});
};

//this function maps the positions of each element in an array passed in as an argument
//Currently not in use as there are x-browser discrepencies which need to be sorted out before this is reliable.
$.fn.mapper = function(array){
	return this.each(function(e){	 
		$.each(array, function(i, val){
			$('.frame', this).addClass('lastFr').attr({src: '/img/core/green_hoverLeft.gif'});
		});
	});
};


RS.portScroll = function(el1, el2, wid, vW){	
	var fac = wid / vW; 
	var sA = wid / fac;
	var hLeft = $('#hor_scroll').css('marginLeft');
	var i = 0;
		$('#'+el1).click(function(){
                    if(i != 0){
                        if($.browser.msie){ $('#hor_scroll').animate({marginLeft:'+='+sA}, {easing:'swing', duration:320});
                        }else{
                            $('#hor_scroll').animate({marginLeft:'+='+sA}, {easing:'swing', duration:320})
                            .effect('bounce', {distance:5, direction:'left'}, 70)
                        }
                        i--;
                    }
		});
		
		$('#'+el2).click(function(){
			if(i < (fac - 1)){
				if($.browser.msie){ $('#hor_scroll').animate({marginLeft:'-='+sA}, {easing:'swing', duration:320});
				}else{
					$('#hor_scroll').animate({marginLeft:'-='+sA}, {easing:'swing', duration:320})
					.effect('bounce', {distance:5, direction:'left'}, 70)
					}
				i++;
				}
		});
}

// rr = id of the Back Button element
// ff = id of Next Button element
// items = id prefix of elements to scroll through - class should be the same as the prefix in the mark-up (id="el0" class="el")
RS.clikThru = function(rr, ff, items){
			var e = $('.'+items);
			var len = e.length;
			var i = 0;
			$('#' + ff).click(function(){
				$('#' + items + i).hide();
				i = ((++i) % len);
				$('#' + items + i).show('slide', {direction : 'right'}, 200).effect('bounce', {direction: 'left', distance: 3}, 30);
			});
			$('#' + rr).click(function(){
				$('#' + items + i).hide();
				i = ((--i) % len);
				if(i < 0){i = len - 1}
				$('#' + items + i).show('slide', {direction : 'left'}, 200).effect('bounce', {direction: 'right', distance: 3}, 30);
			});
};


$.fn.ports = function(el, drop, frame){
	return this.each(function(e){
		var el = e;
		var on = true;
	$(this).hover(function(){
			$('#th'+el).fadeTo(150, .5);},   
			function(){
				$('#th'+el).fadeTo(150, 1);
				}).click(function(){
					$('#pd0').removeClass('show');
					$('#fr0').removeClass('show');
					if($.browser.msie){
						$('.scrollDrop').hide();
						$('.frame').hide();									   
						$('#' + frame + el).show();
						$('#' + drop + el).show();								   
					}else{
						$('.scrollDrop').hide();
						$('.frame').hide();								   
						$('#' + frame + el).show(150);
						$('#' + drop + el).show('drop', {direction: 'left'}, 250).effect('bounce', {direction:'left', distance: 2}, 50);
					}
           });
	});
};

//initText is the material to be split passed in from the inline page js 
//  Ex: in the jsp   var groupDescription = "${targetGroup.description}";
//	So then initText would be 'groupDescription'.
//shortEl is the element ID where the first half of the shortened string will be sent
//longEl is the element ID where the remainder of the string will go
//len = the character length of the shortEl, defaults to 45 characters 
$.fn.wordWrap = function(initText, shortEl, longEl, len){
	a = initText.split(" ");
	b = new Array();
	b2 = new Array();
	if (!len) len = 45;

	for(var i=0; i<a.length; i++){
		b[i] = a[i];
		c = b.join(' ');
		if(c.length >= len){
			$('#'+shortEl).set("html",c +"... ");
			i = i + 1;
			for(i; i<a.length; i++){
                            b2[i] = a[i];
                            d = b2.join(' ');
                            if(i == a.length - 1){$('#'+longEl).set("html",d);}
			}
		}
	}

};

$.fn.teamAjax = function(){
	return this.each(function(e){
	var diff = $('.hiddenBubble', this).height() + 24;
	var h1 = $(this).height();
	var h3w = $('h3', this).width() + 2;
	//alert(e+' '+$(this).height() +' - '+ $('.hiddenBubble', this).height() +' = '+ diff);
	$('.tiny', this).toggle(function(){if($.browser.msie && $.browser.version == '6.0'){/*$('#named'+e).css({width:145});}else{$('#named'+e).css({width:156});*/}$('#salary' + e).animate({height: diff}, 150, function(){$('#bubble'+e).show('drop', {direction: 'up'}, 250).effect('bounce', {direction:'up', distance: 2, times: 2}, 50);});}, function(){$('#bubble'+e).hide('drop', {direction: 'up'}, 200); $('#salary' + e).animate({height: h1}, 100); /*$('#named'+e).animate({width: h3w}, 100);*/});
	});
}