var clicked = 0;
var ajaxpending = 0;
var source = '';
var header = '';

$(document).ready(function() {       
   
    $('#subfooter .ajaxmenu a, #hometop .ajaxmenu a, #slider a, .ajaxlink').click( function(e) {     
        
        e.preventDefault();
        
        clicked = 1;
            
        load_page($(this).attr('href'));
        
    }); 
    
    // Bind an event to window.onhashchange that, when the hash changes, gets the
    // hash and adds the class "selected" to any matching nav link.
    $(window).hashchange( function(){
        
        if (clicked != 1) {
            
            var hash = location.hash;
            var hashparts = hash.split('/');   

            // Set the page title based on the hash.
            //document.title = 'The hash is ' + ( hash.replace( /^#/, '' ) || 'blank' ) + '.';



            if(hashparts[1] && hashparts[2]) {
                load_page(hashparts[1] + '/' + hashparts[2]);
            }
        }
        
        clicked = 0;
    })

    // Since the event is only triggered when the hash changes, we need to trigger
    // the event now, to handle the hash the page may have loaded with.
    
    $(window).hashchange();


    $('#home #mainnav>ul>li').hover( function () {
        
        $(this).find('ul').stop(true,true).slideDown(100);
        $(this).find('a:first').css({
            "background-image": 'url(/assets/images/nav_bg2.png)',
            "color": '#fff',
            "text-shadow" : '1px 1px 1px #000'
        });

    }, function () {
        $(this).find('ul').stop(true,true).slideUp(100);
        $(this).find('a:first').css({
            "background-image": 'none',
            "color": '#630872',
            "text-shadow" : 'none'
        });
    });


    $('#music').toggle( function() { 
            pause();
            $(this).removeClass('on').addClass('off');
        }, function() {
            play();
            $(this).removeClass('off').addClass('on');
        }
    );

    $('#quicksignup input.labeled').each( function() {
        setInputToLabel(this);
    });

    $('#quicksignup input.labeled').focus( function() {
        $(this).val('');
    });

    $('#quicksignup input.labeled').blur( function() {
        if ( $(this).val() == '') {

            setInputToLabel(this);
        }

    })
	
    $('.validateform').submit( function(e) {

        if(!validate_form(this)) {

            return false;
            
        } else {
			
			
			
			if( $(this).hasClass('ajax') ) {
			
				e.preventDefault();	
				ajaxSubmit(this);
				
			}
			
            return true;

        }
     
    });	

});


function ajaxSubmit (passed) {
	
	var data = $(passed).serialize();	
	
	action = $(passed).attr('action'); 
	
	$(passed).find('.loading').show();
	
	$.ajax({
		type: "POST",
		url: action,
		data: data,
		error: function(jqXHR, textStatus, errorThrown) {
			alert('Something broke! Please reload the page and try again.');

		},
		success: function (data, textStatus, jqXHR) {
			$('.loading').hide();
			$(passed).append('<div class="formsuccess">'+data+'</div>');
			
		}
	});
		
	

}

function setInputToLabel(passed) {
    var label = $(passed).parent().find('label').text();
    $(passed).val(label);
}


function validate_form(passed) {

    var valid = 1;

    $(passed).find('.formerror').remove();

    $(passed).find("input.required, textarea.required").each(function() {

        

        if($(this).val() == '' || $(this).val() == $(this).parent().find('label').text()) {
            
            valid = 0;
            add_error(this,'This field may not be left blank');

        } 

    });

    $(passed).find("input.required.emailcheck").each(function() {

        if($(this).val() != $(this).parent().find('label').text()) {
  
            if(!validate_email($(this).val())) {

                valid = 0;
                add_error(this,'Please Enter A Valid Email');

            }

        } 

    });

    $(passed).find("input.required.robot").each(function() {

        if($(this).val() != $(this).parent().find('label').text()) {

            if($(this).val() != 'robot') {

                valid = 0;
                add_error(this,'Please type "robot" in the box below to prove you are a real person.');

            }

        } 

    });

    return valid;
}

function validate_email(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return false;
   } else { return true; }
}

function add_error(passed,error) {

    var invalid_id = $(passed).attr('id');
    //$('#' + invalid_id).css({color:'red'});
    $(passed).parent().append('<div class="formerror">' + error + '</div>');

}
            
function play() {    
    getFlashObject().SetVariable("method:play", "");
    getFlashObject().SetVariable("enabled", "true");
}
function pause() {
    getFlashObject().SetVariable("method:pause", "");
}
function stop() {
    getFlashObject().SetVariable("method:stop", "");
}

function getFlashObject() {
    
    return document.getElementById("flashmp3player");
    
}      

var myListener = new Object();
myListener.onInit = function(){};
myListener.onUpdate = function(){};
