/*=========================================================


	Site Name   Kaimonoken Blog
	File Name   common.js

	Create Date 2009/10/08
	Update Date 2009/10/08


==========================================================*/

/*----------------------------------------------------------

	1. Common Function (require jquery.js)

-----------------------------------------------------------*/

(function($){

	$(function(){
		$.commonFunc.winopen();
		$.commonFunc.rollover();
		$.commonFunc.fadeimg();
		$.commonFunc.externallink();
		$.commonFunc.smoothscroll();
	});

	$.commonFunc = {

		//new window
		winopen : function(options) {
			var c = $.extend({
				selector:'.winopen'
			}, options);
			$(c.selector).live('click',function(){
				var url = this.href;
				var baseurl = url.substr(0, url.indexOf("?"));
				var query = url.replace(/^[^\?]+\??/,'');
				var params = $.commonFunc.parseQuery(query);

				var baseurl = baseurl;
				var name = params['name'];
				var width = params['width'];
				var height = params['height'];
				var resize = params['resize'];

				var x = (screen.availWidth - width) / 2;
				var y = (screen.availHeight - height) / 2;

				newwin = window.open(baseurl, name, "left=" + x + ", top="+ y +", width="+ width +", height="+ height +", scrollbars="+ resize +", resizable="+ resize);
				return false;
			});
		},

		//new window(full flash)
		winopenfull : function(options) {
			var c = $.extend({
				selector:'.winopenfull'
			}, options);
			$(c.selector).live('click',function(){
				var baseurl = this.href;
				var w = screen.availWidth;
				var h = screen.availHeight;

				window.open(baseurl, "_blank", "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=no, resizable=no, left=0, top=0, width="+ w +", height="+ h);
				return false;
			});
		},

		//rollover
		rollover : function(options){
			var c = $.extend({
				selector:'.btn',
				postfix: '_o'
			}, options);
			var image_cache = new Object();
			$(c.selector).each(function(i){
				var imgsrc = this.src;
				var dot = this.src.lastIndexOf(".");
				var imgsrc_on = this.src.substr(0, dot) + c.postfix + this.src.substr(dot, 4);
				image_cache[this.src] = new Image();
				image_cache[this.src].src = imgsrc_on;
				$(this).hover(
					function(){ this.src = imgsrc_on;},
					function(){ this.src = imgsrc;}
				);
			});
		},

		//fade
		fadeimg : function(options){
			var c = $.extend({
				selector:'.fade',
				active:'.active',
				opacity:'0.3'
			}, options);
			$(c.active).fadeTo(0, c.opacity);
			$(c.selector).live('mouseover',
				function(){$(this).dequeue().fadeTo(300, c.opacity);}
			);
			$(c.selector).live('mouseout',
				function(){$(this).dequeue().fadeTo(300, 1);}
			);
		},

		//external link
		externallink : function(options){
			var c = $.extend({
				selector:'.externalLink'
			}, options);
			var popupEvent = function(event){
				window.open(this.href);
				event.preventDefault();
				event.stopPropagation();
			}
			$(c.selector).each(function(i) {
				$(this).click(popupEvent);
				$(this).keypress(popupEvent);
			});
		},

		//smooth scroll
		smoothscroll : function(options){
			var c = $.extend({
				selector:'.pageLink'
			}, options);
			$(c.selector).click(function() {
				if (location.pathname.replace(/^\//,"") == this.pathname.replace(/^\//,"") && location.hostname == this.hostname) {
					var target = $(this.hash);
					target = target.length && target;
					if (target.length) {
						var sclpos = 0;
						var scldurat = 1000;
						var targetOffset = target.offset().top - sclpos;
						$("html,body")
							.animate({scrollTop:targetOffset},{duration:scldurat,easing:"easeOutExpo"});
						return false;
					}
				}
			});
		},

		parseQuery : function(query){
			var Params = {};
			if (!query){return Params;}
			var Pairs = query.split(/[;&]/);
			for(var i=0;i<Pairs.length;i++ ) {
				var KeyVal = Pairs[i].split("=");
				if(!KeyVal||KeyVal.length!=2){continue;}
				var key = unescape(KeyVal[0]);
				var val = unescape(KeyVal[1]);
				val = val.replace(/\+/g, ' ');
				Params[key] = val;
			}
			return Params;
		},

		getPageSize : function(){
			var de = document.documentElement;
			var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
			var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
			arrayPageSize = [w,h];
			return arrayPageSize;
		}

	}

})(jQuery);

/*----------------------------------------------------------

	2. Extra Function (require jquery.js)

-----------------------------------------------------------*/

(function($){

	$(function(){
		$('body[class!="index"] div.mod_entry:last').css({
			marginBottom : '10px'
		});
		$('body[class="index"] div.mod_entry:last').css({
			marginBottom : '0'
		});
	});

})(jQuery);