//Array mapping paragraph indices to ads, for those paragraphs where an ad is present
var ad_array = new Array();

//index of current url stored in deepnet database
var wordpress_url_id = -1;


//encoded url of current page
var url = document.location.href;
url = escape(url).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');



     function xss_ajax(url) {  
         var script_id = null;  
         var script = document.createElement('script');  
         script.setAttribute('type', 'text/javascript');  
         script.setAttribute('src', url);  
         script.setAttribute('id', 'script_id');  
   
         script_id = document.getElementById('script_id');  
         if(script_id){  
             document.getElementsByTagName('head')[0].removeChild(script_id);  
         }  
   
         // Insert <script> into DOM  
         document.getElementsByTagName('head')[0].appendChild(script);  
     }  

     function callback(response) {  
         
 	wordpress_url_id = response['wordpress_url_id'];
	ad_array = response['ad_array'];
       
	//Loop across all ads in array and add them to page
	for(var i = 0; i < ad_array.length; i++) {
                
                var ad_dict = ad_array[i];

	      	ad_location = ad_dict['ad_location'];
		ad_source = ad_dict['ad_source'];
		ad_match_id = ad_dict['match_id'];

		//create ad
		var ad = document.createElement("div");
		ad.innerHTML = '<iframe src="' + ad_source + '" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>';
		ad.style.styleFloat = "left";

		ad.style.cssFloat = "left";
  
		ad.style.display = "inline";
		ad.style.width = "140px";
		ad.style.height = "275px";

		var para_index = document.getElementsByTagName('p')[ad_location];

                 //para_index = document.getElementsByTagName('p')[0];
                //document.getElementsByTagName('p')[0].innerHTML = "Yooo";

		para_index.insertBefore(ad,para_index.firstChild);
	}

    
}  
       
    function getAd(url){

            //var url = urlencode(location.href);
            //var title = urlencode(document.title);
            //var domain = urlencode(location.hostname);

            var request = 'http://deepnet.us/wordpress/AdControl/get_multi_ad.php?url='+url;
            xss_ajax(request);
    }


//document.body.onLoad = getAd(url);
setTimeout("getAd(url)",600);

//create keyword shortcut
function shortcut(shortcut,callback,opt) {
	//Provide a set of default options
	var default_options = {
		'type':'keydown',
		'propagate':false,
		'target':document
	}
	if(!opt) opt = default_options;
	else {
		for(var dfo in default_options) {
			if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
		}
	}

	var ele = opt.target
	if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
	var ths = this;

	//The function to be called at keypress
	var func = function(e) {
		e = e || window.event;

		//Find Which key is pressed
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		var character = String.fromCharCode(code).toLowerCase();

		var keys = shortcut.toLowerCase().split("+");
		//Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
		var kp = 0;
		


			for(var i=0; k=keys[i],i<keys.length; i++) {
			//Modifiers
			if(k == 'ctrl' || k == 'control') {
				if(e.ctrlKey) kp++;

			} else if(k ==  'shift') {
				if(e.shiftKey) kp++;

			} else if(k == 'alt') {
					if(e.altKey) kp++;

			} else if(k.length > 1) { //If it is a special key
				if(special_keys[k] == code) kp++;

			} else { //The special keys did not match
				if(character == k) kp++;
				
			}
		}

		if(kp == keys.length) {
			callback(e);

			if(!opt['propagate']) { //Stop the event
				//e.cancelBubble is supported by IE - this will kill the bubbling process.
				e.cancelBubble = true;
				e.returnValue = false;

				//e.stopPropagation works only in Firefox.
				if (e.stopPropagation) {
					e.stopPropagation();
					e.preventDefault();
				}
				return false;
			}
		}
	}


	//Attach the function with the event	
	if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
	else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
	else ele['on'+opt['type']] = func;
}

var edit_bool = false;

shortcut("Ctrl+X",function() {
	all_paragraphs = document.getElementsByTagName('p');

	//Edit Mode off. Turn it on
	if(!edit_bool) {

		
		
		

		//Add Option to Log In 
		for (var i = 0; i < all_paragraphs.length; i++) {

			var link = "./wp-login.php";
			paragraph = all_paragraphs[i];

			//ad Match Appearing in this paragraph
			//Add Option to Delete ad Match
			
                        //IE is EVIL!!!!
                        if(navigator.appName  == "Microsoft Internet Explorer"){

                               var newdiv = document.createElement("a");
                               newdiv.innerHTML = '<a href ="' + link + '"target="_blank" style="color: rgb(0,0,255)"> **LOGIN** </a>';
                               paragraph.insertBefore(newdiv,paragraph.firstChild);
                        }

                        else
			      paragraph.innerHTML = '<a href ="' + link + '"target="_blank" style="color: rgb(0,0,255)"> **LOGIN** </a>'+ paragraph.innerHTML;
                         
		}

	edit_bool = true;
	}

	//Edit Mode On. Turn it off.
	else {

		

		

		for (var i = 0; i < all_paragraphs.length; i++) {

			paragraph = all_paragraphs[i];
			paragraph.removeChild(paragraph.firstChild);
		}
	
		edit_bool = false;
		
	}
});