$(document).ready(pageLoaderInit);
function pageLoaderInit(){
  $("#nav a").click(function(event){
    if(this.href.indexOf(home)>=0&&this.href.indexOf('/wp-')<0){
	  //Change span text next to logo
   	  $("#spantext").text(this.text);
	  
      // stop default behaviour
      event.preventDefault();
      // remove click border
      this.blur();
      // get caption: either title or name attribute
      var caption = this.title || this.name || "";
      // get rel attribute for image groups
      var group = this.rel || false;
      // display the box for the elements href
	  //alert("Caption: " + caption + " Group: " + group);
      loadPage(this.href);
	  if(window.location!=home+'/'){
	  	window.location=home+'/';
	  }
	  arrowCheck();
    }
  });
  
  $("#mrt_sub_form").submit(function(){

	  if(window.location!=home+'/'){
	  	window.location=home+'/';
	  }
      loadPage(home + '/wp-content/plugins/sms-text-message/sms-record.php?c=' +document.getElementById('c').value + "&pn=" +document.getElementById('pn').value );
	  return false;
    
  });
  
  $("#nlForm").submit(function(){
      if(window.location!=home+'/'){
	  	window.location=home+'/';
	  }
      //loadPage(home + '/wp-content/plugins/newsletter/nlsuccfail.php?ne=' +document.getElementById('ne').value + "&nn=" +document.getElementById('nn').value + "&na=" +document.getElementById('nn').value );
	  loadPage(home + '?ne=' +document.getElementById('ne').value + "&nn=" +document.getElementById('nn').value + "&na=" +document.getElementById('na').value );
	  return false;
    
  });
  
  // Allow anchors in footer to operate the same as the nav.
  $(".footmap a").click(function(event){
    if(this.href.indexOf(home)>=0&&this.href.indexOf('/wp-')<0){
	  //Change span text next to logo
   	  $("#spantext").text(this.text);
      event.preventDefault();
      this.blur();
      var caption = this.title || this.name || "";
      var group = this.rel || false;
	  if(window.location!=home+'/'){
	  	window.location=home+'/';
	  }
      loadPage(this.href);
    }
  });
  
  // Allow anchors in search results to operate the same as the nav.
  $(".topTitle a").click(function(event){
    if(this.href.indexOf(home)>=0&&this.href.indexOf('/wp-')<0){
	 //Change span text next to logo
   	  $("#spantext").text(this.text);
       event.preventDefault();
      this.blur();
      var caption = this.title || this.name || "";
      var group = this.rel || false;
	  //if(window.location!=home+'/'){
//	  	window.location=home+'/';
//	  }
      loadPage(this.href);
    }
  });
  
 // When searchform is submitted, do this. 
  $("#searchform").submit(function(){
	  // If window location is not home, set it to home.
	  if(window.location!=home+'/'){
	  	window.location=home+'/';
	  }
      loadPage(home + '?s='+document.getElementById('s').value);
	  return false;
  });
}


function getHTTPObject() {
  var xmlhttp;
if (window.XMLHttpRequest) {
  // If IE7, Mozilla, Safari, and so on: Use native object.
  xmlhttp = new XMLHttpRequest();
}
else
{
  if (window.ActiveXObject) {
     // ...otherwise, use the ActiveX control for IE5.x and IE6.
     xmlhttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');
  }
}
  return xmlhttp;
}
var isWorking = false;
var http = getHTTPObject();




function loadPage(url){
  if(!isWorking){
   // scroll(0,0);
	// Scroll to top of page smoothly
	$('html, body').animate({
		scrollTop: $("#wrapper").offset().top
	}, 500);
		
    document.getElementById('content').innerHTML='<center><div class="loading"><img src="'+loadingIMG.src+'" /></div></center>';

	isWorking=true;
	$.ajax({
	  url: url,
	  cache: false,
	  success: function(html){
		isWorking=false;
		  var content = html;
		  content = content.split('id="content"')[1];
		  content = content.substring(content.indexOf('>')+1);
		  var depth=1;
		  var output='';
		  while(depth>0){
			temp = content.split('</div>')[0];
			//count occurrences
			i=0;
			pos = temp.indexOf("<div");
			while(pos!=-1){
			  i++;
			  pos = temp.indexOf("<div",pos+1);
			}
			//end count
			depth=depth+i-1;
			output=output+content.split('</div>')[0]+'</div>';
			content = content.substring(content.indexOf('</div>')+6);
		  }
		document.getElementById('content').innerHTML=output;

		//// Below is Code for cycle plugin ///
		$('#pager').html('');
		  dom.query(function() {
			dom.query('.featuredpost').cycle({
			fx:     'fade',
			speed:  'fast',
			timeout: 6000,
			pause:   2,
			next:   '#next2',
			prev:   '#prev2',
			pager:  '#pager',
			pagerAnchorBuilder: newPagerFactory
			});
		 });
		
		// set the left margin of the new pager
	  	$('#pager').css("margin-left", $("#pager").width() - ($("#pager").width() + $("#pager").width()/2));	
	
		dom.query(document).ready(function(){
			arrowCheck();
		});
		
		  pageLoaderInit();
	  }
	});

  }
}



// Create new pager
function newPagerFactory(idx, slide) {
        var s = idx > 1 ? ' style="display:none"' : '';
        return '<a href="#"></a>';
    };
