/*Latest political headlines*/

var PROJECT_DIRECTORY = 'http://politicallyillustrated.com/';
var direction = 'right';
var content_width = 376;

$(document).ready(function(){

  if(document.getElementById("latest_political_headlines")){
		var html = $.ajax({
	    type: "GET",
	    url: PROJECT_DIRECTORY + "scripts/xml-content-parser.php",
	    data: "file_name=latest_political_headlines",
	    async: false
	  }).responseText;
	  
	  $("#latest_political_headlines").append(html);
	  initTopSlideshow();
	}
	
});
 
/**
 *	Top fadeIn/fadeOut
 */ 
var topSliderStatus = 'on';
var topSliderCurrent = 0;
var topSliderCount = 0;
var topSliderDelay = 5000;
var topSliderItems = [];
var topSliderNumbers = [];
var topSliderFadeDelay = 1000;
function initTopSlideshow(){
	$("#content_latest_political_headlines").css({
		'position': 'relative',
		'height': '312px',
		'width': '376px'
	});
	topSliderCount = $("#content_latest_political_headlines li.item").length;
	$("#content_latest_political_headlines li.item").each( function(i){
		$(this).css({
			position: 'absolute',
			opacity: '1',
			top:'0px',
			left: '0px',
			display: 'none',
			padding: '0',
			margin: '0'
		});
		topSliderItems.push( $(this) );
	});
	
	$("#pager_latest_political_headlines a").each( function(i){
		this.index = i;
		$(this).click( function(){
			toggleTopSlider(this.index);
			return false;
		});
		topSliderNumbers.push( $(this) );
	});
	
	$( topSliderItems[topSliderCurrent] ).css({ opacity: '1', display: 'block' });
	
	// init play pause button
	$("#pause_play").click( function(){
 		if(this.className == "pause"){
      topSliderStatus = 'off';
      this.className = "play";
 		}
 		else{
      topSliderStatus = 'on';
      this.className = "pause";
      setTimeout('toggleTopSlider();', topSliderDelay);
    }
 		return false;		
	});
	
	setTimeout('toggleTopSlider();', topSliderDelay);
}

function toggleTopSlider( newSlide ){
	if(topSliderStatus == 'on' || newSlide != null){
		var prevSlider = topSliderCurrent;
		if(newSlide != null){
			topSliderCurrent = newSlide;
		}
		else{
			++topSliderCurrent;
		}
		if(topSliderCurrent > (topSliderCount-1) ){
			topSliderCurrent = 0;
		}
		//alert(prevSlider + ' ' + topSliderCurrent);	
//		topSliderItems[prevSlider].fadeTo(topSliderFadeDelay, 0 , function(){ $(this).css('display', 'none'); });
		topSliderItems[prevSlider].css('display','none');
		topSliderNumbers[prevSlider].removeClass('active');
		
		topSliderItems[topSliderCurrent].css('display', 'block');
		topSliderItems[topSliderCurrent].css('opacity', 1);
//		topSliderItems[topSliderCurrent].fadeTo(topSliderFadeDelay, 1);
		topSliderNumbers[topSliderCurrent].addClass('active');
		if(newSlide == null){
			setTimeout('toggleTopSlider();', topSliderDelay);
		}
	}
}

/*Popular Searches*/
$(document).ready(function(){

  var html = $.ajax({
    type: "GET",
    url: PROJECT_DIRECTORY + "scripts/xml-content-parser.php",
    data: "file_name=popular_searches",
    async: false
  }).responseText;
  
  $("#popular_searches").append(html);
  
  InitTags();
  
});

/*tabs*/
function InitTags() {
  var tag_container = document.getElementById('popular_searches');
  if(tag_container)
  {
  	var link = tag_container.getElementsByTagName("a");
  	if(link)
  	{
  		for (var i = 0; i < link.length; i++)
  		{
  			if(link[i].rel != "")
  			{
  				link[i].onclick = function() 
  				{
            var search_text = document.getElementById('keywords');
            search_text.value = this.rel;
            var form = search_text.parentNode.parentNode;
            form.submit();
            return false;
  				}
  			}
  		}
  	}
  }
} 
/*tabbed content*/
var config = {
	tabs: new Array(
		{
			prefix:'tabtoggler1',
			activeTag: 'this', 
			activeClass: 'active', 
			contentTag: 'div'
		}
	)
};

/* CODE AREA */
var TabsToggler = {};
function initTabs(){
	if(!config.tabs) return;
	for(var i=0; i < config.tabs.length; i++){
		var tab = config.tabs[i];
		var menu = document.getElementById( tab.prefix + '_menu' );
		var content = document.getElementById( tab.prefix + '_content' );
		if( !menu || !content ) continue;
		
		var oTab = tab;
		oTab.active = null;
		oTab.elements = {};
		
		var links = menu.getElementsByTagName("A");
		for(var j=0; j<links.length; j++){
			var linkEl = links[j];
			var activeEl = (tab.activeTag == 'this')? linkEl : getParentByTag(linkEl, tab.activeTag);
			var tabID = linkEl.getAttribute('rel');
			if( activeEl.className.lastIndexOf(tab.activeClass) > -1 ){
				oTab.active = tabID;
			}
			var tabContent = document.getElementById( tab.prefix + '_' + tabID );
			if( tabContent ){
				tabContent.style.display = (oTab.active && oTab.active == tabID)? 'block' : 'none';
			}
			
			linkEl.setAttribute('prefix', tab.prefix);
			oTab.elements[tabID] = {
				tabID: tabID,
				linkEl: linkEl,
				activeEl: activeEl,
				content: tabContent
			};

			linkEl.onclick = function(){
				var prefix = this.getAttribute('prefix');
				var tabID = this.getAttribute('rel');
				var oTab = TabsToggler[prefix];
				
				var activeTab = oTab.elements[ oTab.active ];
				activeTab.activeEl.className = activeTab.activeEl.className.replace(' '+oTab.activeClass, '').replace(oTab.activeClass, '');
				activeTab.content.style.display = 'none';
				
				oTab.active = tabID;
				var activeTab = oTab.elements[ oTab.active ];
				if( activeTab.activeEl.className.lastIndexOf(oTab.activeClass) == -1  )
					activeTab.activeEl.className += ' ' + oTab.activeClass;
				activeTab.content.style.display = 'block';
				
				return false;
			}
		} // end for( links )

		TabsToggler[ tab.prefix ] = oTab;
		
	} // end for( config.tabs )
}

