jQuery.page = function(options) {
	var instance = new jQuery.IfreeProxies(options);
	return instance;
};

jQuery.IfreeProxies = function(options) {
	this.settings = jQuery.extend({}, jQuery.IfreeProxies.defaults, options);
};

jQuery.IfreeProxies.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.IfreeProxies, {
	
	defaults : {
		remove_message_msg: "",
		add_template_like_href: "/template_add_like.php",
		add_article_like_href: "/article_add_like.php",
		template_comments_href: "/template_comments.php",
		img_template_loading: "/img/templateajax.gif",
		img_article_loading: "/img/articleajax.gif",
		increment_script_href: "/script_inc_views.php",
		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) {
					//alert(this.src);
					var n = this.getAttribute("name");
					var src = this.getAttribute("src");
					if (/google_ads/.test(n) || /googlesyndication.com/.test(src)) {
							var obj = new jQuery.IfreeProxies.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);
					}
		},
		
		screenshotPreview : function() {	
				
			xOffset = 10;
			yOffset = 30;
				
			$("a.screen").hover(function(e){
				this.t = this.title;
				this.title = "";	
				
				var c = (this.t != "") ? "<br/>" + this.t : "";
				
				$("body").append("<p id='screenshot'><img src='"+ $(this).attr('screenrel') +"' alt='Template Preview' />"+ c +"</p>");								 
				$("#screenshot")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX + yOffset) + "px")
					.fadeIn("fast");						
		    },
			function(){
				this.title = this.t;	
				$("#screenshot").remove();
		    });	
			$("a.screen").mousemove(function(e){
				$("#screenshot")
					.css("top",(e.pageY - xOffset) + "px")
					.css("left",(e.pageX + yOffset) + "px");
			});			
		},
	
		storeCaret : function(element) {
			if (element.createTextRange) element.caretPos = document.selection.createRange().duplicate();
		},
		
		emoticon : function(text, textareaId) {
			var element = document.getElementById(textareaId);
			
			if (element != null) {
				if (element.createTextRange && element.caretPos) {
					var caretPos = element.caretPos;
					caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
					element.focus();
				}
				else if (element.selectionStart != undefined) {
					element.value = element.value.substring(0, element.selectionStart) + text + element.value.substring(element.selectionStart);
					element.focus();
				} else {
					element.value += text;
					element.focus();
				}
			}
		},
		
		templateComments : function(templateId, page, pages) {
			var url = this.settings.template_comments_href;
			
			$.ajax({
				url: url,
				cache: false,
				success: function(html){
					$("#template_comments").append(html);
				}
			});
		},
		
		templateLike : function(templateId) {
			var url = this.settings.add_template_like_href;
		
			document.getElementById("template_like_"+templateId).innerHTML = '<span><img src="'+this.settings.img_template_loading+'"/></span>';
		
			$.ajax({
				url: url,
				data: "id_template=" + templateId,
				type: "POST",
				cache: false,
				success: function(html){
					if (html != null && html != '') {
						document.getElementById("template_like_"+templateId).innerHTML = "<span><strong>"+html+"</strong></span>";
					}
				}
			});
		},
		
		articleLike: function(articleId) {
			var url = this.settings.add_article_like_href;
		
			document.getElementById("article_like_"+articleId).innerHTML = '<span><img src="'+this.settings.img_template_loading+'"/></span>';
		
			$.ajax({
				url: url,
				data: "id_article=" + articleId,
				type: "POST",
				cache: false,
				success: function(html){
					if (html != null && html != '') {
						document.getElementById("article_like_"+articleId).innerHTML = "<span><strong>"+html+"</strong></span>";
					}
				}
			});
		},
		
		incScriptViews: function(uniquename) {
			var url = this.settings.increment_script_href;
			try {
				$.ajax({
					url: url,
					data: "unique_title=" + uniquename,
					type: "GET",
					cache: false,
					success: function(html){
					}
				});
			} catch (e) {}
		},
		
		incTemplateViews: function(uniquename) {
			var url = this.settings.increment_template_href;
			try {
				$.ajax({
					url: url,
					data: "unique_title=" + uniquename,
					type: "GET",
					cache: false,
					success: function(html){
					}
				});
			} catch (e) {}
		},
		
		incArticleViews: function(uniquename) {
			var url = this.settings.increment_article_href;
			try {
				$.ajax({
					url: url,
					data: "unique_title=" + uniquename,
					type: "GET",
					cache: false,
					success: function(html){
					}
				});
			} catch (e) {}
		}
	}
});

var page = $.page();

$(function() {
	page.doAdv();
});
