/**
 * Extends The jTemplate Plugin
 *
 * @param	string - location of template file
 * @param	mixed - variable to be parsed to template
 * @return		string - the template output
 **/
jQuery.extend({
	processTemplateURLCache : new Array() ,
	processTemplateURL : function(url , vars) {
		if (typeof $.processTemplateURLCache[url] === 'undefined') {
			$.processTemplateURLCache[url] = $.fileGetContents(url);
		}
		var T = $.createTemplate($.processTemplateURLCache[url], [] , {filter_data: false});
		return $.processTemplateToText(T , vars);
	}
});

/**
 * Get Contents From URL
 *
 * @param	string - location of file
 * @return		string - contents of file
 **/
jQuery.extend({
	fileGetContents : function(file , params , exOptions) {
		var options = {
			data  : (typeof params === 'undefined') ? {} : params ,
			dataType : 'text' 
		};
		var responseData;
		$.extend(options , exOptions , { 
			type : 'GET' ,		// ตรงนี้เป็น required options ห้ามตั้งค่าอื่้นนอกจากน
			url : file ,
			async : false ,
			success : function(data) {
				responseData = data;
			}
		});
		try {
			$.ajax(options);
		}
		catch(e)
		{
			alert(YAHOO.lang.dump(e));
		}
		return responseData;
	}
});

jQuery.extend({
	xJSON : function(url , params , method)
	{
		var json = {};
		var def = {
			url : '' ,
			params : {} ,
			method : 'GET'
		};
		$.extend(def, {url:url , params:params , method:method});
		if (typeof url == 'string' && url !== '')
		{
			$.ajax({
				url : def.url ,
				data : def.params ,
				type : (def.method != 'GET') ? 'POST' : 'GET' ,
				dataType : 'text' ,
				async : false ,
				success : function(data)
				{
					try {
						json = eval('('+data+')');
					} catch(e) {
						print_r(data);	// ie จะ error ตรงนี้
						return ;		// ie จบตรงนี้
					}

					if (typeof json !== 'object')
					{
						print_r(data);
					}
				}
			});
		}
		return json;
	}
});

/**
 * $.jInclude jQuery Plugin ใช้โหลด javascript เข้ามาใช้แต่จะโหลดแค่ครั้งเดียวใน 1 หน้า
 * ควรเรียกในฟังก์ชั่น ไม่ควรอยู่ระดับนอกสุด เพราะจะไม่มีประโยชน์
 * 
 * @param	string url ของที่อยู่ไฟล์ javascript
 * @return		void
 **/

$.jInclude = function (url)
{
	// เช็คตัวแปรที่บันทึก flag url ว่าโหลดไปแล้วหรือยัง , โหลดแล้วก็จะข้ามไปเลย
	if (typeof $.jIncludeIdentifier === 'undefined') $.jIncludeIdentifier = new Array();
	if (typeof $.jIncludeIdentifier[url] !== 'undefined') return; // already loaded before

	// ระบุประเภทงานที่กำลังจะโหลดว่าเป็น javascript หรือ css หรืออื่นๆก็จะ error
	var type = '';
	if (url.match(/\.js$/i))
	{
		type = 'js';
	}
	else if (url.match(/.css$/i))
	{
		$('head').append('<link rel="stylesheet" type="text/css" href="'+ url +'" />');

		// flag ว่า url นี้โหลดไปแล้ว
		$.jIncludeIdentifier[url] = true;
		return;
	}
	else
	{
		alert(url + ' - jInclude ไม่รองรับการโหลดไฟล์ประเภทนี้');
		return; // ไม่ใช่แบบที่สามารถ include ได้
	}

	// เริ่มดาว์นโหลดไฟล์
	$.ajax({
		url : url ,
		dataType : 'text' ,

		// อนุญาติให้โหลดจาก browser cache ถ้ามี
		cache : true ,

		// pause สคริปท์จนกว่าจะดาว์นโหลดเสร็จ
		async : false ,

		// โหลดไฟล์สำเร็จก็จับใส่เพจตามประเภทของไฟล์
		success : function (data)
		{
			 // เป็น javascript
			if (type == 'js')
			{
				eval(data); // evaluate response ให้เป็น javascript
			}

			// flag ว่า url นี้โหลดไปแล้ว
			$.jIncludeIdentifier[url] = true;
		} , 

		// ไม่พบไฟล์
		error : function ()
		{
			alert('jInclude ไม่สามารถโหลดไฟล์ ' + url + ' ได้');
		}
	});
};

$.log = function(t , s)
{
	s = s || '';
	t = t || '';
	typeof window.console !== 'undefined' && t !== '' && console.log(t + ' : \n\n' + s);
};

function print_r(obj)
{
	// $.jInclude('/framework/jquery/plugin/dump/jquery.dump.js');
	$.log('Object Dump' , $.dump(obj));
}


function recycleOddEven()
{
	var i = 'odd';
	$('table.listing-table tbody tr').each(function(index){
		$(this).removeClass('odd even').addClass(i);
		if (i == 'odd') i = 'even';
		else i = 'odd';
	});
}