function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

jQuery.fn.validateForm = function() {
	//	console.log("submit");
	var testedInput = $(this).find('input[validation=true],textarea[validation=true],select[validation=true]');
	//	console.log("submit");
	for(i=0;i<testedInput.length;i++) {
		if($(testedInput[i]).val() == "") {
			$(testedInput[i]).focus();
			alert("Please enter the "+$(testedInput[i]).prev().text());
			return false;			
		}
	}
	return true;
};

$(document).ready(function () {
	
	$.easing.exponential = function (x, t, b, c, d, s) {
		t /= d/2;
		if (t < 1) return c/2 * Math.pow( 2, 10 * (t - 1) ) + b;
		t--;
		return c/2 * ( -Math.pow( 2, -10 * t) + 2 ) + b;
	};

	$.easing.quadratic = function (x, t, b, c, d, s) {
		t /= d/2;
		if (t < 1) return c/2*t*t*t*t + b;
		t -= 2;
		return -c/2 * (t*t*t*t - 2) + b;
	};

	$.easing.sin = function (x, t, b, c, d, s) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	};
	
	$('#id_zip').change(function() {
		$('#id_zip_key').val('');
	});
	
	$('#wfrn a').popupWindow({ 
		height:507, 
		width:822, 
		top:50, 
		left:50 
	}); 
	
	$('a#changeCategory').colorbox({
		innerHeight: 400,
		innerWidth: 1050
	});
	
	if($('input[name=unionName]').val() == 'ALL UNIONS') {
		$('input[name=unionName]').css('color','#999');
	}
	
	$('input[name=unionName]').blur(function() {
		if($('input[name=unionName]').val() == '') {
			$('input[name=unionName]').css('color','#999');
			$('input[name=unionName]').val('ALL UNIONS');
		}
	});
	
	$('input[name=unionName]').focus(function() {
		if($('input[name=unionName]').val() == 'ALL UNIONS') {
			$('input[name=unionName]').val('');
			$('input[name=unionName]').css('color','#000');
		}
	});
	
	if($('input[name=vendorCategory]').val() == 'ALL CATEGORIES') {
		$('input[name=vendorCategory]').css('color','#999');
	}
	
	$('input[name=vendorCategory]').blur(function() {
		if($('input[name=vendorCategory]').val() == '') {
			$('input[name=vendorCategory]').css('color','#999');
			$('input[name=vendorCategory]').val('ALL CATEGORIES');
		}
	});
	
	$('input[name=vendorCategory]').focus(function() {
		if($('input[name=vendorCategory]').val() == 'ALL CATEGORIES') {
			$('input[name=vendorCategory]').val('');
			$('input[name=vendorCategory]').css('color','#000');
		}
	});
	
	$('#topBanner').serialScroll({
		axis: 'x',
		duration: 1300,
		items:'div div', // Selector to the items ( relative to the matched elements, '#sections' in this case )
		stop: true,
		lock: false,
		interval: 30000,
		easing: 'exponential',
		force: true, // Force a scroll to the element specified by 'start' (some browsers don't reset on refreshes)
		constant: false,
		onBefore:function( e, elem, $pane, $items, pos ){
			//currentIndex = pos;
		}
	});
	
	var minimized = true;
	
	$('#mapViewLink,#closeMap').click(function() {
		if(minimized) {
			minimized = false;
			$('#mapViewWindow').animate({
				height: '420px'
			}, 1100,'exponential');
		}
		else {
			minimized = true;
			$('#mapViewWindow').animate({
				height: '0px'
			}, 1100,'exponential');
		}
		return false;
	});
	
	$('#categories ul#mainCategories a.main').click(function() {
		if($(this).siblings('ul').css('display') == 'none') {
			$(this).siblings('ul').css('display','block');
			$(this).parent('li').css('list-style','disc url(images/arrow-d.gif) outside');
		}
		else {
			$(this).siblings('ul').css('display','none');
			$(this).parent('li').css('list-style','disc url(images/arrow-r.gif) outside');
		}
		return false;
	});
	
	$('#moreZipInfo div a#moreInfo').click(function() {
		if($(this).html() == "More Info") {
			$(this).html("Less Info");
			$(this).parents('div').parents('div').children('#expandable').show();
		}
		else {
			$(this).html("More Info");
			$(this).parents('div').parents('div').children('#expandable').hide();
		}
		
		return false;
	});

	$('#showPollResults').click(function() {
		var pollId = $(this).attr('name');
		$('#showPollResults').hide(500);
		
		$.ajax({
		 type: "GET",
		  data: "pollid="+pollId,
		  url: 'ajax/PollResults.php',
		  success: function(data) {			
			$('#PollResults').append(data);	
				
		  }
		});
		return false;
	});
	
	$('#readMoreNews').click(function() {
		$.ajax({
			type: "GET",
			url: 'ajax/readMoreNews.php',
			success: function(data) {			
				$('#newsContent').html(data);
				$('#readMoreNews').hide();
			}
		});
		return false;
	});
	
	$('form:not(#contactForm,#contractForm)').submit(function() {
	//	console.log("submit");
		var testedInput = $(this).find('input[validation=true],textarea[validation=true],select[validation=true]');
	//	console.log("submit");
		for(i=0;i<testedInput.length;i++) {
			if($(testedInput[i]).val() == "") {
				$(testedInput[i]).focus();
				alert("Please enter the "+$(testedInput[i]).prev().text());
				return false;			
			}
		}
		return true;
	});
	
	$('form#contactForm').submit(function() {
		var testedInput = $(this).find('input[validation=true],textarea[validation=true]');
	//	console.log("submit");
		for(i=0;i<testedInput.length;i++) {
			if($(testedInput[i]).val() == "") {
				$(testedInput[i]).focus();
				alert("Please enter the "+$(testedInput[i]).prev().text());
				return false;			
			}
		}
		var inputData = $(this).find('input:not([type=submit]),textarea');
		var datastr = '';
		for(i=0;i<inputData.length;i++) {
			datastr += $(inputData[i]).attr('name') + '=' + $(inputData[i]).attr('value') + '&';
		}
		
		send(datastr);
		return false;

	});
	
	$('#menu h2.shortenedTitle').textOverflow();
	if($('#menu h2.shortenedTitle').html() == ($('#menu h2.fullTitle').html() + "... ")) {
		$('#menu h2.shortenedTitle').css("display","none");
		$('#menu h2.fullTitle').css("display","block");
	}
	
	$('form#contractForm').submit(function() {
		if($(this).validateForm()) {
			var datastr = "";
			
			$(this).children('div').children('input,textarea,select').each(function() {
			//	console.log($(this).attr('name')+' = '+$(this).attr('value'));
				datastr = datastr + '&' + $(this).attr('name') + '='+$(this).attr('value');
				
			});
			
			$('#threeColForm').hide();
			$("#postFormMessage").fadeIn("slow");
			setTimeout(function() {
				sendContract(datastr);
			},1000);
		}
		else return false;
	});
	
		
	$('.locationTypeSelection').change($.changeVisibleLocType);
	
	$('#adType').change(function() {
		if($(this).val() == "TRUE") {
			$('#locations').show(500);
		}
		if($(this).val() == "FALSE") {
			$('#locations').hide(500);
		}
	});
	
	$('#newLocBtn').click(function() {		
		$.ajax({
		 type: "POST",
		  data: "action=new",
		  url: 'Ad_Location.php',
		  success: function(data) {
	    	var selectName = $(data).find('.locationTypeSelection').attr('name');
			var deleteName = $(data).find('.deleteLocBtn').attr('name');						
			
			$(data).insertBefore('#newLocBtnRow');
			
			//alert("DeleteName:"+deleteName+"<br>"+$("#del_" + deleteName).attr('name'));
			
			$("[name=" + selectName + "]").change($.changeVisibleLocType);		
			$("#del_" + deleteName).click($.deleteLoc);			
		  }
		});
		return false;
	});

	$('.deleteLocBtn').click($.deleteLoc);
	
	$('#moduletable_content object embed').mouseenter(function() {
		$(this).parent().parent('#moduletable_content').css('width','285px');
		$(this).parent().parent('#moduletable_content').css('height','160px');
		$(this).parent('object').attr('width','285');
		$(this).parent('object').attr('height','160');
		$(this).attr('width','285');
		$(this).attr('height','160');
	}).mouseleave(function() {
		$(this).parent().parent('#moduletable_content').css('width','154px');
		$(this).parent().parent('#moduletable_content').css('height','88px');
		$(this).parent('object').attr('width','154');
		$(this).parent('object').attr('height','88');
		$(this).attr('width','154');
		$(this).attr('height','88');
	});
	
	$('#moduletable_content object').mouseenter(function() {
		$(this).parent('#moduletable_content').css('width','285px');
		$(this).parent('#moduletable_content').css('height','160px');
		$(this).attr('width','285');
		$(this).attr('height','160');
		$(this).children('embed').attr('width','285');
		$(this).children('embed').attr('height','160');
	}).mouseleave(function() {
		$(this).parent('#moduletable_content').css('width','154px');
		$(this).parent('#moduletable_content').css('height','88px');
		$(this).attr('width','154');
		$(this).attr('height','88');
		$(this).children('embed').attr('width','154');
		$(this).children('embed').attr('height','88');
	});

});

