jQuery.page = function(options) {
	var instance = new jQuery.ifreewebtools(options);
	return instance;
};

jQuery.ifreewebtools = function(options) {
	this.block = false;
	this.settings = jQuery.extend({}, jQuery.ifreewebtools.defaults, options);
};

jQuery.ifreewebtools.pagerank = function() {
	this.datacenter;
	this.resultid;
	this.domain;
	this.next;
			
	this.send = function() {
		var sendData = "datacenter="+this.datacenter+"&domain="+this.domain;
			
		var resId = this.resultid;
		var nex = this.next;
		
		$.ajax({
			type: "POST",
			url: "pagerank_viewer",
			async: true,
			data: sendData,
			success: function(msg){
				document.getElementById(resId).innerHTML = '<img src="img/pagerank/color/pg'+msg+'.gif" title="PageRank '+msg+'" alt="PageRank '+msg+'"/>';
				if (nex != null) {
					nex.send();
				}
		   	}
		});
	}
};

jQuery.ifreewebtools.backlink = function() {
	this.datacenter;
	this.resultid;
	this.domain;
	this.next;
			
	this.send = function() {
		var sendData = "datacenter="+this.datacenter+"&domain="+this.domain;
			
		var resId = this.resultid;
		var nex = this.next;
		
		$.ajax({
			type: "POST",
			url: "link_popularity_viewer",
			async: true,
			data: sendData,
			success: function(msg){
				document.getElementById(resId).innerHTML = '<span class="blackmedium">'+msg+"</span>";
				if (nex != null) {
					nex.send();
				}
		   	}
		});
	}
};

jQuery.ifreewebtools.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: "adc",
				async: false,
				data: sendData
			}).responseText;
					
			this.sended = true;
		}
	}
};


