/**
 * Jimmygraphic Javascript Core v1.0
 * 
 * Copyright (c) 2009 Jimmygraphic [www.jimmygraphic.com]
 * 
 * @uses		jQuery
 * @version 	1.0.0
 * @copyright 	(c) 2009. All rights reserved
 * @author 		Raymond [ray58750034 at gmail dot com]
 */

var jm_version = "1.0";

/**
 * 
 * @param script
 * @param isfile
 * @param callback 
 * 			result - "ok" for successful load js
 * 					 "exist" for js already exist
 * @return
 */
function includeJS(script, callback, isfile){
	if(typeof isfile == 'undefined') isfile = true;
	var flag = false;
	/*
	$('script').each(function(i){
		if(flag)return;
		if((isfile && $(this).attr('src') == script) ||
				$(this).text() == script){
			if(typeof callback != 'undefined')
				callback("exist");
			flag = true;
			return;
		}
	});*/
	
	if(!flag){
		var scriptElement;
		scriptElement = document.createElement('script');
		scriptElement.type = 'text/javascript';
		
		if(isfile){
			scriptElement.src = script;
		}else{
			var scriptText = document.createTextNode(script);
			scriptElement.appendChild(scriptText);
		}
		$('head')[0].appendChild(scriptElement);
		
		/*
		scriptElement.onreadystatechange = function(){
			if(scriptElement.readyState == 'complete' && typeof callback != 'undefined'){
				callback("ok");
			}
		};
		scriptElement.onload = function(){
			if(typeof callback != 'undefined')
				callback("ok");
		};*/
	}
	return !flag;
};

function includeCSS(style, callback, title, isfile, media, alternate){
	if(typeof isfile == 'undefined') isfile = true;
	if(typeof media == 'undefined') media = 'all';
	if(typeof alternate == 'undefined') alternate = false;	
	var flag = false;
	/*
	$('link').each(function(i){
		if(flag)return;
		if((isfile && $(this).attr('href') == style) ||
				$(this).text() == style){
			if(typeof callback != 'undefined')
				callback("exist");
			flag = true;
			return;
		}
	});*/
		
	if(!flag){
		if(isfile){	
			var linkElement;
			linkElement = document.createElement('link');		
			linkElement.rel = alternate?'alternate stylesheet':'stylesheet';
			linkElement.type = 'text/css';
			linkElement.href = style;
			linkElement.media= media;
			$('head')[0].appendChild(linkElement);

			//linkElement.onreadystatechange = function(){
				//if(linkElement.readyState == 'complete'){
			if(typeof callback != 'undefined')
					callback("ok");
				//}
			//};
			//linkElement.onload = function(){
				//callback("ok");
			//};
		}else{
			var styleElement;
			styleElement = document.createElement('style');
			styleElement.type = 'text/css';
			styleElement.media = media;
			styleElement.title = title;
			
			var styleText = document.createTextNode(style);
			styleElement.appendChild(styleText);
			$('head')[0].appendChild(styleElement);
		}
	}
	return !flag;
};

/**
 * @uses JimmyDom
 */
function reload(url, params){
	if(typeof(params) != "undefined"){
		var inputs = [];
		for(var p in params){
			inputs.push('input');
			inputs.push({type:'hidden', name:p, value:params[p]});
			inputs.push(null);
		}
		
		var fm = $('body').createAppend(
				'form', {action: url, method: 'post'}, inputs);
		
		fm.submit();
	}else if(typeof(url) != "undefined"){
		window.location = url;
	}else{	
		window.location.reload(true);
	}
};

function isDefined(variable){
	return !(typeof(window[variable]) == "undefined");
};
