/**
 * @author aprian
 */

/*
 * 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() {
	openPopup();
	bookmark();
	
})
