// =================== Searchbox functions =================== 
var Searchbox = { 
  // Clears the main search box if the value is set to 'Search products...'	
  ClearBox:function(obj){
    try{
      var SearchValue = $F(obj).strip();
      if(SearchValue == "Search products..."){
          obj.value = "";
      }
    }catch(e){
      alert("ClearBox exception: " + e)
    }
  },

  // Populates the main search box if the value is set to ''   
  PopulateBox:function(obj){
    try{
      var SearchValue = $F(obj).strip();
      if(SearchValue == ""){
          obj.value = "Search products...";
      }
    }catch(e){
      alert("PopulateBox exception: " + e)
    }
  }
}

// =================== Carousel Setup Functions =================== 
var Carousel = {
	// This needs to be hooked up to the back end so that the attribute 'url' returns items via an ajax call.
	// See http://docs.prototype-ui.com/rc0/Carousel/AjaxCarousel for more documention.
	
	SetHomeCarousel:function() {
		// Left this is for the dynamic stuff
		//
		// var carousel = null;
		// carousel = new UI.Ajax.Carousel("carousel", {url: "js/more_product_details.js", elementSize: 143})
		// .observe("request:started", function() {
		// $('spinner').show().morph("opacity:0.8", {duration:0.5});
		// })
		// .observe("request:ended", function() {
		// $('spinner').morph("opacity:0", {duration:0.5, afterFinish: function(obj) { obj.element.hide(); }});
		// });
		try{	
		  new UI.Carousel("carousel", {elementSize: 226});
		}catch(e){
      alert("SetHomeCarousel exception: " + e)
    }
	},
	
		SetPageCarousel:function() {
		// Left this is for the dynamic stuff
		//
		// var carousel = null;
		// carousel = new UI.Ajax.Carousel("carousel", {url: "js/more_product_details.js", elementSize: 143})
		// .observe("request:started", function() {
		// $('spinner').show().morph("opacity:0.8", {duration:0.5});
		// })
		// .observe("request:ended", function() {
		// $('spinner').morph("opacity:0", {duration:0.5, afterFinish: function(obj) { obj.element.hide(); }});
		// });
		try{	
		  new UI.Carousel("carousel", {elementSize: 174});
		}catch(e){
      alert("SetPageCarousel exception: " + e)
    }
	},
	
	SetMediaCarousel:function() {
		// Left this is for the dynamic stuff
		//
		// var carousel = null;
		// carousel = new UI.Ajax.Carousel("carousel", {url: "js/more_product_details.js", elementSize: 143})
		// .observe("request:started", function() {
		// $('spinner').show().morph("opacity:0.8", {duration:0.5});
		// })
		// .observe("request:ended", function() {
		// $('spinner').morph("opacity:0", {duration:0.5, afterFinish: function(obj) { obj.element.hide(); }});
		// });
		try{
		  media_carousel = new UI.Carousel("media-carousel", {elementSize: 200});
		}catch(e){
      alert("SetMediaCarousel exception: " + e)
    }
	}
}

// =================== Twitter Callback functions =================== 
var Twitter = {
  Callback:function(twitters) {
    var statusHTML = [];
    for (var i=0; i<twitters.length; i++){
      var username = twitters[i].user.screen_name;  
	  var linked = twitters[i].text.match(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g);
	  var status;
	  if(linked!=null){
		status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
	        return '';});
	  	status = '<a href="'+linked+'">' + Twitter.TrimStatus(status,80) + '</a>';
	  }else{
	  	status = Twitter.TrimStatus(twitters[i].text,80);
	  }
      //var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      //  return '<a href="'+url+'">'+url+'</a>';
      //}).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      //  return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
      //});
	  //statusHTML.push('<li>'+status+'<span><a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+Twitter.RelativeTime(twitters[i].created_at)+' from ' + twitters[i].source +'</a></span></li>');
      statusHTML.push('<li>'+status+'<span><a href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+Twitter.RelativeTime(twitters[i].created_at)+' from ' + twitters[i].source +'</a></span></li>');
    }
    document.getElementById('tweets').innerHTML = statusHTML.join('');
  },
  
  TrimStatus:function(value,len){
  	if(value.length>len){
		return value.substring(0,len) + "...";
	}
	return value;
  },
  
  SmallDate:function(value){
  	if(value<10){
		return "0"+value;
	}
  	return value;
  },

  RelativeTime:function(time_value) {
    var values = time_value.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var parsed_date = Date.parse(time_value);
	// Small date function
	dt = new Date(time_value);
	return Twitter.SmallDate(dt.getDate())+ '.' + Twitter.SmallDate(dt.getMonth()+1) + '.' + (dt.getFullYear() + "").substring(2,4);
	//
    var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
    var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
    delta = delta + (relative_to.getTimezoneOffset() * 60);

    if (delta < 60) {
      return 'less than a minute ago';
    } else if(delta < 120) {
      return 'about a minute ago';
    } else if(delta < (60*60)) {
      return (parseInt(delta / 60)).toString() + ' minutes ago';
    } else if(delta < (120*60)) {
      return 'about an hour ago';
    } else if(delta < (24*60*60)) {
      return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
    } else if(delta < (48*60*60)) {
      return '1 day ago';
    } else {
      return (parseInt(delta / 86400)).toString() + ' days ago';
    }
  }
}
// =================== Global Warning Drop Down Select functions =================== 
var GlobalWarning = {
	FilterList:function(DropDown){
		var text = $(DropDown).getValue();
		if(text == 'All'){
			$('agreements-list').select('li').each(function(s){
				s.show();
			});
		}else{
			$('agreements-list').select('li').each(function(s){
				if(s.innerHTML.include(text.escapeHTML())){
					s.show();
				}else{
					s.hide();	
				}
			});
		}	
	}
}

