$(document).ready( function() {

    $.post("/ajax/winners.php", {'id': 0}, function(data) {
		$('#previousWinnersSub').html(data);
	});
	
	$('.answerIcon').live('click', function(event) {
		$(".answerIcon").colorbox({width:"640", height:"480", iframe:true });
		return false;
	});


	$('.winnerOffset').live('click', function() {
		var id = $(this).attr('alt');
		$.post("/ajax/winners.php", {'id': id}, function(data) {
			$('#previousWinnersSub').html(data);
		});
		return false;
	});

	$('.twentyQuestions').click( function(event) {
		event.stopPropagation();
		var link = $(this).attr('href');
		$.fn.colorbox({width:"640", height:"480", iframe:true, href:link });
		return false;
	});

	$('.twentyQuestions').live('click', function(event) {
		event.stopPropagation();
		var link = $(this).attr('href');
		$.fn.colorbox({width:"640", height:"480", iframe:true, href:link });
		return false;
	});

	$(".colorbox").colorbox({width:"640", height:"480", iframe:true});
	$(".colorboxFull").colorbox({
		width:"1020", 
		height:"580", 
		iframe:true,
		onClosed:function(){ 
			window.location.href="/";
		}
	});

	$(".colorboxFullNoClose").colorbox({
                width:"1020",
                height:"580",
                iframe:true
        });


	
        function cbStatus(){
		return colorboxOpen;
	}	

	$(".colorboxPhoto").colorbox({width:"500", height:"400", iframe:true });

	$("div.question > p.answer").hide();
	$("div.question > h2").click( function() {
		if($(this).next().is(':hidden')) {
			$(this).next().show();
			$(this).children('img').attr('src', '/images/minus.png');
		} else {
			$(this).next().hide();
			$(this).children('img').attr('src', '/images/plus.png');
		}
	});

	$('#coda-slider').codaSlider({
		dynamicArrows: false,
		dynamicTabs: false
	});

        $(".slider").slider({
                min: 48,
                max: 90,
                slide: function(event, ui) {
                        var id = ($(this).attr('id'));
                        var foot        = Math.floor(ui.value/12);
                        var inches      = ui.value - (foot * 12);
			if(inches > 0)  $("input#"+id).val(foot + "ft " + inches + "in");
                        else            $("input#"+id).val(foot + "ft");
			if(foot==7 && inches == 6){
				$("input#"+id).val("Giant");
			}
                }
        });

	$("#contestantForm").bind("keypress", function(e) {
	  if (e.keyCode == 9) return false;
	});

	$("div.contestantMale").mouseover(function(){
		$(this).addClass("contestantMaleOver");
	}).mouseout(function(){
		$(this).removeClass("contestantMaleOver");		
	});

	$("div.contestantFemale").mouseover(function(){
		$(this).addClass("contestantFemaleOver");
	}).mouseout(function(){
		$(this).removeClass("contestantFemaleOver");		
	});

	$("div.contestantMale").click(function(){
		if(!$(this).hasClass("contestantSelected")) {
			$("div.contestantMale").removeClass("contestantSelected");
			$(this).addClass("contestantSelected");
		}
	});

	$("div.contestantFemale").click(function(){
		if(!$(this).hasClass("contestantSelected")) {
			$("div.contestantFemale").removeClass("contestantSelected");
			$(this).addClass("contestantSelected");
		}
	});

	$("#previousWinners").click(function() {
		$("#previousWinnersSub").slideToggle("slow");
	});

	$(".autoUpdateDate").bind('keyup', function() {
		var id = $(this).attr('id');
		var pos = id.indexOf('-');
		id = id.substring(0,pos);
		$('#'+id).val($('#'+id+'-3').val() + '-' + $('#'+id+'-1').val() + '-' + $('#'+id+'-2').val());
	});
	$(".autoUpdatePhone").bind('keyup', function() {
		var id = $(this).attr('id');
		var pos = id.indexOf('-');
		id = id.substring(0,pos);
		$('#'+id).val('(' + $('#'+id+'-1').val() + ') ' + $('#'+id+'-2').val() + ' - ' + $('#'+id+'-3').val());
	});
	$(".autoUpdate").bind('focus', function() {
		if ($(this).val() == this.defaultValue)	$(this).val("");
	});
	$(".autoUpdate").bind('blur', function() {
		if ($(this).val() == "")	$(this).val(this.defaultValue);
	});

	$(".voteNow").click( function() {
		var cid1 = $('.contestantId', $('div#columnLeft > div.contestantSelected')).html();
		var cid2 = $('.contestantId', $('div#columnRight > div.contestantSelected')).html();
		if(cid1 && cid2) {
			$.post('/ajax/vote.php', { 'cid1': cid1, 'cid2': cid2 });
			$(this).hide();
			$('h4', $(this).parent()).hide();
			// colorbox confirmation
			$.fn.colorbox({href:'/ajax/successfulvote.php', width: 540});
		} else {
			// colorbox failure notification
			$.fn.colorbox({href:'/ajax/unsuccessfulvote.php', width: 540});
		}
		return false;
	});

	

	$('#contestantSubmit').click( function() {
		$('#statusMessage').empty();
		$('#statusMessage').remove();
		$.ajax({
                	type: "POST",
                        url: "/forms/validate.php",
                        data: $('#contestantForm').serialize(),	
				success: function(data) {
					$('#validation').append('<div id="statusMessage">' + data + '</div>');
					$('#statusMessage').addClass('statusMessageStyle');
					$('#statusMessage').show('fast');
					if($('#statusMessage').is(":empty")){
                                	$.ajax({
                                            type: "POST",
                                            url: "/forms/upgrade.php",
                                            data: $('#contestantForm').serialize(),
                                            success: function() {
                                                $('#contestantForm').hide('fast', function() {
                                                        $('#success').show('fast');
                                                });
                                            }
                                       });
                        	       }
                	}					
                });					
		return false;
	});
	
	$.extend($.fn.disableTextSelect = function() {
		return this.each(function() {
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			} else if($.browser.msie) {//IE
				$(this).bind('selectstart',function(){return false;});
			} else {//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});
	$('.noSelect').disableTextSelect();

	if($('form#participateForm').length) {
		$('#selectAll').click( function() {
			if($(this).is(':checked'))	$("form#participateForm input[type='checkbox']").attr('checked', true);
			else				$("form#participateForm input[type='checkbox']").attr('checked', false);
		});
	}

	if($('#profilePhoto').length) {
//		$('#profilePhoto').colorbox({width:"694", height:"640", iframe:true, href:link });
		$('#profilePhoto').click( function() {
			var link = '/profile/photo.php';
			window.location.href=link;
		});
	}

	if($('#contacterror').length) {
		$('form#contact').submit( function() {
			$.post("/ajax/contactformcheck.php", $('form#contact').serialize(), function(data) {
				if(data) {
					$('#contacterror').html(data).show();
					return false;
				} else {
					$('form#contact').unbind('submit');
					$('#submit').click();
				}
			});
			return false;
		});
	}

});
	var colorboxOpen = false;
	$(document).bind('cbox_open', function(){
  	  colorboxOpen = true;
	});
	$(document).bind('cbox_cleanup', function(){
	  colorboxOpen = false;
	});

$('#squestion_9 a').attr('tabindex','-1');

