var index = "http://www.rigspy.com/";
var dispatcher = "";
if(location.href.indexOf("rigspy.com")==-1){index = "http://localhost/rigspy/www/"; dispatcher = "index.php/";}
if(location.href.indexOf("demo.rigspy.com")!=-1){index = "http://demo.rigspy.com/"; dispatcher = "";}

$(document).ready(function(){
	$("a[href^='http:']").not("[href*='rigspy']").attr('target','_blank');
	if($("form#home-search").length>0){initHomeSearchForm();}
	if($("body#p-advanced-search").length>0){initAdvancedSearchForm();}
	if( ( $("body#p-join-us").length>0 ) || ( $("body#p-admin-edit").length>0 ) ){initJoinForm();}
	if($("body#p-password").length>0){initPasswordForm();}
	if($("body#p-login-page").length>0){initLoginForm();}
	if($("body#p-browse-contributions").length>0||$("body#p-browse-artists").length>0){initBrowseSelect();}
	if(($("body#p-track-detail").length>0)||($("body#p-item-detail").length>0)){initVotingButtons();}
	if($("body#p-artist-add-item").length>0){initArtistAddEntryForm();}
	initHover('#login button,#search-button,#join-us-button,#contact-button,#submit-button,#log-in-button,#ctas .cta,#search-panel button,button#add-item-button');
	initFocusInOut('#p-home #column2 input,#p-home #column2 select,#search-panel input,#search-panel select,form#standard input,form#standard select,form#standard textarea');	
	initInputDefaultState('#login input#email','email');
	initInputDefaultState('#login input#password','password');

	$('a#info-icon').tooltip({delay: 0, showURL: false, showBody: " - ", extraClass: "tooltip-style", fixPNG: true, opacity: 0.95,positionLeft:true,track:true,left:80,top:300,fade:250});   
	$('a.tooltip').tooltip({delay: 0, showURL: false, showBody: " - ", extraClass: "tooltip-style", fixPNG: true, opacity: 0.95,positionLeft:true,track:true,left:80,top:-130,fade:250});   
	$('a.small-tooltip').tooltip({delay: 0, showURL: false, showBody: " - ", extraClass: "small-tooltip-style", fixPNG: true, opacity: 0.95,positionLeft:true,track:true,left:40,top:-60,fade:250});   
	
	$(".colorbox").colorbox();
	$(".login-colorbox").colorbox({onComplete:function(){initLoginForm();}});
	$(".contact-colorbox").colorbox({onComplete:function(){initContactForm();}});
	$(".add-colorbox").colorbox({onComplete:function(){if($("div#pop-up input#email").length>0){initLoginForm();} else {initAddEntryForm();}}});
	$(".comment-colorbox").colorbox({onComplete:function(){initCommentForm();}});
	$(".tell-a-friend-colorbox").colorbox({onComplete:function(){initTellAFriendForm();}});

	$("#albums-container").accordion({ header: ".album-header", clearStyle: true, autoHeight: false });
	//$(".rig-items").accordion({ header: "h3", clearStyle: true, autoHeight: false  });	
	//$(".instrument").accordion({ header: "h3", clearStyle: true, autoHeight: false  });	
	$('#search-results-secondary ul li:odd,#artist-listing li:odd,#release-listing li:odd,#song-listing li:odd,#personal-details li:odd,#change-password li:odd,#your-contributions li:odd,#comments li:odd,#item-details li:odd').addClass('alt');	
	$(".rig-item").each(function(i){$('#rig-item' + i + ' ul li:odd').addClass('alt');});
	$('#tabs').tabs();	
	$('ul.directory:last').css('background', 'none');
	if($("div.message-box").length>0){$("div.message-box").fadeOut(12000);}
});

function initVotingButtons(){
	initVoteButton('agree');
	initVoteButton('disagree');
}

function initVoteButton(className){
	$("span." + className + " a").click(function(){
		$.get($(this).attr("href"),function(data){
			location.reload();
		});
		return false;
	});
}

