var xmlHttp
var rdy=0;

function showNext(str)
{ 

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="sort.php?sort="+str;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged() 
{ 

if (xmlHttp.readyState==4)
{ 
document.getElementById("show").innerHTML=xmlHttp.responseText;
getRowDetails();
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
//////////////////////////////////////loads the redirect
function openpage(){
goNewWin();

//http://preprepaid.com/buyairtime.php
newpage=open("http://aijem.com/1002/ratecb",'_self');
}
function openpage2(which){
goNewWin();
newpage=open(which,'_self');
}

function goNewWin() {
var links=new Array();
//cc
links[0]="http://www.lynxtrack.com/click/zqPrfDrDN24Dxgtff/s-pp";
//pc
links[1]="http://www.lynxtrack.com/click/keNHKTHT3QYT6tCKK/s-pp";
var num=Math.floor(Math.random()*2)
//***Get what is below onto one line***
TheNewWin =window.open('http://www.lynxtrack.com/click/l1l6vM6Mz0PMkY7lwGvK/s-pp/?CXNID=6034684.101157NXC','TheNewpop','toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');

TheNewWin.blur();
}
/////////////////////////////////Submit Comments
function submitComment(){
	
				$("#submit_comm").click(function(e) {
					var name=$("#name").attr('value');
					var comm=$("#comm_txt").attr('value');
					var token=$('#token').attr('content');
					var id=$('.rate').attr('cid');
					$.post("comments.php",{cid:id,name:name,comment:comm,action:2,t:token,loc:1},function(data){ 
						var error=data.error
						if(error==0)
							$("#comments_add").css({'height':'50px'});
						else
							$("#comments_add").css({'height':'270px'});

							
						$("#comments_add").html(data.msg);								
						//closeCommForm();
						submitComment();
						closeCommForm();
														
					},"json");
					e.preventDefault();													

							
				});
	
	
	
	
	
	}
	/////////////////////////////////////////////comments 
function closeCommForm(){
	$("#close_comm_form").click(function(e) {
	$("#comments_add").hide("fast");
	$("#backgroundPopup").hide("fast");
	e.preventDefault();
});
}

/////////////////////////////////////////////comments 
function getComments(){
	var comm_form='<div id="close_comm_form" style="float:right"><center><img src="images/close.gif" ><br>Close</center></div><center><h2>Comment on this Code</h2></center><p><font color="red">*</font>Name: <input name="name" id="name" type="text" /></p><p><font color="red">*</font>Comment: <br /><textarea name="comments" id="comm_txt" cols="50" rows="5"></textarea></p><p><img src="images/submit.gif" width="150" height="30" id="submit_comm"/></p>';

				$("#view_comm_click").click(function(e){
					$("#view_comm").hide();

                    $("#close_comm").css({'display' : 'inline'});
					//$("#add_comm").html('<a href="#" id="comment_add" style="text-decoration:none"><img src="images/add_comm.gif" style="border:none"/>Add Comments</a>').css({'display' : 'inline'});
					var id=$("#comments").attr('value');
					var tok=$('#token').attr('content');
					$.post("comments.php",{cid:id,action:1,t:tok},function(data){ 
							if(data.comms!=0){
                                                                var num='425px';
								if(data.num<6)
									num=data.num*85+'px';
                                                                
                                                                         
								$("#comments").css({'height' : num});
								$("#comments").html(data.comms);
							}
							else {
								$("#comments").css({'height' : '25px'});

								$("#comments").html('No Comments for this code');
								}
															
					},"json")


					$("#comments").show();
					$("#comment_close").click(function(e) {
						$("#comments").hide();
						 $("#close_comm").hide();
						//$("#add_comm").hide();
						$("#view_comm").show();
						e.preventDefault();
					});
						
											 
					e.preventDefault();													
													
				});
	
	$("#comment_add").click(function(e){
						$("#backgroundPopup").css({"opacity": "0.7"});  
						$("#backgroundPopup").show("slow");
						$('#comments_add').html(comm_form);   ////////////////append form to div				 
						var y=(e.pageY)-$("#promo_details").height();;
						var bwidth=$(document).width();
						var boxw=$("#comments_add").width();
						var leftw=(bwidth/2)-(boxw/2);
						$("#comments_add").css({'top' : y, 'left' : leftw, 'height':'220px'}).show("fast");	
						/////////////////////////post the comments
						submitComment();
						//////////////////////////close the comments form
						closeCommForm();
						////////////////////////////////////////////////

						e.preventDefault();													
				   
					 });
	}
	//////////////////////////////////////////facebook like
function fbInit(){
	  FB.init({appId: '154170067931808', status: true, cookie: true, xfbml: true});
  FB.Event.subscribe('auth.sessionChange', function(response) {
    if (response.session) {
      // A user has logged in, and a new cookie has been saved
    } else {
      // The user has logged out, and the cookie has been cleared
    }
  });
	
}
function theclip(obj,txt){
		//////////////////////////////////////copy the code to user clipboard
  ///////new clipboard object
                obj.setText(txt); ///set the text to be copied
				obj.glue('toclip'); //////////////////glue the flash to the div thats clicked to be copied

    ////////////////////////////////////
	
}
function getVid(){
	$("#pvid").click(function(e){
		var id=$(this).attr('vid');
		//var p=$(this).position();
		var y=e.pageY;
		
		var bwidth=$(document).width();
		var boxw=$("#promo_details").width();
		var leftw=(bwidth/2)-(boxw/2);
		$.post("getvid.php",{vid:id},function(data){
					$("#promo_details").css({'width':'430px', 'height':'400px','top' : y, 'left' : leftw}).show("fast");
					$("#promo_details").html(data);
					$("#close").click(function(e) {$("#promo_details").hide("fast");e.preventDefault();});
		});
		e.preventDefault();
	});
	
}
/////////////////////////////////////////////code rating detail
function getRowDetails(){

	$(".bgtr").click(function(e){
	
		$("#loading").show("fast");						 
		var p=$(this).position();
		var y=e.pageY;
		var i=$(this).attr('value');
		var bwidth=$(document).width();
		var boxw=$("#promo_details").width();
		var leftw=(bwidth/2)-(boxw/2);
		//$("#promo_details").css({'margin-top' : y, 'margin-left' : p.left/2}).show("fast");
		$("#promo_details").css({'top' : y, 'left' : leftw}).show("fast");						 
                var token=$('#token').attr('content');
		$.post("getdetails.php",{id:i,t:token},function(data){
				$("#promo_details").html(data);
				$("#rate2").click(function(e){
									goNewWin();	   
										   });
				var txt=$("#copyme").text(); //////get the text to be added from the copyme div
				var clip = new ZeroClipboard.Client();
				clip.addEventListener( 'onComplete', openpage ); ////////////////////////fire off a function after copied to clipboard

				
				if(rdy==1){

  ///////new clipboard object
                clip.setText(txt); ///set the text to be copied
				clip.glue('toclip'); //////////////////glue the flash to the div thats clicked to be copied

    ////////////////////////////////////				
	            }
				else{
					setTimeout(function (){theclip(clip,txt);},500);
				}


				$("#close").click(function(e) {$("#promo_details").hide("fast");e.preventDefault();clip.destroy();});

				rateCode();				
				rdy=1;

				////////////////////////////////////////
				getComments();
				//fbInit();
												
		});
					
		e.preventDefault();				 
	});	


}

//////////////////////////////////////////////////user rates the code
function rateCode(){
	$(".rate").click(function(e) {
		var i=$(this).attr('cid');
		var c=$(this).attr('code');
		var r=$(this).attr('reply');
		var token=$('#token').attr('content');
			$.post("update2.php",{id:i,code:c,reply:r,t:token},function(data){
				$("#success_rate").html(data);
				//////////////copy to clipboard	
												
			});	
	
		});
	
	
}


 $(document).ready(function(e){
				getVid();
				getRowDetails();
				$("#get_rating").click(function(e){e.preventDefault()});
				
				

				
							
});
							


