var c0=0;
var db = new Array('id','pr','url','adf');
db['id']=new Array(), db['pr']=new Array(), db['url']=new Array(),db['adf']=new Array();

$(function(){
    $('.slide-out-div').tabSlideOut({
        tabHandle: '.handle',pathToTabImage: 'images/contact_tab.gif',imageHeight: '122px',imageWidth: '40px',tabLocation: 'right',speed: 300,action: 'click',topPos: '270px',leftPos: '20px',fixedPosition: true
    });

});

var core=function(){
	var sort=true;
	var pr_tbl='<table id="prtbl" class="dark box"><thead><tr><th id="pr_img">Pagerank (img)</th><th id="pr_num">Pagerank (numeric)</th><th>Website</th><th id="pr_remove">Action</th></tr></thead><tbody>';
	/*Pagerank Stuff*/
	this.pr_check=function(){
	
		var l=$('#l').val();
		var links = l.split(/(\r\n|\n|\r)/gm);
		
        $('#load').html('<img src="images/loading.gif" />');
		
		/*-DB array*/
		for(var i=0;i<links.length;i=i+2){
            this.pr(links[i],i);
		}

        $('#prtbl').append("<tbody></table>");
		$('#load').hide().remove();
	}
	this.pr=function(prlink,c){
		if(c==0)
			this.pr_reset();
		
		$.ajax({
			type: "POST",
			url: "process.php?p=1",
			data: "l="+prlink+"&c="+c,
			error: function(err){alert("Connection error");},
			success: function(msg){
				eval(msg);
				
				db['id'][c0]  = c;
				db['pr'][c0]  = pr;
				db['url'][c0] = url;
				db['adf'][c0] = adf;
				if(pr>=0){
					var img="http://www.optimizare-promovare.ro/seo//images/pr/Google/pr"+pr+".gif";
				}else{
                    var img="images/notavailable.png";
				}
				$('#prtbl').append('<tr id="wid'+c+'"><td><img src="'+img+'" /></td><td>'+pr+'</td><td><a href="'+url+'">'+url+'</a></td><td><a href="#wid'+c+'" onclick="icx.pr_remove(\''+c+'\');"> Remove (-)</a></td></tr>');
				c0++;
				
				
				//$("#prtbl").append(msg);
			}
		});
	}
	this.pr_reset=function(){
		$("#result").html(pr_tbl);
		db['id']=new Array(), db['pr']=new Array(), db['url']=new Array();db['adf'] = new Array();
		c0=0;
	}
	this.clear=function(){
		$('#l').val('');
	}
	this.pr_sort=function(){
		var i=0,j=0,t;
		
		for(i=0;i<c0;i++){
			for(j=0;j<c0;j++){
			/*Crescator*/
			if(db['pr'][i] > db['pr'][j]){
						t=db['pr'][i];
						db['pr'][i]=db['pr'][j];
						db['pr'][j]=t;
						t=db['url'][i];
						db['url'][i]=db['url'][j];
						db['url'][j]=t;
						t=db['adf'][i];
						db['adf'][i]=db['adf'][j];
						db['adf'][j]=t;
				}
			}
		}
		this.pr_show_tbl();
	}
	this.pr_count=function(){
		var counter=1,i=0;
		for(i=0;i<db['id'].length;i++){
			if(db['id'][i])
				counter++;
		}	
		return counter;
	}
	this.pr_remove=function(c){
		$('#wid'+c).remove();
		var j;
		/*Remove from db, where id = c*/
		for(j=0;j<c0;j++){
			if(db['id'][j] == c){
				delete db['pr'][j];
				delete db['url'][j];
				delete db['id'][j];
				delete db['adf'][j];
			}
		}
		$('#current').html('Current Links: <span class="total">'+this.pr_count()+'</span>');
	}
	this.pr_show_tbl=function(){
		$("#result").html(pr_tbl);
		var i=0;
		for(i=0;i<db['pr'].length;i++){
		if(db['url'][i]){
				if(db['pr'][i] >=0){
					var img="http://www.optimizare-promovare.ro/seo//images/pr/Google/pr"+db['pr'][i]+".gif";
				}else{
					var img="images/notavailable.png";
				}
			$('#prtbl').append('<tr id="wid'+db['id'][i]+'"><td><img src="'+img+'" /></td><td>'+db['pr'][i]+'</td><td><a href="'+db['adf'][i]+'">'+db['url'][i]+'</a></td><td><a href="#wid'+db['id'][i]+'" onclick="icx.pr_remove(\''+db['id'][i]+'\');"> Remove (-) </a></td></tr>');
			}
		}
	}
	this.pr_export=function(){
		$('#export_total').html('Links: <span class="total">'+this.pr_count()+'</span>');
		$('#export').html('<textarea id="pr_export"></textarea>');
		var i=0;
		for(i=0;i<db['pr'].length;i++){
			if(db['url'][i])
				$('#pr_export').append(db['url'][i]+'\n');
		}
	}
	
	this.send_message=function(){
	var e=$('#c_email').val();
	var s=$('#c_subj').val();
	var m=$('#c_mess').val();
	
	$.ajax({
			type: "POST",
			url: "process.php?p=3",
			data: "m="+m+"&s="+s+"&e="+e,
			error: function(err){alert("Connection error");},
			success: function(msg){
				alert(msg);
			}
		});
	}
	
	/*Backlinks*/
	this.qu=function(query){
		return "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q="+query;
	}
	this.bls_report=function(){
		var qurl=$('#bl_url').val();
		var start=0,cu='not available';
		var blsite=this.qu('link:')+escape(qurl)+"&start="+start;
        
		var blsite=this.qu('site:')+escape(qurl)+"&start="+start;
		$.ajax({
		url: blsite,
        async:false,
		dataType: 'jsonp',
		success: function(json){
			var d=json.responseData.results[0].unescapedUrl;
			cu=json.responseData.cursor.estimatedResultCount;
			if(!cu){cu='not available'}
			$('#result').append('Intern Backlinks: '+cu+'<br />');
		}
		});
        
		$.ajax({
			type: "POST",
			url: "process.php?p=5",
            async:false,
			data: "u="+qurl,
			error: function(err){alert("Connection error");},
			success: function(msg){
				$('#result').append("Extern Backlinks: "+msg+"<br/>");
			}
		});
	}
    
    
    
    
    
    /* Dominos Analyze*/
    this.dominos_analyze=function(){
        if($("#dominos_website_url").val()=="http://"){
            $('#result').html("Make sure you insert a valid website url");
        }
        
        $.ajax({
			type: "POST",
			url: "process.php?p=4",
			data: "u="+$("#dominos_website_url").val(),
			error: function(err){alert("Connection error");},
            beforeSend:function(){
                $("#result").html("<img src='images/loading.gif' alt='Dominus Website Analyzer Loading'/>");
            },
			success: function(msg){
				$('#result').html(msg);
			}
		});
    }
    /*Wordpress Spam*/
    this.wp_spam=function(){
        var l=$('#l').val();
		var links = l.split(/([\r\n]|\n|\r)/gm);
        
        var url=$('#url').val();
        var email=$('#email').val();
        var name=$('#name').val();
        var ms=$('#ms').val();
        var i;
        for(i=0;i<links.length;i++){
            alert(i+" :: "+links[i]);
        }
    }
    
    /*Exclusive Request*/
    this.iporder=function(order){
        $('#exclusive').hide('slow');
        $('#exclusive_req').html('<p><span class="total version">All you have to do now is to insert your paypal email address and you\'ll have to wait for the invoice to come in your paypal.</span></p> <input type="text" class="dominus_input" id="ppemail" value="Paypal email address"/> <a href="#" onclick="icx.sendorder(\''+order+'\')" class="box content-box darker btn">Send Order</a>');
        
    }
    this.sendorder=function(order){
    var pp=$('#ppemail').val();
    
        $.ajax({
			type: "POST",
			url: "process.php?p=7",
			data: "order_id="+order+"&pp="+pp,
			error: function(err){alert("Connection error");},
            beforeSend:function(){
                $("#exclusive_req").html("<img src='images/loading.gif' alt='Dominus Website Analyzer Loading'/>");
            },
			success: function(msg){
				$('#exclusive_req').html(msg);
			}
		});
    }
    
}
	
$(document).ready(function(){
	icx=new core;
	
	$('#send_email').click(function(){
		icx.send_message();
	});
});