function initBrowseSelect(){
	$("select#goto").change(function(){
		document.location = index + dispatcher + $("input#browse-url").val() + "/" + $(this).val();
	});
}

function initPasswordForm(){
	var YourEmail = new LiveValidation('YourEmail', { wait:1000});
	YourEmail.add(Validate.Presence);
	YourEmail.add(Validate.Email);
}

function initLoginForm(){
	$("div#pop-up input#email").focus();
	if( ( location.href.indexOf("/track/") + location.href.indexOf("/entry/") ) > 0){
		$("input#requestedPage").val(location.href.replace(index + dispatcher,"")); //todo - this shouldn't be the absolute path it should be eg. /account/edit
	}
	var email = new LiveValidation('email', {wait:1000});
	email.add(Validate.Presence,{failureMessage: 'Please enter your email'});
	email.add(Validate.Email,{failureMessage: 'Please enter a valid email'});
	var password = new LiveValidation('pass', {wait:1000});
	password.add(Validate.Presence,{failureMessage: 'Please Enter your password'});
}

function initJoinForm(){
	$("input#account_email").focus();
	
	var firstName = new LiveValidation('firstName', { wait:1000});
	firstName.add(Validate.Presence,{failureMessage: 'What is your first name champ?'});

	var lastName = new LiveValidation('lastName', { wait:1000});
	lastName.add(Validate.Presence,{failureMessage: 'What is your last name buddy?'});

	var email = new LiveValidation('account_email', { wait:1000});
	email.add(Validate.Presence,{failureMessage: '...your email?'});
	email.add(Validate.Email,{failureMessage: '...er, a real email please?'});

	var password = new LiveValidation('account_pass', { wait:1000});
	password.add(Validate.Length,{tooShortMessage:'Use at least 6 characters.', minimum:6, tooLongMessage:'Use less than  20 characters.', maximum:20 });

	var reEnterPassword = new LiveValidation('reEnterPassword', { wait:1000});
	reEnterPassword.add(Validate.Confirmation,{match: 'account_pass', failureMessage: 'Try again dickhead.'});

	var birthdayDay = new LiveValidation('birthdayDay', { wait:1000});
	birthdayDay.add(Validate.Format,{pattern:/d/, negate:true});

	var birthdayMonth = new LiveValidation('birthdayMonth', { wait:1000});
	birthdayMonth.add(Validate.Format,{pattern:/m$/, negate:true});

	var birthdayYear = new LiveValidation('birthdayYear', { wait:1000});
	birthdayYear.add(Validate.Format,{pattern:/y/, negate:true});

	var sex = new LiveValidation('sex', { wait:1000});
	sex.add(Validate.Format,{pattern:/Select/, negate:true});

	var category = new LiveValidation('category', { wait:1000});
	category.add(Validate.Format,{pattern:/Select/, negate:true});

	var timezones = new LiveValidation('timezones', { wait:1000});
	timezones.add(Validate.Format,{pattern:/Select/, negate:true});

	if($("body#p-join-us").length>0 ){
		password.add(Validate.Presence,{failureMessage: 'Choose a password.'}); 
		reEnterPassword.add(Validate.Presence,{failureMessage: 'Re-enter your password.'});

		var termsAndConditions = new LiveValidation('termsAndConditions', { wait:1000});
		termsAndConditions.add(Validate.Acceptance,{failureMessage: 'You need to read and agree to the Terms & Conditions to continue.'});
	} else {
		var oldPassword = new LiveValidation('old_pass', { wait:1000});
		oldPassword.add(Validate.Length,{tooShortMessage:'Use at least 6 characters.', minimum:6, tooLongMessage:'Use less than  20 characters.', maximum:20 });
	}
}

