
//set to false to stop rotation, true to enable rotation
var rotate = true; 


var splash_url = 'http://my.democrats.org/demsplash-sotu';	// SPLASH PAGE URL
//var splash_url = 'http://my.democrats.org/page/content/votesplash';
var cookie_time = 2;							// COOKIE DURATION IN DAYS
 
function get_URL_param (name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}
 
function checkCookie() {
  var date = new Date();
  date.setTime(date.getTime()+(cookie_time*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
  if (document.cookie.indexOf("demsplash") < 0 && document.cookie.indexOf("bsduser") < 0) {
    document.cookie = "demsplash=true"+expires;
    if(!get_URL_param('nosplash')){
      document.location = splash_url;
    }
  }
}

checkCookie();

stories = {
 
  load:function() {
  
	this.sSorts = jQuery(".hStory");

	this.sSorts.sort(function(a, b) {
	   var compA = jQuery(a).attr("id");
	   var compB = jQuery(b).attr("id");
	   return (compA > compB) ? -1 : (compA < compB) ? 1 : 0;
	});	

	jQuery("#hStoryBox").empty().show();

	jQuery.each(this.sSorts, function(i, itm){jQuery("#hStoryBox").append(itm);});

	this.resort("All");
	
  },
  
  resort:function(what) {

		if (what == "All") { 
			var faders = jQuery(".hStory"); 
		} 
		else {  
			var faders = jQuery('.'+what);
		 }
		jQuery(".hStory").hide();
		number = 1000/faders.length;

		faders.each(function (i) {jQuery(this).fadeIn((number*i)+1);});
	}

};


feature = {

  load:function(speed) {
	this.speed = (speed === undefined) ? 4000 : speed;
	features = jQuery("#homeFeature > div");	
	titles = jQuery("#homeFeatureTitles > div");
	links = jQuery("#homeFeatureTitles > div > a");
	this.featNum = features.length-1;	
	this.pointer = 0;
	jQuery("#homeFeature > div:not(:first-child)").hide();
	jQuery("#homeFeatureTitles > div:not(:first-child)").hide();	
	jQuery(".featureButOff").eq(0).addClass("featureButOn");

    var self = this;
	if (this.featNum !== 0) {  
	
		
			if (rotate) { jQuery(".featureButOn").fadeTo(this.speed, 0.6);	}	
		

		
		jQuery(".featureButOff").each(function (i, el) {
			jQuery(this).click(function() {self.clickItem(i);});
		});
	}
	
	jQuery(".fhomeShareThis").each(function (i, el) {
		jQuery(this).click(function(event) {
			event.preventDefault();
			event.stopImmediatePropagation();		
			event.stopPropagation();
		});
	});
	
	/*
	features.each(function(i, el){
		jQuery(this).click(function() {
			//links.eq(i).click(); // not possible
			//console.log(links.eq(i).text());
			// window.location = links.eq(i).attr("href"); //won't target blank properly
		});	
	});	

	
	jQuery(".fhomeFBShare").each(function (i, el) {
		jQuery(this).click(function(event) {
			event.preventDefault();
			event.stopImmediatePropagation();		
			event.stopPropagation();		
			u = jQuery(this).attr("href");
			t = jQuery(this).attr("title");
			window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
			clearInterval(self.changer);
		});
	});
	
	jQuery(".fhomeTWShare").each(function (i, el) {
		jQuery(this).click(function(event) {
			event.stopPropagation();
			clearInterval(self.changer);			
		});
	});	
	*/
	links.each(function(i, el){
		jQuery(this).click(function() {});});

	
		if(this.featNum !== 0 && rotate) { this.changer = setInterval("feature.nextItem()", this.speed); }
	
  },
  
  nextItem:function() {
	var self= this,
	next = (this.pointer === this.featNum) ? 0 : this.pointer+1;
	jQuery(".featureButOn").stop(true,true).fadeTo('fast',1);
	jQuery(".featureButOff").removeClass("featureButOn")
	jQuery(".featureButOff").eq(next).addClass("featureButOn");
	jQuery(".featureButOn").fadeTo(self.speed, 0.6);		
	features.eq(this.pointer).fadeOut(400);
	features.eq(next).css('background-image',features.eq(next).css('background-image')).fadeIn(800,function(){features.css({'z-index':'3'});
		$(this).css({'z-index':'5'});
	});
	titles.eq(this.pointer).fadeOut(400);
	titles.eq(next).fadeIn(800);
	this.pointer = next;
  },
  
  clickItem:function(next) {
	clearInterval(this.changer);
  	jQuery(".featureButOff").removeClass("featureButOn");
	jQuery(".featureButOff").eq(next).addClass("featureButOn").stop(true,true).fadeTo(1,1);
	features.eq(this.pointer).fadeOut(90);
	features.eq(next).css('background-image',features.eq(next).css('background-image')).fadeIn(180,function(){features.css({'z-index':'3'});
		$(this).css({'z-index':'5'});
	});
	titles.eq(this.pointer).fadeOut(90);
	titles.eq(next).fadeIn(180);	
	this.pointer = next;	
  }  
  
};



jQuery(document).ready(function() {


	feature.load(6000);
	stories.load();	
	

	jQuery(".hAll").click(function (event) {
		event.preventDefault();
		stories.resort('All');
	});

	jQuery(".hBlogs").click(function (event) {
		event.preventDefault();
		stories.resort('hBlog');
	});

	jQuery(".hVideos").click(function (event) {
		event.preventDefault();
		stories.resort('hVideo');
	});	

	jQuery(".hLinks").click(function (event) {
		event.preventDefault();
		stories.resort('hLink');
	});	

});
