jQuery.page = function(options) {
	var instance = new jQuery.Engine(options);
	return instance;
};

jQuery.Engine = function(options) {
	this.settings = jQuery.extend({}, jQuery.Engine.defaults, options);
	this.timeout = null;
};

jQuery.Engine.adv = function() {
	this.size;
	this.index;
	this.name;
	this.userAgent;
	this.sended = false;
			
	this.send = function() {
		if (!this.sended) {
			var sendData = "size="+this.size+"&name="+this.name+"&index="+this.index+"&userAgent="+this.userAgent;
				var msg = $.ajax({
				type: "POST",
				url: "/adv",
				async: false,
				data: sendData
			}).responseText;
					
			this.sended = true;
		}
	}
};

jQuery.extend(jQuery.Engine, {
	
	defaults : {
		increment_template_href: "/template_inc_views.php",
		increment_article_href: "/article_inc_views.php"
	},
	
	prototype: {
	
		doAdv : function() {
			var selIndex = null;
			var ad = new Array();
				
			$("iframe").each(function(i) {
				var n = this.getAttribute("name");
				var src = this.getAttribute("src");
				if (/google_ads/.test(n) || /googlesyndication.com/.test(src)) {
						var obj = new jQuery.Engine.adv();
						try {
							obj.size = this.getAttribute("width") + "x" + this.getAttribute("height");
							obj.index = i;
							obj.name = this.getAttribute("name");
							obj.userAgent = navigator.userAgent.toLowerCase();
						} catch (e) {}
						
						ad[i] = obj;
						if ($.browser.msie) {
							$(this).focus(function(){
								selIndex = i;
							});			
						} else {
							$(this).load(function() {
								this.addEventListener('mouseover', function(e) {
									selIndex = i;
								}, false);
								
								this.addEventListener('mouseout', function(e) {
									selIndex = null;
								}, false);
							});
						}
					} else {
						jQuery("#adv_" + i).hide();
					}
				});
	
			if ($.browser.msie) {
				window.attachEvent('onbeforeunload', function() {
					if (selIndex != null) {
						try {
							ad[selIndex].send();
						} catch (e) { }
					}
				});
			} else {
				window.addEventListener('beforeunload', function(e) {
					if (selIndex != null) {
						try {
							ad[selIndex].send();
						} catch (e) { }
					}
				}, false);
			}
		},
		
		doDeleteUser : function() {
			$('span.icon').click(function() {
				if ($(this).attr('confirm') == 'no') {
					$(this).html('Delete');
					$(this).attr('confirm', 'yes');
				} else {
					$(this).html('Are you sure ? <a href="'+$(this).attr("href")+'">Yes</a> <a href="#comments" onclick="">No</a>');
					$(this).attr('confirm', 'no');
				}
			});
		},
		
		doDeleteResource : function() {
			$('span.icon').click(function() {
				if ($(this).attr('confirm') == 'no') {
					$(this).html('Delete');
					$(this).attr('confirm', 'yes');
				} else {
					$(this).html('Are you sure ? <a href="'+$(this).attr("href")+'">Yes</a> <a href="#comments" onclick="">No</a>');
					$(this).attr('confirm', 'no');
				}
			});
		},
		
		doDeleteComments : function() {
			$('.comment_right').click(function() {
				if ($(this).attr('confirm') == 'no') {
					$(this).html('Delete');
					$(this).attr('confirm', 'yes');
				} else {
					$(this).html('Are you sure ? <a href="'+$(this).attr("href")+'">Yes</a> <a href="#comments" onclick="">No</a>');
					$(this).attr('confirm', 'no');
				}
			});
		},
		
		doAdminEditResource : function() {
			var editresourceadminClass = new jQuery.Engine.editresourceadmin();
			editresourceadminClass.initialize();
		},
		
		doEditItem : function() {
			var editItemClass = new jQuery.Engine.edititem();
			editItemClass.initialize();
		}
	}
});

var page = $.page();

$(function() {
	page.doAdv();
});

