
//Array of WordPress Posts on Front Page
var post_array = new Array();

//function to encode url
function encode(url){ return 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) {  
         
 	//post_paragraphs = document.getElementsByClassName('content_p');
        post_paragraphs = getElementsByClassName('content_p');
        
	for(var i=0; i < response.length; i++) {

		var id = response[i]['wordpress_url_id'];
		var ad_array = response[i]['ad_array'];

		post_array[i].set_info(id,ad_array);
                post_array[i].show_ads();


               //post_paragraphs[i].innerHTML = id;

	}

		
}


function init() {

	//select paragraphs from posts that have been marked by plugin as belonging to content_p class
	//post_paragraphs = document.getElementsByClassName('content_p');
        post_paragraphs = getElementsByClassName('content_p');        

        //total number of content based paragraphs
        var count = 0;

        //url of ajax request to deepnet server for ads associated with each paragraph
        var request = 'http://deepnet.us/wordpress/AdControl/get_front_page_ad.php';

	var prev_url = "";
        var extension = "";
	for(var i = 0; i < post_paragraphs.length; i++) {

		var post_p = post_paragraphs[i];
                
                //url stored in title
		var post_url = post_p.title;
	        
		//url associated with new post
		if(post_url != prev_url) {
		
                        
			prev_url = post_url;
                        encoded_url = encode(post_url);
                        

                        //create new wordpress post object and add to array
			post_array.push(new WP_Post(encoded_url));
                       

                        //extend request url
                        if(count==0){
                            extension = "?url0="+ encoded_url;}

                        else{
                            extension = extension + "&url"+count+"="+encoded_url;}

                        
                        count += 1;        
		}
 
               

               post_array[count-1].add_paragraph(post_p);
	}

        extension = extension + "&num=" + count;
        request = request + extension;

        xss_ajax(request);
        //post_paragraphs[0].innerHTML = request;
}

//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() {
	
         for(var i = 0; i <  post_array.length; i++)
              post_array[i].toggle_visitor(edit_bool);

         edit_bool = !edit_bool;
         
});

setTimeout("init()",600);
	