var $j = jQuery;
$j(function(){
	var url = location.href;
	pty.testing = url.indexOf('pty_test') > -1;
	pty.now = url.indexOf('pty_open') > -1;
	pty.ePops = pty.eventPopups();
	if(
		!(($j('#pty_nopop').length || pty.m())) 
		|| pty.ePops.length 
		|| (pty.testing || pty.now)
	){
		pty.load(url);
	}
});
pty = {
	load: function(url){
		pty.do_hook('loading');
		var loadPops = '';
		if (pty.ePops.length) {
			loadPops = '&loadPopups='+pty.ePops.join(',');
		}
		var url = PTY_AJAX+'?action=pty_getActive&incoming_ajax=true&url='+location.hash.substr(1)+loadPops;
		var s = document.createElement('script');
		s.id = 'pty_w';
		s.src = url;
		s.text = 'text/javascript';
		document.getElementsByTagName('head')[0].appendChild(s);
	},
	eventPopups: function(){
		var ids = [];
		$j('.pty_click').each(function(){
			var id = pty.getID($j(this));
			if (id !== undefined) {
				ids.push(id);
			}
		});
		return ids;
	},
	getID: function(elm){
		var classes = elm.attr('class').split(' ');
		var id = false;
		$j.each(classes, function(i, v){
			if (v.indexOf('pty_id') > -1) {
				var b = v.split('_');
				id = b[2];
			}
		});
		return id;
	},
	add_hook: function(hook, fnc) {
		if (pty.hooks[hook] == undefined) {
			pty.hooks[hook] = function(){fnc();};
		}
		else {
			var existing = pty.hooks[hook];
			pty.hooks[hook] = function() {existing();fnc();};
		}
	},
	do_hook: function(hook) {
		if (pty.hooks[hook] != undefined) {
			pty.hooks[hook]();	
		}
	},
	m: function(){
		var uagent = navigator.userAgent.toLowerCase();
		var agents = ['ipad', 'iphone', 'ipod', 'android', 'iemobile', 'symbian'];
		var ismobile = false;
		$j.each(agents, function(i, v){
			if (uagent.indexOf(v) > -1) {
				ismobile = true;
			}
		});
		return ismobile;
	},
	hooks: {}
}

