var register = {
	changeTab: function(tab) {
		$(".register_tabs span").toggleClass("selected");
		if(tab == 1) {
			$(".register_form").css({"display":"block"})
			$(".login_form").css({"display":"none"})
		} else {
			$(".register_form").css({"display":"none"})
			$(".login_form").css({"display":"block"})		
		}
	}
}

var promo = {
	slide: function(btn) {
	if(btn == 1) {
		$(".promo_slider").animate({"left":"0px"});
		$("#btn3").attr({src:"http://70.32.121.226/wp-content/themes/collab/images/btn_carousel.gif"});
		$("#btn2").attr({src:"http://70.32.121.226/wp-content/themes/collab/images/btn_carousel.gif"});
		$("#btn1").attr({src:"http://70.32.121.226/wp-content/themes/collab/images/btn_carousel_on.gif"});
		}
	if(btn == 2) {
		$(".promo_slider").animate({"left":"-999px"});
		$("#btn3").attr({src:"http://70.32.121.226/wp-content/themes/collab/images/btn_carousel.gif"});
		$("#btn2").attr({src:"http://70.32.121.226/wp-content/themes/collab/images/btn_carousel_on.gif"});
		$("#btn1").attr({src:"http://70.32.121.226/wp-content/themes/collab/images/btn_carousel.gif"});		
		}	
	if(btn == 3) {
		$(".promo_slider").animate({"left":"-1998px"});
		$("#btn3").attr({src:"http://70.32.121.226/wp-content/themes/collab/images/btn_carousel_on.gif"});
		$("#btn2").attr({src:"http://70.32.121.226/wp-content/themes/collab/images/btn_carousel.gif"});
		$("#btn1").attr({src:"http://70.32.121.226/wp-content/themes/collab/images/btn_carousel.gif"});		
		}	
	}

}