function getParentByTag( node, tag ){
	if(typeof(node.tagName) != 'undefined'){
		if(node.tagName.toUpperCase() == tag.toUpperCase()){
			return node;
		}
		else if(typeof(node.parentNode) != 'undefined'){
			return getParentByTag(node.parentNode, tag);
		}
	}
	return null;
}

$(document).ready(function(){

  initTabs();
  
});

/*most viewed
  most blogged
  most searched
*/
$(document).ready(function(){

  var html = $.ajax({
    type: "GET",
    url: PROJECT_DIRECTORY + "scripts/xml-content-parser.php",
    data: "file_name=most_viewed",
    async: false
  }).responseText;
  
  $("#most_viewed").append(html);
  
  var html = $.ajax({
    type: "GET",
    url: PROJECT_DIRECTORY + "scripts/xml-content-parser.php",
    data: "file_name=most_blogged",
    async: false
  }).responseText;
  
  $("#most_blogged").append(html);
  
  var html = $.ajax({
    type: "GET",
    url: PROJECT_DIRECTORY + "scripts/xml-content-parser.php",
    data: "file_name=most_searched",
    async: false
  }).responseText;
  
  $("#most_searched").append(html);
  
  var html = $.ajax({
    type: "GET",
    url: PROJECT_DIRECTORY + "scripts/xml-content-parser.php",
    data: "file_name=featured_stories",
    async: false
  }).responseText;
  
  $("#slider-container").append(html);
  
  initFeaturedSlider();
  
});

/**
 *	Featured Slider
 */ 
var liCount;
var liCurrent = 0;
var liWidth = 193;
function initFeaturedSlider(){
	if($('#featured-stories')){
		
		liCount = $('#featured-stories li').length;
		var ulWidht = liWidth * liCount;
	
		var ul = $('#slider');
		ul.css( "width", ulWidht+'px' );
		ul.css( "marginLeft", '0px' );
		
		$('#featured-next').click(function(){
			if(liCurrent < (liCount-5)){
				liCurrent++;
				$('#slider').animate({marginLeft: ((-1) * liWidth * liCurrent)+'px'}, 500);
			}
			return false;
		});

		$('#featured-back').click(function(){
			if(liCurrent > 0){
				liCurrent--;
				$('#slider').animate({marginLeft: ((-1) * liWidth * liCurrent)+'px'}, 500);
			}
			return false;
		});
		
	}
}

/**
 *	init Comments form
 */ 
$(document).ready(function(){
	if(!document.getElementById("comment_form")) return;
	
	$("#comment_form input").each( function(i){
		if($(this).attr("type") == "text" && $(this).attr("alt") != ""){
			var parts = String($(this).attr("alt")).split(':');
			$(this).attr('default_value', jQuery.trim(parts[1])); 
			$(this).focus( function(e){
				if(jQuery.trim($(this).val().toLowerCase()) == jQuery.trim($(this).attr('default_value').toLowerCase())){
					$(this).val('');
				}
			});
			$(this).blur( function(e){
				if(this.value == ''){
					$(this).val($(this).attr('default_value'));
				}
			});
		}
	});
});

/**
 *	category headlines ajax
 */ 
$(document).ready(function(){
	if(typeof(currentCategory) != 'undefined' && currentCategory != ''){
	  
		var html = $.ajax({
	    type: "GET",
	    url: PROJECT_DIRECTORY + "scripts/xml-content-parser.php",
	    data: "file_name=category_headline&category=" + currentCategory,
	    async: false
	  }).responseText;
	  
	  $("#leftcol .news-headlines").append(html);
		
	}
});
/**
 *	category headlines popular online ajax
 */
$(document).ready(function(){
	if(typeof(currentCategory) != 'undefined' && currentCategory != ''){

		var html = $.ajax({
	    type: "GET",
	    url: PROJECT_DIRECTORY + "scripts/xml-content-parser.php",
	    data: "file_name=popular_online&category=" + currentCategory,
	    async: false
	  }).responseText;

	  $("#leftcol .popular-online").append(html);

	}
});
/**
 *	category headlines exclusive ajax
 */
$(document).ready(function(){
	if(typeof(currentCategory) != 'undefined' && currentCategory != ''){

		var html = $.ajax({
	    type: "GET",
	    url: PROJECT_DIRECTORY + "scripts/xml-content-parser.php",
	    data: "file_name=exclusive&category=" + currentCategory,
	    async: false
	  }).responseText;

	  $("#leftcol .exclusive").append(html);

	}
});