function send(datastr){
	$.ajax({	
		type: "POST",
		url: "/ajax/mail.php",
		data: datastr,
		cache: false,
		success: function(html){
			//_gaq.push(['_trackPageview','/Questions-Quotes']);
			//$("#response").fadeIn("slow");
			//$("#response").html(html);
			//setTimeout('$("#response").fadeOut("slow")',4000);
			alert(html);
		}
	});
}
	
function sendContract(datastr){
	//alert("start!");
	$.ajax({	
		type: "POST",
		url: "/mailContract.php",
		data: datastr,
		cache: false,
		success: function(html){
			//_gaq.push(['_trackPageview','/Questions-Quotes']);

			//$("#response").html(html);
			//setTimeout('$("#response").fadeOut("slow")',4000);
			//alert("success!");
		}
	});
}

	
/*****************************
 *
 * Vendor Admin Functions
 *
 ****************************/
 
 
$.changeVisibleLocType = function(){
	if($(this).val() == "1") {
		var row = $(this).parent('.typeCol').parent('tr');
		$('.bannerZip', row).show(500);
		$('.bannerState', row).hide(500);
		$('.bannerNation', row).hide(500);
	}
	if($(this).val() == "2") {
		var row = $(this).parent('.typeCol').parent('tr');
		$('.bannerZip', row).hide(500);
		$('.bannerState', row).show(500);
		$('.bannerNation', row).hide(500);
	}
	if($(this).val() == "3") {
		var row = $(this).parent('.typeCol').parent('tr');
		$('.bannerZip', row).hide(500);
		$('.bannerState', row).hide(500);
		$('.bannerNation', row).show(500);
	}
}

	
$.deleteLoc = function() {
	var vendorId = $('#newLocBtn').attr('name');
	var locId = $(this).attr('name');

	var deletedRow = $(this).parent('.deleteCol').parent('tr');
	
	
	if(locId < 0){
		$(deletedRow).hide(500);
		$(deletedRow).remove();		
	}
	
	else {
		$.ajax({
		 type: "POST",
		  data: "action=del&vendorId="+vendorId+"&locId="+locId,
		  url: 'Ad_Location.php',
		  success: function(data) {
			$("#row_"+data).hide(500);	
			$("#row_"+data).remove();
		  }
		});
	}

	return false;
} 