sfHover = function() {
/*
	var sfEls = document.getElementById("topnav").getElementsByTagName("LI");

	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" over";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" over\\b"), "");


		}
	}
*/
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

var root = "/";
var footer = {
	form: function(idx) {	
		var dataError = false;
		var userName = document.getElementById("name"+idx).value;
		var userEmail = document.getElementById("email"+idx).value;
		
		
		if ( userName == "" ) {
			$('#span_name'+idx).show();
			dataError = true;
		}
		else {
			$('#span_name'+idx).hide();
		}
		
		if ( userEmail == "" ) {
			$('#span_email'+idx).text('( Required )');
			$('#span_email'+idx).show();
			dataError = true;
		}
		else if ( !isValidEmail( userEmail ) ) {
			$('#span_email'+idx).text('( Invalid )');
			$('#span_email'+idx).show();
			dataError = true;
		}
		else {
			$('#span_email'+idx).hide();
		}
		
		
		if ( !dataError ) {
		
		//alert($('#registerForm').find('input').serialize());
		//{"name": document.getElementById("name").value }
		if ( idx == 1 ) {
			$.post(root+"collab_ajax/register.php", 
				   {name: userName, email: userEmail }, 
					function(data){ 
							$(".bottom_form").fadeOut("medium", function() {
							$(".bottom_form").html("<p>We've received your information, thanks!</p>");
							$(".bottom_form").fadeIn("medium");
							});				
					});
			
		}
		else {
			$.post(root+"collab_ajax/register.php", 
				   {name: userName, email: userEmail }, 
					function(data){ 
							$(".register_form").fadeOut("medium", function() {
							$(".register_form").html("<br/><br/><h2>We've received your information, thanks!</h2>");
							$(".register_form").fadeIn("medium");
							});	
							var redirectURL = parent.document.getElementById("url_container").innerHTML;
							if ( redirectURL == "" ) {
								parent.window.location=root;
							}
							else {
								parent.window.location=redirectURL;
							}
					});
			

		}
		
		}
	},
	
	modal: function(state,url) {
		document.getElementById("url_container").innerHTML = url;
		if(state == 1) {
			$.blockUI({ css: { visibility: 'hidden', display: 'none' } });
			$('#register_iframe').fadeIn('fast');
		}
		if(state == 2) {
			$.unblockUI();
			$('#register_iframe').fadeOut('fast');
		}		
	}
}

var isValidEmail = function(email) {

	var email_pattern = /(\w+)@([0-9a-zA-Z_\-]+?\.[a-zA-Z]{2,3})/i;
	var matched = email.match(email_pattern);
		
	if ( matched == null || matched < 0 ) {
		return false;
	}
	else {
		return true;
	}
}


var Dachis = window.Dachis || {};

Dachis.StreamManager = {
	refreshInterval: 60000,
	//feedURI: 'http://www.dachisgroup.com/collab_ajax/getFeed.php',
        feedURI: '/collab_ajax/getFeed.php',
	//feedURI: 'http://70.32.121.226/wp-content/themes/collab/js/dummyFeed.php',
	isLoading: false,
	stream: {type1:[], type23:[]},
	
	init: function() {
	
		// Get the feed initially
		this.getFeed();
		var that = this;
		// Get feed at this interval					
		window.setInterval(function() {
			that.getFeed();
		}, this.refreshInterval);			
		
	},
			
	getFeed: function() {
		
		if(!this.isLoading) {
			var that = this;
			this.isLoading = true;

/*
 $.ajax({
   type: "GET",
   url: this.feedURI,
   data: {timestamp: this.timestamp, timestamp1: this.timestamp1, timestamp23: this.timestamp23},
   success: function(msg){
     //document.body.innerText = msg;
     that.feedLoaded(msg, "success");
   },
   error: function (XMLHttpRequest, textStatus, errorThrown) {
     alert("error: " + textStatus);
   },
   dataType: "json"

 });
*/

			$.get(this.feedURI, 
					{timestamp: this.timestamp, timestamp1: this.timestamp1, timestamp23: this.timestamp23},
					function(data, status) { that.feedLoaded(data,status); }, 
					"json"	
			);
		}
	},
	
	feedLoaded: function(data, status) {
		this.isLoading = false;
		if(status == "success") {
		
			var that = this;
			this.timestamp = data.timestamp;
			this.timestamp1 = data.timestamp1;
			this.timestamp23 = data.timestamp23;
			$.each(data.results, function() {
				switch(this.type) {
					case 1:
						that.stream.type1.push(this);
					break;
					
					case 2:
						that.stream.type23.push(this);
					break;
						
					case 3:
						that.stream.type23.push(this);
					break;
				}	
			});
			if(data.results.length > 0) {
				$(this).trigger('streamDataLoaded');
			}
		}
	}
};


Dachis.Type1StreamView = {

	container: null,
	
	isInserting: false,

	init: function() {
		var that = this;
		$(Dachis.StreamManager).bind('streamDataLoaded', function() {
			if(!that.isInserting) {
				that.insertNext();			
			}
		});
		
		this.container = $('#type1Stream');
	},
	
	insertNext: function() {
		var items = Dachis.StreamManager.stream.type1;
	
		if(items.length > 0) {
			this.isInserting = true;
			var that = this;
			//Position the UL absolutely, clipped by the container 
			//insert new item
			this.container.prepend(items[0].html);

			// move the top property - 120px
			this.container.css({'top': '-200px'});
			
			// Animate back to 0
			
			this.container.animate({'top': '0px'} , 2000, function() {
				that.container.find('li:last').remove();
				Dachis.StreamManager.stream.type1 = Dachis.StreamManager.stream.type1.slice(1);
				
				setTimeout(function() {
					that.insertNext();
				}, 2000);
				
			});
		} else {
			this.isInserting = false;
		}
	}
}

Dachis.Type23StreamView = {

	container: null,
	
	isInserting: false,

	init: function() {
		var that = this;
		$(Dachis.StreamManager).bind('streamDataLoaded', function() {
			if(!that.isInserting) {
				that.insertNext();			
			}
		});
		
		this.container = $('#type23Stream');
	},
	
	insertNext: function() {
		var items = Dachis.StreamManager.stream.type23;
	
		if(items.length > 0) {
			this.isInserting = true;

			var that = this;

			this.container.prepend(items[0].html);
			window.currentTop = parseInt(this.container.css('top'));
			
			this.container.css({'top': window.currentTop-40 + 'px'});
			
			var breakItems = $('li.break', this.container);
			
			breakItems.each(function() {
				$(this).clone().removeClass('break').addClass('break-copy').insertAfter($(this));
				$(this).css({'margin-bottom': '40px'});
			});
			
			$('li.break-copy', this.container).fadeIn('slow');
			
			this.container.animate({'top': window.currentTop} , 2000, function() {
			
				var breakElements = $('li.break', that.container);
				var breakElementsTotal = breakElements.length-1;
				
				breakElements.each(function(index, value) {
					if(index == breakElementsTotal) {
						var lastItem = true;
					} else {
						var lastItem = false;
					}
			
					$(this).fadeOut(300, function() {
						$(this).removeClass('break').prev('li').addClass('break').css({'margin-bottom': ''});
						$(this).remove();
						if(lastItem) {
							$('li.break-copy', that.container).removeClass('break-copy');
							that.container.find('li:last').remove();
							Dachis.StreamManager.stream.type23 = Dachis.StreamManager.stream.type23.slice(1);
							setTimeout(function() {
								that.insertNext();
							}, 2000);
				
						}
					});
				});
			});
		} else {
			this.isInserting = false;
		}
	}
}


