jQuery(document).ready(function($) {
	init();
	scrollTo(0,0)
});

function init(){
	rnd.today=new Date();
	rnd.seed=rnd.today.getTime();
	
	if ($('body').attr('id') == 'Index'){
		
		$('#home-slideshow').shuffle();
		
		var random  = Math.floor(Math.random()*$('#home-slideshow').children().length);
		$('#home-slideshow').cycle({
			timeout: 4000 + 300 * rand(10),
			delay: rand(5) * 1000 - 4000,
			speed: 1500,
			startingSlide: random
		});
		
	}
	
	$("div#sidenav").css({
		height: $('#content #right-bg').height()
	});
	
}

function rnd(){
	rnd.seed = (rnd.seed*9301+49297) % 233280;
	return rnd.seed/(233280.0);
}

function rand(number){
	return Math.ceil(rnd()*number);
};

$(function(){	
	$(document).pngFix();
	
	$('a').click(function(){this.blur()});
	$(".mp3player").media( { width: 300, height: 20 } );

});


// jQuery.Randomizer = {
// 	build: function(images, imgdir) {
// 		$(this).attr('src',imgdir + images[Math.ceil(Math.random()*images.length)-1]);
// 	}
// };

// jQuery.fn.Randomize = jQuery.Randomizer.build;

jQuery.fn.minHeight = function()
{
	this.each(function(){		
		var mh = $(this).css('min-height');
	    var mhsize = mh.replace(/px/,'');
	    var h = $(this).height();
		
	    if (h < mhsize) $(this).css('height',mh);
	});
    
};


jQuery.merchantMap = {
	clearTip: function()
	{	
		if ( $('#instructions') && !$(this).attr('href') )
			$('#instructions').remove();

		if ($('#nameTip')) $('#nameTip').remove();
	},
	highlight: function( mid ) {
		
		var areas = $('area[merchant_id="'+mid+'"]');
		
		areas.each(function(){
			var coords = $(this).attr('coords').split(',');
			var imgx = $('#imgmap')[0].offsetLeft;
			var imgy = $('#imgmap')[0].offsetTop;
			
			var x = parseInt(coords[0].trim());
			var y = parseInt(coords[1].trim());
			
			jQuery.merchantMap.clearTip();
			$('#imgmap').after('<div id="nameTip" style="display: none;"><div class="merchantsTip">' + $(this).attr('title') + '</div></div>');
			
			$('#nameTip').css({position: 'absolute',left: (x + imgx + 1) +'px', top: (y + imgy - 25) +'px', zIndex: 5000, display: 'block' });
			
		})
	}
};

(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);

livesearch = {
  t: 0,
  qCache: '',
  search: function() {
	var q = $('#msearch').val();
	if (this.qCache == q) return false;
    if(q != '')
    {
		this.qCache = q;
        $('#spinner').show();
        $('#results').load( webroot + 'merchants/search/' + q, function(){
          $('#spinner').hide();
        });
    }
    else
    {
        $('#results').empty();
    }
  }
};

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};
