var oUri = {
	base : burl + 'tv/',
	interval : null,
	curent : '',
	last : ''
};
var oList = {
	_act : null,
	_id : null,
	_key : 'name',
	_dir : 'asc',
	_page : 1
};
var oDynList = {
	_page : 1
};
var oPlay = {
	_id : null
};
var oFlag = {
	isPlayed : false,
	isLoaded : false
};

$(function() {
	$.postJSON = function(url, data, callback) {
		$.post(url, data, callback, "json");
	};

	oUri.interval = setInterval('route()', 100);

	doDynLoad();

	$('#paging a').live('click', function() {
		var p = $(this).attr('rel');
		page(p);

		return false;
	});

	$('#dyn-paging a').live('click', function() {
		var p = $(this).attr('rel');
		dyn_page(p);

		return false;
	});

	$('.star-rating a').live('click', function() {
		var value = $(this).attr('rel');
		var req = oUri.base + 'rate/' + oPlay._id + '/' + value;
		$.postJSON(req, {}, function(data) {
			$('#media-rating ul.star-rating').html(data);
		});

		return false;
	});

	$('#media-refresh').live('click', function() {
		var id = $(this).attr('rel');
		oPlay._id = null;
		play(id);

		return false;
	});

	$('#media-favourite').live('click', function() {
		var id = $(this).attr('rel');
		alert('Saved to Cookie');

		return false;
	});

	$('#media-error').live('click', function() {
		var id = $(this).attr('rel');
		alert('Report to webmaster successfuly. Thank you');

		return false;
	});
});

function route() {
	oUri.curent = window.location.href;
	if (oUri.curent != oUri.last) {
		oUri.last = oUri.curent;
		var aParam = explode('#', oUri.last);
		if (aParam[1] != null) {
			var aGetParam = explode('/', aParam[1]);
			var aRequestParam = explode('_', aGetParam[0]);
			var action = aRequestParam[0];
			if (action == 'category' || action == 'country' || action == 'view') {
				if (aRequestParam[1] == null || aRequestParam[1] == '') {
					alert('System Alert: \n-Code:ER-01\n-Message: request URI error.');
					window.location.href = burl + '#home';
				}
			}
			process(aRequestParam);
		} else {
			window.location.href = burl + '#home';
		}
	}
}

function process(req) {
	var act = req[0];
	var id = (req[1] != null) ? $.base64Decode(req[1]) : null;
	var key = (req[2] != null) ? req[2] : 'name';
	var dir = (req[3] != null) ? req[3] : 'asc';
	var page = (req[4] != null) ? req[4] : 1;
	switch (act) {
	case 'category':
	case 'country':
	case 'home':
		getLoad(act, id, key, dir, page);
		break;
	case 'search':

		break;
	case 'play':
		play(id);
		break;
	case 'help':

		break;
	default:
		window.location.href = burl + '#home';
		break;
	}

	if (oList._act == null) {
		oList._act = 'home';
		doLoad();
	}

	if (oPlay._id == null) {
		setDynChannel();
	}
}

function setDynChannel() {
	$.postJSON(oUri.base + 'auto_channel', {}, function(data) {
		play(data);
	})
}

function getLoad(act, id, key, dir, page) {
	var oGet = {
		_act : act,
		_id : id,
		_key : key,
		_dir : dir,
		_page : page
	};
	if (compareObject(oGet, oList) == false) {
		$.extend(oList, oGet);
		doLoad();
	}
}

function doLoad() {
	var req = oUri.base;
	req += oList._act + '/';
	req += (oList._act != 'home') ? oList._id + '/' : '';
	req += oList._key + '/';
	req += oList._dir + '/';
	req += oList._page + '/';
	// log(req);

	oFlag.isLoaded = true;

	$.postJSON(req, {}, function(json) {
		sortCss(oList._key);
		$('#titler').text(json.title);
		$('#paging-info').html(json.paging_info);
		$('#paging').html(json.paging);
		$('#l2').html(json.l2);
	});
}

function play(id) {
	if (id != oPlay._id) {
		oPlay._id = id;
		var req = oUri.base + 'play/' + oPlay._id;

		$.postJSON(req, {}, function(data) {
			$('#media-box').html(data);
		});
	}
}

function page(page) {
	oList._page = page;
	doLoad();
}

function sort(key) {
	if (key == oList._key) {
		oList._dir = (oList._dir == 'asc') ? 'desc' : 'asc';
	} else {
		oList._key = key;
		oList._dir = 'asc';
		sortCss(key);
	}
	doLoad();
}

function sortCss(id) {
	$('#sort-nav a').removeClass('sortedby');
	$("a[rel='" + id + "']").addClass('sortedby');
}

/** ********************* DYn LoAd *********************** */
function doDynLoad() {
	var req = oUri.base + 'auto_locate/' + oDynList._page;

	$.postJSON(req, {}, function(json) {
		$('#dyn-titler').text(json.title);
		$('#dyn-count').text(json.total);
		$('#dyn-paging-info').html(json.paging_info);
		$('#dyn-paging').html(json.paging);
		$('#l1').html(json.l1);
	});
}

function dyn_page(page) {
	oDynList._page = page;
	doDynLoad();
}

function log(message) {
	console.log(message);
}

function explode(delimiter, string) {
	return string.split(delimiter);
}
function compareObject(o1, o2) {
	for ( var p in o1) {
		if (o1[p] !== o2[p]) {
			return false;
		}
	}
	for ( var p in o2) {
		if (o1[p] !== o2[p]) {
			return false;
		}
	}
	return true;
}