function getUrlParameter(name){name=name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");var regexS="[\\?&]"+name+"=([^&#]*)";var regex=new RegExp(regexS);var results=regex.exec(window.location.href );if(results==null )return "";else return results[1];}
function isNumeric(value){if(value==null||!value.toString().match(/^[-]?\d*\.?\d*$/))return false;return true;}
function getRadioValue(name){var radios=document.getElementsByName(name);var length=radios.length;for(var i=0;i<length;i++)if(radios[i].checked)return radios[i].value;}
Array.prototype.in_array=function(p_val){for(var i=0,l=this.length;i<l;i++){if(this[i]==p_val){return true;}}return false;}
//function initShadowbox(){Shadowbox.init({skipSetup:true,overlayOpacity:0.7});}

var url = 'http://classicrock.candlewoodsuites.com';
var aSug = '';
var inputValues = new Array('First Name','Last Name','Email Address','Priority Club'+unescape('%u00AE')+' Member Number*','Favorite Classic Rock Band');
var bandOrder = '';
var timer = '2000';

$(document).ready(function() {
	if($('ul#error').length) {
		$('ul#error').dialog({
			bgiframe: true,
			modal: true,
			resizable: false,
			width: 400,
			buttons: {
				OK: function(){
					$(this).dialog('close');
				}
			}
		
		});
		$('p.error').hide();
		clearHighlights();
	}
	$('#scrollpane').height('470px').jScrollPane();
	$('#facebook').click(function() {window.open(this.href,'sharer','toolbar=0,status=0,width=626,height=436');return false;});
	$('ol#formentry input').focus(function() {if(inputValues.in_array(this.value)) {this.value='';}})
						   .blur(function() {if(this.value=='') {this.value=this.defaultValue};});
	ajaxButtons();
	initAutoSuggest();
	var options = { 
        target:			'#bandResults',
        success:		handleBandSubmit,
        url:			url+'/js_functions.php?type=bandSubmit&page='+$('#enc').attr('class')
	}; 
	$('#favband').ajaxForm(options);
	//if(typeof document.body.style.maxHeight === "undefined") setTimer();
});

function initAutoSuggest() {
	var options_xml = {
		//script: function (input) {return url+"/auto_suggest.php?input="+input+"&testid="+$('#testid').val();},
		script: function (input) {return url+"/auto_suggest.php?input="+input;},
		varname:"input",
		delay:100,
		timeout:20000
	};
	aSug = new bsn.AutoSuggest('favband',options_xml);
}

function handleBandSubmit(responseText,statusText) {
	if(statusText=='success') {
		var array = responseText.split('|||');
		var html;
		aSug.clearSuggestions();
		$('#band').val('');
		switch(array[0]) {
			case 'Verify':
				html = '<p>Is this the band you would like to vote for?</p>' + array[1];
				break;
			case 'List':
				html = '<p>Your search returned the following bands. Select the band you would like to vote for:</p>' + array[1];
				break;
			default:
				html = array[1];
				break;
		}
		$('#bandResults').html(html).fadeIn('slow');
	}	
}

function ajaxButtons() {
	if($('#bandlist').length==0) return;
	$.each($('#bandlist td.vote'), function() {
		bandOrder += this.id.substr(4);
	});
	$('#bandlist a').livequery('click',function() {
		var vote = getVoteInfo(this.href);
		$.ajax({
		    url: 'js_functions.php?type=Vote',
		    type: 'POST',
		    data: 'vote='+vote[1]+'&id='+vote[0],
		    timeout: 1000,
		    error: function(){alert('Vote failed. Please try again');},
		    success: function(status) {
		        var array = status.split('|||');
				if(array[0] == 'success') {
					$('#vote'+vote[0]).addClass('voted').html('<img class="voted" alt="Voted" src="'+url+'/images/layout/image-VoteCheckmark.png" style="_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=\'true\',src=\''+url+'/images/layout/image-VoteCheckmark.png\');_padding-top:16px;" />');
					if (array[1] != bandOrder) {
						if(typeof document.body.style.maxHeight === "undefined") {
							var votes = $('#votes' + vote[0]).html();
							if (vote[1] == 'vote-up') var votes = (votes * 1) + 1;
							else var votes = (votes * 1) - 1;
							$('#votes' + vote[0]).html(votes);
						}else reorderList();
					}
					else {
						var votes = $('#votes' + vote[0]).html();
						if (vote[1] == 'vote-up') var votes = (votes * 1) + 1;
						else var votes = (votes * 1) - 1;
						$('#votes' + vote[0]).html(votes);
					}
				}
		    }
		});
		return false;
	});
}

function reorderList() {
	$.get('js_functions.php?type=Reorder',function(html) {
		$('#bandlist').html(html);
	});
}

function getVoteInfo(url) {
	var params = url.split('/').reverse();
	return params;
}

function submitBand(bandID) {
	$.post(url+'/js_functions.php?type=bandID&page='+$('#enc').attr('class'),{id:bandID},function(html){
		aSug.clearSuggestions();
		$('#band').val('');
	    $('#bandResults').html(html).fadeIn('slow');
	});
}

function clearHighlights() {
	$('ol#formentry input.highlight').change(function() {
		if(this.value != this.defaultValue) $(this).css('background-position','0 0').css('color','#5b5b5b');
		else $(this).css('background-position','0 -18px').css('color','#660000');
	});
}

function setTimer() {
	setInterval(reorderList,timer);
}