jQuery.extend(jQuery.ifreewebtools, {
	
	defaults : {

	},
	
	prototype: {
	
		setPageranksTabs : function() {
			var instance = this;
		
			$("img").each(function() {
				var that = $('#'+this.id);	
				if (that.attr("tab")) {	
					
					that.click(function() {
					
						if (instance.block) {
							return;
						}
						
						instance.block = true;					
						var thatId = this.id;
						
						var prevContentId = "pagerank0_content";

						$("div").each(function() {
							if ($('#'+this.id).attr("selected")) {
								prevContentId = this.id;
							}
						});
				
						if (prevContentId != this.id+"_content") {
							if (prevContentId != undefined) {
							
								$('#'+prevContentId).fadeOut('slow', function() {			
									$('#'+thatId+"_content").fadeIn('slow', function() {
										instance.block = false;
									});
								});
								
								$('#'+prevContentId).removeAttr("selected");
								
							} else {
															
								$('#'+this.id+"_content").fadeIn('slow', function() {
									instance.block = false;
								});
							}
						} else {
							instance.block = false;
						}
						
						$('#'+this.id+"_content").attr("selected","true");

					});
				}
			});
		},
	
	
	
		
		backlinks : function(domain) {
			var table = Array();
			var oldCheckObj = null;
			var checkPageRank = null;
			var i = 0;
			
			$("div").each(function(index) {
				if ($(this).attr("type") == "pagerank") {
					checkPageRank = new jQuery.ifreewebtools.backlink();
					checkPageRank.datacenter = $(this).attr("datacenter");
					checkPageRank.resultid = $(this).attr("id");
					checkPageRank.domain = domain;
					
					if (oldCheckObj != null) {
						oldCheckObj.next = checkPageRank;
					} else {
						first = false;
					}
					
					table[i] = checkPageRank;
					i ++;
				}
				
				oldCheckObj = checkPageRank;
			});
			
			if (table[0] != null) {
				table[0].send();
			}
		},
		
		pageranks : function(domain) {
			var table = Array();
			var oldCheckObj = null;
			var checkPageRank = null;
			var i = 0;
			
			$("div").each(function(index) {
				if ($(this).attr("type") == "pagerank") {
					checkPageRank = new jQuery.ifreewebtools.pagerank();
					checkPageRank.datacenter = $(this).attr("datacenter");
					checkPageRank.resultid = $(this).attr("id");
					checkPageRank.domain = domain;
					
					if (oldCheckObj != null) {
						oldCheckObj.next = checkPageRank;
					} else {
						first = false;
					}
					
					table[i] = checkPageRank;
					i ++;
				}
				
				oldCheckObj = checkPageRank;
			});
			
			if (table[0] != null) {
				table[0].send();
			}
		},
		
		setPanelColor : function(color) {
			if (document.getElementById('tool_table') != null) {
				document.getElementById('tool_table').className = "tool_table_"+color;
				
				this.setCookie('tool_color', color, 30, null, null, null);
			}
		},
		
		getCookie : function (check_name) {
			var a_all_cookies = document.cookie.split( ';' );
			var a_temp_cookie = '';
			var cookie_name = '';
			var cookie_value = '';
			var b_cookie_found = false; 
			
			for (i = 0; i < a_all_cookies.length; i++) {
				a_temp_cookie = a_all_cookies[i].split( '=' );
				cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
				if (cookie_name == check_name) {
					b_cookie_found = true;

					if (a_temp_cookie.length > 1) {
						cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
					}
					return cookie_value;
					break;
				}
				a_temp_cookie = null;
				cookie_name = '';
			}
			if (!b_cookie_found) {
				return null;
			}
		},	
		
		setCookie : function(name, value, expires, path, domain, secure) {
			var today = new Date();
			today.setTime( today.getTime() );
			
			if (expires) {
				expires = expires * 1000 * 60 * 60 * 24;
			}
			
			var expires_date = new Date(today.getTime() + (expires));
			
			document.cookie = name + "=" +escape( value ) +
			( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
			( ( path ) ? ";path=" + path : "" ) + 
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
		},
				
		directoryList : function(column, order, filter, page, flag) {
			var obj = this;
			var sendData = "column="+column+"&order="+order+"&filter="+filter+"&page="+page+"&flag="+flag;
			
			document.getElementById("directory_list_panel").innerHTML = '<img src="img/wait.gif"/>';
			
			$.ajax({
				type: "GET",
				url: "seo_directory_list_page",
				async: true,
				data: sendData,
				success: function(msg){
					document.getElementById("directory_list_panel").innerHTML = msg;
					
					if (flag == 1) {
						obj.refreshSelectedDirectoryRows();
					}
			   	}
			});
		},
		
		removeDirectory : function(directoryId, column, order, filter, page, flag) {
			var obj = this;
			
			if (confirm('Are you sure?') == false) {
				return;
			}
			
			if (directoryId != null) {
				var sendData = "&dirid="+directoryId;
					
				$.ajax({
					type: "POST",
					url: "seo_directory_remove",
					async: true,
					data: sendData,
					success: function(msg){
						obj.directoryList(column, order, filter, page, flag);
				   	}
				});
							
			}
		},
		
		saveSubmitPage : function(inputid, directoryId, column, order, filter, page, flag) {
			var inp = document.getElementById(inputid);
			var obj = this;
			
			if (inp != null && directoryId != null) {
				var sendData = "submitp="+encodeURIComponent(inp.value)+"&dirid="+directoryId;
					
				$.ajax({
					type: "POST",
					url: "seo_directory_submit",
					async: true,
					data: sendData,
					success: function(msg){
						obj.directoryList(column, order, filter, page, flag);
				   	}
				});
							
			}
		},
		
		refreshSelectedDirectoryRows : function() {
			var savedRows = this.getCookie('sel_directory_rows');
			
			if (savedRows != null) {
				var cookieIds = savedRows.split(',');
				
				var hashMap = Array();
				for (var i = 0; i < cookieIds.length; i++) {
					hashMap[cookieIds[i]] = 'true';
				}
				
				$("tr").each(function(i) {
					var dirid = this.getAttribute("dirid");
					var type = this.getAttribute("type");
					var permSelected = this.getAttribute('permSelected');
									
					if (type == 'dir_row') {
						if (hashMap[dirid] == 'true') {
							this.setAttribute('permSelected', 'true');
							$(this).css({backgroundColor: "#BF582F", cursor:"pointer" });
						}
					}
				});
			}
		},
		
		clearDirectorySelectedRows : function() {
			this.setCookie('sel_directory_rows', '', 30, null, null, null);
			this.refreshSelectedDirectoryRows();
			this.directoryList('pagerank', 'desc', '', 1, 1);
		},
		
		removeRowsFromFlatString : function(rows, trobj) {
			var cookieIds = rows.split(',');
			var rowId = trobj.getAttribute("dirid");
			var retRows = '';
			for (var i = 0; i < cookieIds.length; i++) {
				if (rowId != cookieIds[i]) {
					retRows += ","+cookieIds[i];
				}
			}
			
			if (retRows.length > 0) {
				retRows = retRows.substring(1);
			}
			
			return retRows;
		},
		
		saveSelectedRow : function(trobj) {
			var savedRows = this.getCookie('sel_directory_rows');
			var rowId = trobj.getAttribute("dirid");
			
			if (savedRows == null || savedRows == '') {
				savedRows = rowId;
			} else {
				savedRows += ','+rowId;
			}
			this.setCookie('sel_directory_rows', savedRows, 30, null, null, null);
		},
		
		removeSelectedRow : function(trobj) {
			var savedRows = this.getCookie('sel_directory_rows');
			var rowId = trobj.getAttribute("dirid");
	
			var newIds = this.removeRowsFromFlatString(savedRows,trobj);
			this.setCookie('sel_directory_rows', newIds, 30, null, null, null);
		},
		
		doubleClickDirectory : function(trobj) {
			if (trobj == null) {
				return;
			}
			
			var permSelected = trobj.getAttribute('permSelected');
			var basecolor = trobj.getAttribute("basecolor");
			
			if (permSelected == null || permSelected != 'true') {
				trobj.setAttribute('permSelected', 'true');
				$(trobj).css({backgroundColor: "#BF582F", cursor:"pointer" });
				this.saveSelectedRow(trobj);
				
			} else {
				if (permSelected == 'true') {
					trobj.setAttribute('permSelected', null);
					$(trobj).css({backgroundColor: basecolor, cursor:"pointer" });
					this.removeSelectedRow(trobj);
				}
			}
		},
		
		selectDirectory : function(trobj) {
			
			if (trobj == null) {
				return;
			}
			
			$("tr").each(function(i) {
				var basecolor = this.getAttribute("basecolor");
				var dirid = this.getAttribute("dirid");
				var type = this.getAttribute("type");
				var permSelected = this.getAttribute('permSelected');
												
				if (type == 'dir_row') {
					this.setAttribute("selectedDirectory", null);
					if (permSelected != null && permSelected == 'true') {
						$(this).css({backgroundColor: "#BF582F", cursor:"pointer" });
					} else {
						$(this).css({backgroundColor: basecolor, cursor:"pointer" });
					}
				}
			});
			
			$(trobj).css({backgroundColor: "#F19F33", cursor:"pointer" });
			trobj.setAttribute("selectedDirectory", "true");
		},
		
		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.ifreewebtools.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);
					}
		}
		
	}
});

var page = $.page();
