/**
 * FirebugX
 */
(function(){
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}
})();

/**
 * Twitter Plugin
 *
 */
(function($) {
 
  $.fn.tweet = function(o){
    var s = {
	  user : 'storvoinc',
      count: 5
    };

    $.fn.extend({
      linkUrl: function() {
        var returning = [];
        var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
        this.each(function() {
          returning.push(this.replace(regexp,"<a href=\"$1\" target=\"_blank\">$1</a>"))
        });
        return $(returning);
      },
      linkUser: function() {
        var returning = [];
        var regexp = /[\@]+([A-Za-z0-9-_]+)/gi;
        this.each(function() {
          returning.push(this.replace(regexp,"<a href=\"http://twitter.com/$1\">@$1</a>"))
        });
        return $(returning);
      },
      linkHash: function() {
        var returning = [];
        var regexp = / [\#]+([A-Za-z0-9-_]+)/gi;
        this.each(function() {
          returning.push(this.replace(regexp,"<a href=\"http://search.twitter.com/search?q=&tag=$1&lang=all\">#$1</a>"))
        });
        return $(returning);
      }
    });


	function custom_date(time_value) {
		var regExp = new RegExp('([a-zA-Z]*) ([a-zA-Z]*) ([0-9]+) ([0-9]+)\:([0-9]+)\:([0-9]+) (\\+[0-9]*) ([0-9]{4})', 'g');
		var theDay = time_value.replace(regExp, '$3');
		var theMonth = time_value.replace(regExp, '$2');
			switch(theMonth){
				case 'Jan' : theMonth = '01'; break;
				case 'Feb' : theMonth = '02'; break;
				case 'Mar' : theMonth = '03'; break;
				case 'Apr' : theMonth = '04'; break;
				case 'May' : theMonth = '05'; break;
				case 'Jun' : theMonth = '06'; break;
				case 'Jul' : theMonth = '07'; break;
				case 'Aug' : theMonth = '08'; break;
				case 'Sep' : theMonth = '09'; break;
				case 'Oct' : theMonth = '10'; break;
				case 'Nov' : theMonth = '11'; break;
				case 'Dec' : theMonth = '12'; break;
			}
		var theYear = time_value.replace(regExp, '$8');

		return theDay+'/'+theMonth+'/'+theYear;
	}
	$(this).find('ul').remove();
    return this.each(function(){
      var list = $('<ul>').appendTo(this);
	  var url = 'http://api.twitter.com/statuses/user_timeline.json?screen_name='+s.user+'&count='+s.count+'&include_rts=true&callback=?';
	  
      $.getJSON(url, function(data){
		$.each(data, function(i,item){
          var text  = '<p>'+$([item.text]).linkUrl().linkUser().linkHash()[0]+'</p>';
          list.append('<li>' + text + '</li>');
        });
      });
    });
  };
})(jQuery);


$(document).ready(function(){
/**
 * Mage Cart and Account
 */
$('#shoppingbag').empty().load('/shop/blog_items-cart.php');

/**
 * Twitter Feed
 */
$("#twitter").tweet();
setInterval(function(){$("#twitter").tweet();},60000);
 

/**
 * Init Image Scroller, only in Home
 */
	if($('#image-scroller').length){
		imageScroller();	
	}
/**
 * Init Share functionality
 */
 	$('.share').each(function(){
		$(this).click(function(e){
			e.preventDefault();
			if($(this).prev().css('display') == 'none')
			{
				$(this).prev().fadeIn('fast');
			}
			else
			{
				$(this).prev().fadeOut('fast');
			}
		});						  
	});
	
	$("#newsletter form").bind("submit", function(event){
		event.preventDefault();
		var emailpattern = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
		if($('[name=email]').attr("value").match(emailpattern) == null)
		{
			alert("E-mail invalido, por favor verifique.");
			$('[name=email1]').focus();
			return false;
		}
		else
		{
			$.post('/wordpress/wp-content/themes/storvo/newsletter.php', $("#newsletter form").serialize(),function(msg){
				if(msg == 'sucesso'){
					alert('E-mail cadastrado com sucesso!');
				}else{
					alert('Erro ao cadastrar e-mail, por favor tente novamente mais tarde.');
				}
			});
			return false;
		}
	});

/**
 * Set navigation link in footer
 */
 /*
	if($('#nav-below').length){
		if($('#nav-below .nav-previous a').length){
			$('#nav-footer .nav-previous').attr('href',$('#nav-below .nav-previous a').attr('href'));
		} else {
			$('#nav-footer .nav-previous').hide();
			$('#nav-footer span').hide();
		}
		if($('#nav-below .nav-next a').length){
			$('#nav-footer .nav-next').attr('href',$('#nav-below .nav-next a').attr('href'));
		} else {
			$('#nav-footer .nav-next').hide();
			$('#nav-footer span').hide();
		}
		$('#nav-footer').show();
	}
 */
});


/**
* Image Scroller
*
*/
function imageScroller() {
	$('#image-scroller ul li:first').addClass('show').css({opacity: 1.0});
	setInterval('imageScrollerChange()',5000);
}
function imageScrollerChange() {	
	var current = ($('#image-scroller ul li.show')?  $('#image-scroller ul li.show') : $('#image-scroller ul li:first'));
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('#image-scroller ul li:first') :current.next()) : $('#image-scroller ul li:first'));	
	next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	current.animate({opacity: 0.0}, 1000).removeClass('show');
};
