﻿/*
	$.fixSrc(selector)
*/
(function ($) {
	$.fixSrc = function (selector) {
		var s, p;
		$(selector + ' img[src]').each(function (intIndex) {
			s = $(this).attr('src');
			p = s.indexOf('media/');
			if (p !== -1) {
				$(this).attr('src', httpRoot = 'media/' + s.substring(p + 6));
			}
		});
	};
})(jQuery);
/*
	$.getCookie(name, defaultValue)
*/
(function ($) {
	$.getCookie = function (name, defaultValue) {
		var re = new RegExp(name + "=[^;]+", "i"); //construct RE to search for target name/value pair
		if (document.cookie.match(re)) { //if cookie found
			return document.cookie.match(re)[0].split("=")[1]; //return its value
		}
		return defaultValue;
	};
})(jQuery);
/*
	$.setCookie(name, value)
*/
(function ($) {
	$.setCookie = function (name, value) {
		document.cookie = name + "=" + value + "; path=/";
	};
})(jQuery);
/*
	$(elements).butonize(path, normalImg, hoverImg, pushedImg)
*/
(function ($) {
	$.fn.buttonize = function (path, normalImg, hoverImg, pushedImg) {
		this.filter('img'); // only for images 
		this.hoverIntent(function () {
			this.src = path + hoverImg;
		},
		function () {
			this.src = path + normalImg;
		}).mousedown(function () {
			this.src = path + pushedImg;
		}).mouseup(function () {
			this.src = path + hoverImg;
		});
		return this; // keep the chain going
	};
})(jQuery);
/*
	$.newsfeed(options)
*/
(function ($) {
	var s;
	$.fn.newsfeed = function (callerOptions) {
		return this.each(function () {
			s = $.extend({
				body: '',
				source: '',
				/* scroll speed */
				speed: 2500,
				/* initial delay before rotating */
				pause: 7000,
				/* delay before rotating */
				delay: 7000,
				/* mouseout delay */
				resume: 250
			},
			callerOptions || {});
			s.newsfeed = $(s.newsfeed);
			s.offset = $(this).height() + 10;
			var rotate = function () {
				if (!s.rotating) {
					s.timeout = false;
					s.rotating = true;
					s.curItem = (s.oldItem + 1) % s.count;
					$(s.items + ':eq(' + s.oldItem + ')').animate({
						top: -s.offset
					},
					s.speed, function () {
						$(this).css('top', s.offset);
					});
					$(s.items + ':eq(' + s.curItem + ')').animate({
						top: 0
					},
					s.speed, function () {
						s.rotating = false;
						if (!s.timeout) {
							s.timeout = setTimeout(rotate, s.pause);
						}
					});
					s.oldItem = s.curItem;
					$.setCookie('newsfeed', s.curItem);
				}
			};
			//			function() {
			s.oldItem = 0;
			s.timeout = false;
			s.rotating = false;
			s.items = 'div#news-feed > div.news-item';
			s.offset = $('div#news-feed').height() + 10;
			s.count = $(s.items).length;
			s.curItem = parseInt($.getCookie('newsfeed', 0), 10);
			if(s.curItem < 0) s.curItem = 0;
			if(isNaN(s.curItem)) s.curItem = 0;
 			if(s.curItem >= s.count) s.curItem = 0;
			s.oldItem = s.curItem;
			$('img.read-more').buttonize(httpRoot + 'media/preload/', 'more-n.gif', 'more-h.gif', 'more-p.gif');
			$(s.items + ':eq(' + s.curItem + ')').css('top', '0');
			s.timout = setTimeout(rotate, s.delay);
			$(this).hoverIntent(function () {
				if (s.timeout) {
					clearTimeout(s.timeout);
					s.timeout = false;
				}
			},
			function () {
				if (!s.timeout) {
					s.timeout = setTimeout(rotate, s.resume);
				}
			});
			//			});
		});
	};
})(jQuery);
/*
	$(elements).borderRadius(radius);
*/
(function($){
	$.fn.borderRadius = function(radius) {
		return $(this).each(function(index) {
			switch($.layout.name) {
				case 'khtml':
					$(this).css('-khtml-border-radius', radius +'px');
					break;
				case 'gecko':
					$(this).css('-moz-border-radius', radius +'px');
					break;
				case 'presto':
					break;
				case 'webkit':
					$(this).css('-webkit-border-radius', radius +'px');
					break;
				case 'presto':
					break;
				case 'trident':
					break;
				default:	
			}
		});
	};
})(jQuery);
/*
	$.viewport()
*/
(function($) {
	var v;
	$.viewport = function() {
		var w, h, t = $(window).scrollTop(), l = $(window).scrollLeft();
		if(!v){
			v = $('<div style="top:0px; left:0px; width:100%; height:100%; position:absolute; background:transparent"></div>').prependTo('body');
		}
		w = $(v).width(), h = $(v).height();
		return {top: t, left: l, width: w, height: h, right: l + w, bottom: t + h };
	};
})(jQuery);
/*
	$(elements).tooltip({
		options
	})
*/
(function($){
	var sDiv, tDiv;
	function show(e) {
		var t, l, w, h, x = 0, y = 0, v = $.viewport();
		sDiv.css('width', tDiv.width() + 'px');
		sDiv.css('height', tDiv.height() + 'px');
		tDiv.css('min-width', tDiv.width() + 'px');
		w = sDiv.outerWidth(), h = sDiv.outerHeight();
		if(!$.browser.msie){
			x = o.widths[1];
			y = o.widths[2]
		}
		if(o.debug){
			t = v.bottom - h - y, l = v.right - w -x
		} else {
			t = e.pageY + o.yTooltip, l = e.pageX + o.xTooltip;
		}	
		if(t + h + x > v.bottom) t = e.pageY - h - x;
		if(t < v.top) t = v.top;
		if(l + w + y > v.right) l = e.pageX - w - y;
		if(l < v.left) l = v.left;
		if($.browser.msie){
			sDiv.css('top', t + 'px').css('left', l + 'px').css('visibility', 'visible');
			t = t + o.widths[0];
			l = l + o.widths[3];
		}
		tDiv.css('top', t + 'px').css('left', l + 'px').css('visibility', 'visible');
	}
	function over(e){
		o  = $(this).data('options');
		sDiv.css('z-index', o.zIndex -1).attr('class', o.styleClass);
		tDiv.css('z-index', o.zIndex).attr('class', o.styleClass).css('max-width', o.maxWidth +'px');
		tDiv.empty().html($(this).data('tooltip'));
		sDiv.css('border-color', o.color);
		sDiv.css('background-color', o.color);
		if(o.shadows){
			if($.browser.msie){
				sDiv.css(o.shadowCSS, o.shadowText[1]);
			}else{
				tDiv.css(o.shadowCSS, o.shadowText[1]);
			}
		}
		if(o.borders) $(tDiv).borderRadius(o.borderRadius);
		show(e);
	};
	function out(e){
		sDiv.css('border', tDiv.css('border')); 
		sDiv.css('background', tDiv.css('background')); 
		sDiv.css('visibility', 'hidden').css('left', '0px').css('top', '0px');
		tDiv.css('visibility', 'hidden').css('left', '0px').css('top', '0px').css('min-width', '0px');
		if(o.shadows) sDiv.css(o.shadowCSS, o.shadowText[0]);
		if(o.borders) $(sDiv).borderRadius(0);
	};
	function init(o){
		var ieText;
		o.shadows = false;
		o.borders = false;
		o.widths = [0, 0, 0, 0];
		o.shadowText = ['', ''];
		if(!tDiv) {
			tDiv = $('<div style="position:absolute; visibility:hidden"></div>').appendTo('body');
			sDiv = $('<div style="position:absolute; visibility:hidden"></div>').appendTo('body');
		}
		function initBorders(){
			if(($.browser.name === 'chrome') && (o.chromeOption !== 'border-radius')) return;
			o.borders = (o.borderRadius > 0);
		}
		function initShadows(){
			if(($.browser.name === 'chrome') && (o.chromeOption !== 'box-shadow')) return;
			if(o.yShadow < 0) o.widths[0] = o.blurRadius - o.yShadow;
			if(o.xShadow > 0) o.widths[1] = o.blurRadius + o.xShadow;
			if(o.yShadow > 0) o.widths[2] = o.blurRadius + o.yShadow;
			if(o.xShadow < 0) o.widths[3] = o.blurRadius - o.xShadow;
			o.shadows = (Math.max(o.widths[0], o.widths[1], o.widths[2], o.widths[3]) > 0);
			if(o.shadows){
				o.shadowText[0] = '0px 0px 0px '+ o.color
				o.shadowText[1] = o.xShadow + 'px ' + o.yShadow + 'px ' +o.blurRadius + 'px ' + o.color
			}
		}
		switch($.layout.name){
			case 'khtml':
				o.shadowCSS = '-khtml-box-shadow';
				initBorders();
				initShadows();
				break;
 			case 'gecko':
				if($.browser.name === 'netscape') break;
				o.shadowCSS = '-moz-box-shadow';
				initBorders();
				initShadows();
				break;
			case 'presto':
				break;
			case 'webkit':
				o.shadowCSS = '-webkit-box-shadow';
				initBorders();
				initShadows();
				break;
			case 'trident':
				o.shadowCSS = 'filter';
				o.shadows = (o.ieRadius > 0);
				ieText = 'progid:DXImageTransform.Microsoft.' + o.ieFilter + '(';
				switch(o.ieFilter){
					case 'Glow':
						ieText = ieText + 'strength=' + o.ieRadius + ',';
						o.shadowText[1] = ieText + 'color=' + o.ieColor + ')';
						o.widths = [o.ieRadius, o.ieRadius, o.ieRadius, o.ieRadius];
						break; 
					case 'Blur':
						ieText = ieText + 'PixelRadius=' + o.ieRadius + ',';
						o.shadowText[1] = ieText + 'color=' + o.ieColor + ')';
						o.widths = [o.ieRadius, o.ieRadius, o.ieRadius, o.ieRadius]; 
						break;
					case 'Shadow':
						ieText = ieText + 'direction=' + o.ieDirection + ',';
						ieText = ieText + 'strength=' + o.ieRadius + ',';
						o.shadowText[1] = ieText + 'color=' + o.ieColor + ')';
						if($.inArray(o.ieDirection, [0, 45, 90, 135, 180, 225, 270, 315]) === -1) o.ieDirection = 135;
						switch(o.ieDirection){
							case 0:
								o.widths[0] = o.ieRadius;
								break;
							case 45:
								o.widths[0] = o.ieRadius, o.widths[1] = o.ieRadius;
								break;
							case 90:
								o.widths[1] = o.ieRadius;
								break;
							case 180:
								o.widths[2] = o.ieRadius;
								break;
							case 225:
								o.widths[3] = o.ieRadius, o.widths[2] = o.ieRadius;
								break;
							case 270:
								o.widths[3] = o.ieRadius;
								break;
							case 315:
								o.widths[0] = o.ieRadius, o.widths[3] = o.ieRadius;
								break;
							case 135:
							default:
								o.widths[1] = o.ieRadius, o.widths[2] = o.ieRadius, o.ieDirection = 135;
						}
						break;
							o.shadows = false;
					default:
				break;						}
			default:
		}		
	};
	$.fn.tooltip = function(options) {
		options = $.extend({
			xTooltip: 		16, // horizontal offset
			yTooltip: 		16, // vertical offset
			debug:			false,
			fadeIn:			0,
			fadeOut:		0,
			duration:		0,
			zIndex:			100,
			maxWidth:		200,
			borderRadius:	10,
			color:			'#888',
			xShadow:		5, // horizontal offset
			yShadow:		5, // vertical offset
			blurRadius:		5,
			chromeOption:	'box-shadow',
			ieFilter:		'Shadow',	// None | Glow | Blur | Shadow
			ieColor:		'#888888',	// no shortcuts - must be 6 digits
			ieRadius:		7,
			ieDirection:	135,	//	0	45	90	135	180	225	270	315
									//	t	tr	r	br	b	bl	l	tl							
			styleClass:		'tooltipDiv'
			},
			options || {}
		);
		init(options);
		return $(this).each(function(index) {
			$(this).data('options', options).data('tooltip', $(this).attr('title')).removeAttr('title') .removeAttr('alt').hoverIntent(over, out);
		});
	};
})(jQuery);
