$(document).ready(function(){ 

	$(document).pngFix();
	$("ul.navigation").superfish();	
	$("div.roundcorners").corner();
	
	$("table.sorted").tablesorter();
	
	$("table.sorted").bind("sortStart",function() {         
		$("table.sorted tr.alternate").removeClass('alternate');     
	}).bind("sortEnd",function() {         
		$("table.sorted tr:even").addClass('alternate');     
	}); 
	
	
	$("input.search-button").hover(
      function () {
		$(this).attr('src', $(this).attr('src').replace('off','on') );
      }, 
      function () {
        $(this).attr('src', $(this).attr('src').replace('on','off') );
      }
    );
	
	$("img.slide-button").hover(
      function () {
		$(this).attr('src', $(this).attr('src').replace('off','on') );
      }, 
      function () {
        $(this).attr('src', $(this).attr('src').replace('on','off') );
      }
    );
	
	$('#q').focus(function(){
	  if($(this).val() == 'Search Our Site') {
		  $(this).attr('value','');
	  }
	});
	$('#q').blur(function(){
	  if($(this).val() == '') {
		  $(this).val('Search Our Site');
	  }
	});
	
	$("a.pager-prev").hover(
	  function () {
		$(this).children('img:first').attr('src', $(this).children('img:first').attr('src').replace('off','on') );
	  }, 
	  function () {
		$(this).children('img:first').attr('src', $(this).children('img:first').attr('src').replace('on','off') );
	  }
	);
	
	$("a.pager-pause").hover(
	  function () {
		$(this).children('img:first').attr('src', $(this).children('img:first').attr('src').replace('off','on') );
	  }, 
	  function () {
		$(this).children('img:first').attr('src', $(this).children('img:first').attr('src').replace('on','off') );
	  }
	);
	
	$("a.pager-next").hover(
	  function () {
		$(this).children('img:first').attr('src', $(this).children('img:first').attr('src').replace('off','on') );
	  }, 
	  function () {
		$(this).children('img:first').attr('src', $(this).children('img:first').attr('src').replace('on','off') );
	  }
	);
	
	$('.slide-rotation').cycle({ 
		fx:     'fade', 
		speed:  500, 
		timeout: 8000, 
		next:   '.slide-next', 
		prev:   '.slide-previous',
		before:  onBefore
	});
	
	if($.browser.msie && parseInt($.browser.version) == 6) {
		$('.sub-headdisplay').css('margin-top','-20px');
		$('.header').cycle({
			fx: 'wipe',
			speed:  50,
			timeout:  4000,
			pager: '.pager-nav',
			next:   '.pager-next', 
			prev:   '.pager-prev',
			before:  onBeforeHeader
		});
	} else {
		$('.header').cycle({
			fx: 'fade',
			speed:  500,
			timeout:  4000,
			pager: '.pager-nav',
			next:   '.pager-next', 
			prev:   '.pager-prev',
			before:  onBeforeHeader
		});
	}
	
	$("a.pager-pause").click(
	  function () {
		$('.header').cycle('pause');
	  }
	);
	
	$('.pager-nav a:eq(0)').addClass('head-buy-tickets-on');
	
	$('.eventCategorySelect').change(function(){
	  var title = $(this).attr('title');
	  var name = $(this).attr('name');
	  window.location.href = "/events/search/d:" + title + "/category:" + $(this).children("[@selected]").val(); 
	});
	
	$("a.add-item").click(
	  function () {
		var num_items = 1;
		var old_num_items;
		var newitem;
		if($(this).next('input').val() == '') {
			num_items = 1;
		} else {
			num_items = Number($(this).next('input').val());
		}
		old_num_items = num_items;
		num_items += 1;
		$(this).next('input').val(num_items);
		if(num_items == 5) {
			$(this).hide();
		}
		newitem = $(this).prev('table').clone();
		$(newitem).find('input,textarea').each(function (i) {
			$(this).attr('name',$(this).attr('name').replace('_'+old_num_items,'_'+num_items));	
		});
		newitem.find('input,select').val('');
		newitem.insertBefore(this);
	  }
	);
	
	$("#same_as_current").click(
		function () {
			if(this.checked) {
				$("#permanent_address input,#permanent_address select").each(function (i) {
					$(this).val( $('#'+$(this).attr('id').replace('permanent_','current_')).val() );
				});
			} else {
				$("#permanent_address input,#permanent_address select").val('');
			}
		}
	);
	
}); 

function onBefore(curr, next, opts) { 
	var index = $(this).parent().children().index(this);
	index++;
	$('.slide-index').html(index);
}

function onBeforeHeader(curr, next, opts) { 
	var index = $(this).parent().children().index(this);
	var hZindex = Number($(this).css('z-index'));
	var hDindex = (hZindex+1);
	var hSCindex = (hDindex+1);
	$('.sub-headdisplay').css('z-index',hSCindex);
	$('.pager-nav a').removeClass('head-buy-tickets-on');
	$('.pager-nav a:eq('+index+')').addClass('head-buy-tickets-on');
	$('div.header-slide-content').css('display','block');
	$('.sub-headdisplay').css('display','block');
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.,$";
   var IsNumber = true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
      	IsNumber = false;
      }
   }
   if(sText.length == 0) {
   		IsNumber = false;
   }
   return IsNumber;
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}

function formatNumber(data) {
	if(IsNumeric(data)) {
		data = String(data).replace(/[\$,]/g,'');
		var temp = data.split(".");
		if(temp[1] && temp[1].length>2)
			temp[1]=temp[1].substr(0,2);
	 
		var output="";
		var re=/(\d{1,3})$/;
		while( temp[0].match( re ) )
		{
			output = "," + RegExp.$1 + output;
			temp[0] = temp[0].replace( re, '' );
		}
		if("undefined" !== typeof(temp[1]) )
		{
			output += "." + String(temp[1]);
		}
		output = output.substring(1);
		return output;
	} else {
		return data;
	}
}
