/**
 * @author aprian
 */


/* for empty content, set height of #main-body & #left to make it full with the browser area */
function bodyHeight() {
	var containerLength = $("#container").height();
	var bodyLength = $("body").height();
	
	if(bodyLength > containerLength) {
		var mh = $("div#main-head").height();
		var msh = $("div#main-subhead").height();
		var bm = $("#body-menu").height();
		var cml = $("#community-login").height()? $("#community-login").height(): 0;
		
		var mbLength = parseFloat(bodyLength) - parseFloat(msh) - parseFloat(mh) - 8;
		var lLength = mbLength - parseFloat(bm) - parseFloat(cml) - 45;

		/* set css */
		$("div#main-body").css({"min-height":mbLength,'height':mbLength});	
		$("div#left").css("height",lLength);
		//$("div#partners").css({"margin-top":0,"position":"absolute","bottom":0});
	} else {
		var mc = $("div#main-content").height();
		var l = $("div#left").height();

		if (mc > l) {
			$("div#left").css("height",mc);
			//$("div#partners").css({"margin-top":0,"position":"absolute","bottom":0});
		}
	}
}

/* Give zebra striped color for a table on mouse hover
 * class: odd
 * description: It will create "tr.odd td". Give style for class 'odd' in your stylesheet
 */

(function($){
	$.fn.zebraColor = function(){
		return this.each(function(){
			var obj = $($(this).find('tbody tr').get()); // get all <tr> as jQuery obj
			obj.each(function(){
				$(this).hover(function(){
					$(this).addClass('odd');
				}, function(){
					$(this).removeClass('odd');
				});
			})
		});
	};	
})(jQuery);

/* change background color of a table row if mouse is over on it */
function rowColor(objs){
	var obj = objs.split(',');
	$(obj).each(function(i){
		el = obj[i];
		$("#" + el + " td").hover(function(){
			$(this).parents("tr").addClass('odd');
		}, function(){
			$(this).parents("tr").removeClass('odd');
		})
	})
}

/* make stripped color for a list */
(function($){
	$.fn.stripped = function(){
		return this.each(function(){
			$(this).find("li:odd").addClass('odd');
		});
	};
})(jQuery)

/*
 * CLOSE BUTTON
 * Bind close function for element with ID btn_close (#btn_close)
 * ID : #btn_close
*/

function closePopup() {
	$("input#btn_close").each( function() {
		$(this).click(function() {
			self.close();
		});
	});
}

/*
 * POPUP WINDOW
 * Bind open new window function for element with ID openPopup (#openPopup)
 * Class: openPopup pop_[Width]x[Height]
 * To create popup 800x600, use class: openPopup pop_800x600
*/

function openPopup() {
	$("a.openPopup").each( function() {
		$(this).click(function() {
			
			var winpop;
			var xurl = $(this).attr('href');
			var xtarget = '_blank';
			var thisClass = $(this).attr('class').split(' ');
			var re = new RegExp('^(pop_)');
			for(i=0;i<thisClass.length;i++) {
				if(thisClass[i].match(re)) {
					popSizes = thisClass[i].split('_');
					popSize = popSizes[1].split('x');
					popWidth = popSize[0];
					popHeight = popSize[1];
				}
			}
			
			winpop = window.open(xurl, xtarget,'width=' + popWidth + ',height=' + popHeight + ',resizable=0,scrollbars=yes,menubar=no,status=no' );
			winpop.focus();
			return false;
		});
	});
}

/*
 * ADD TO FAVORITES
 * Class: bookmark
*/

function bookmark() {
	
	var sUrl = location.href;
	var sTitle = document.title;

	$(".bookmark").click(function(){
		if($.browser.msie) {
			window.external.AddFavorite(sUrl, sTitle); // IE/Win
		} else if($.browser.mozilla) {
			$(this).attr("title",sTitle);
		} else if($.browser.opera) {
			void(0);
		} else if($.browser.safari) {
			alert('You need to press CTRL/Cmd + D to bookmark our site.');	
		} else {
			alert('In order to bookmark this site you need to do so manually through your browser.');
		}
	});

}

/*
 * OPEN IN NEW WINDOW ON CLASS OUTSIDE
 * Class: outside
 */
function goOutside(){
	$(".outside").click(function(){
		var sUrl = $(this).attr('href');
		window.open(sUrl);
		return false;
	});
}

/*
 * 
 * FONT RESIZER
 * Class: resize_[font size]
 * Example: <a href="" class="resize_12">Font size 12px</a>
 * Required: jquery.cookie.js
 */
(function($){
	
	$.fn.textResize = function(options){
		
		return this.each(function(){
			
			var divResizer = $(this);
			var resizer = divResizer.find("a[class^=resize_]");
			
			$(resizer).each(function(){
			
				var thisElm = $(this)
				
				thisElm.click(function(){
					
					/* set some variable */
					var thisClass = $(this).attr('class');
					var cssLink = $('link.cssResize');
					var cssFile = $('<link rel="stylesheet" class="cssResize" href="/assets/css/' + thisClass + '.css" />');
					
					/* *** if not normal font size *** */
					if(thisClass !== 'resize_normal'){
						
						/* if link.cssResize exist, then delete before append a new one as approriate */
						if(cssLink.size() > 0){
							
							/* remove link.cssResize */
							$('link.cssResize').remove();
							
						}
							
						/* append css file to the body */
						$("body").append(cssFile);
						
						/* create cookie */
					 	$.cookie('resize', thisClass, { path: '/', expires: 1 });
										
						return false;
						
					}
					
					/* *** for normal font size, remove link.resize and clear the cookie *** */
					else {
						
						/* remove link.cssResize */
						$('link.cssResize').remove();
						
						/* append css file to the body */
						$("body").append(cssFile);
						
						/* remove cookie */
						$.cookie('resize', null, {path: '/'});
						
						return false;
							
					}
					
				});
				  
			});
		})	
	}
		
})(jQuery);

$(document).ready( function() {
	bodyHeight();
	openPopup();
	bookmark();
	goOutside();
	$("#text-resize").textResize();
})