function initTellAFriendForm(){
	$("input#friendsName").focus();

	var yourName = new LiveValidation('yourName', {wait:1000});
	yourName.add(Validate.Presence,{failureMessage: 'Please enter your Name'});

	var yourEmail = new LiveValidation('yourEmail', {wait:1000});
	yourEmail.add(Validate.Presence,{failureMessage: 'Please enter your Email'});
	yourEmail.add(Validate.Email,{failureMessage: 'Please enter a valid Email'});

	var friendsName = new LiveValidation('friendsName', {wait:1000});
	friendsName.add(Validate.Presence,{failureMessage: 'Please enter your Name'});

	var friendsEmail = new LiveValidation('friendsEmail', {wait:1000});
	friendsEmail.add(Validate.Presence,{failureMessage: 'Please enter your Email'});
	friendsEmail.add(Validate.Email,{failureMessage: 'Please enter a valid Email'});
}

function initArtistAddEntryForm(){
	$("select#releaseID").change(function(){
		$("span#tracks").load(index + dispatcher + "ajax/get_tracks_for_release/" + $("select#releaseID").val());
	});

	$("select#releaseID").focus();

	var categoryName = new LiveValidation('categoryName', { wait:1000});
	categoryName.add(Validate.Format,{pattern:/Select/, negate:true});

	var modelName = new LiveValidation('modelName', {wait:1000});
	modelName.add(Validate.Presence,{failureMessage: 'Please enter the model name'});

	var brandName = new LiveValidation('brandName', {wait:1000});
	brandName.add(Validate.Presence,{failureMessage: 'Please enter the brand name'});

}

function initAddEntryForm(){
	$("input#usedForAllTracks").click(function(){
		if ($(this).attr("checked")) {
			$("li#track-name").hide(1000)

			return;
		}
		$("li#track-name").show(1000)
	});

	$("input#modelName").focus();

	var categoryName = new LiveValidation('categoryName', { wait:1000});
	categoryName.add(Validate.Format,{pattern:/Select/, negate:true});

	var modelName = new LiveValidation('modelName', {wait:1000});
	modelName.add(Validate.Presence,{failureMessage: 'Please enter the model name'});

	var brandName = new LiveValidation('brandName', {wait:1000});
	brandName.add(Validate.Presence,{failureMessage: 'Please enter the brand name'});

}

function initCommentForm(){
	$("textarea#comment").focus();

	var comment = new LiveValidation('comment', { wait:1000});
	comment.add(Validate.Presence,{failureMessage: 'Please enter your comment'});
}

function initContactForm(){
	if( $("input#yourName").val().length > 0 ){
		$("textarea#comments").focus();
	}else{
		$("input#yourName").focus();
	}
	var yourName = new LiveValidation('yourName', {wait:1000});
	yourName.add(Validate.Presence,{failureMessage: 'Please enter your Name'});

	var yourEmail = new LiveValidation('yourEmail', {wait:1000});
	yourEmail.add(Validate.Presence,{failureMessage: 'Please enter your Email'});
	yourEmail.add(Validate.Email,{failureMessage: 'Please enter a valid Email'});

	var comments = new LiveValidation('comments', {wait:1000});
	comments.add(Validate.Presence,{failureMessage: 'Please Enter your Comments'});

	$("form.contact").submit(function(){
		$.post(index + dispatcher + "contact_process",
		{yourName: $("input#yourName").val(), yourEmail: $("input#yourEmail").val(), comments: $("textarea#comments").val()},
		function(){
			$("div#pop-up").load(index + dispatcher + "contact-thankyou");
		});
		return false;
	});
}

function initHomeSearchForm(){
	$("input#searchArtistName").focus();
}

function initAdvancedSearchForm(){
	$("input#searchArtistName").focus();
}

function initInputDefaultState(selector,defaultText){
	$(selector).focus(function(){ 
        var defaultText = $(this).val(); 
        if (defaultText==defaultText){$(this).val('');}
    	$(selector).blur(function(){var userInput = $(this).val();if(userInput == ''){$(this).val(defaultText);}});
    });
}

function initHover(selector){$(selector).hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');});}
function initFocusInOut(selector){$(selector).focusin(function(){$(this).addClass('focus');}).focusout(function(){$(this).removeClass('focus');});}
