var actualStep = 1;
var selectedhours = 0;
var steps = 3;
var conn ="";
NiftyLoad=function(){
	Nifty("div#profileOthers");
	Nifty("div#profileError");
	Nifty("div#profileUpdates");
	Nifty("div#profilePicture");
	Nifty("div#profileThumbs");
	Nifty("div#profileMessage");
	Nifty("div#profileBooking");
	Nifty("div#profileDetails");
	Nifty("div#profileAskpicture");
	Nifty("div#profileAskvideo");
	Nifty("div#profileFunctions");
	$('#profileMessage').css('display', 'none');
	$('#profileAskpicture').css('display', 'none');
	$('#profileAskvideo').css('display', 'none');
}
function CheckUseremail(){ // This function we will use to check to see if a username is taken or not.
	if(verifyEmpty('#user_name_ss')){
		$('#user_name_ss').css('background-color','#dddddd');
		if(conn != ""){
			conn.abort();
			conn="";
		}
		var dataString = "app=min&username="+$('#user_name_ss').val();
		conn = $.ajax({  
		   type: "GET",
		   url: "check.php",
		   data: dataString,
		   dataType: "html",
		   success: function(data){
			if(data == 'free'){
				$('#user_name_ss').css('background-color','#eaffe3');
			}else{
				$('#user_name_ss').css('background-color','#FFE3E3');
			}
			conn="";
		   }
		});
		}else{
		$('#user_name_ss').css('background-color','');
		}
	} // Close Function.
function clearText(thefield,type){
	if (thefield.defaultValue==thefield.value){
		thefield.value = "";
		if (type=='pwd'){
			thefield.type = 'password';
		}
	}
}

function writeText(thefield,type){
	if (thefield.value==""){
		thefield.value = thefield.defaultValue;
		if (type=='pwd'){
			thefield.type = 'text';
		}
	}
}

function showStep(id){
	if(id != actualStep){
	var ok = true;
	for(var i = 0; i<id; i++){
		if(!verifyStep(i)){
			if(actualStep != i){
				$('#step'+actualStep).hide('fast');
				actualStep = i;
				$('#step'+i).show('slow');
			}
			ok = false;
			break;
		}
	}
	if(ok){
		if(id == 4){
			var writehours = new Array();
			$('#dispList input:checkbox').each(function(){
				if(this.checked){
					writehours.push($(this).val());
				}
			});
			var hourstring= writehours[0]-1+'h - '+writehours[writehours.length-1]+'h';
			$('#sumdate').text($("input[name='go_search_date2']").val()+', '+hourstring+', '+$('#country :selected').attr('label'));
			var writeservices = new Array();
			$('#sumservices').text('');
			$('#step2 input:checkbox').each(function(){
				if(this.checked){
					$('#sumservices').append($(this).parent().find('#service').html()+'<br/>');
				}
			});
			$('#sumothers').text($('#street').val()+', '+$('#city_text').val()+', '+$('#postcode').val());
			
		}
		$('#steps'+actualStep).removeClass('stepSelected');
		$('#step'+actualStep).hide('fast');
		actualStep = id;
		$('#steps'+id).addClass('stepSelected');
		$('#step'+id).show('slow');
	}
	}
}

function verifyEmpty(field){
	if($(field)[0].defaultValue == $(field).val() || $(field).val() == ''){
		return false;
	}else{
		return true;
	}
}

function showAuth(what){
	$('#'+what).show('slow');
	$('#authfunctions').hide('fast');
}

function booklogin(act){
		$('#bookloginerror').html('<img src="images/justjoined/loader.gif" />');
		if(act == 'register'){
			$('#bookloginconfirmerror').html('');
			$('#confirm').val('Veryfying...'); 
			var dataString='act=login&act2=register&user='+$('#user_name_ss').val()+'&password='+$('#pass_word_ss').val();
		}else{
			var dataString='act=login&user='+$('#login_user_name').val()+'&password='+$('#login_pass_word').val();
		}
		$.ajax({  
		   type: "POST",
		   url: "ajaxbookauth.php",
		   data: dataString,
		   dataType: "html",
		   success: function(data){
			if(data=='success'){
				$('#bookbtn').show('slow');
				$('#autharea').hide('fast');
				$('#bookbtn').attr('type', 'submit');  $('#signupl').attr('type', 'button');
			}else{
				if(act == 'register'){
					$('#confirm').attr('type', 'submit');  $('#signupr').attr('type', 'button');
					$('#confirm').val('OK'); 
					$('#bookloginconfirmerror').html(data);
				}else{
					$('#bookloginerror').html(data);
				}
			}
		   }
		});
	return false;
}

function bookregister(){
		$('#bookregistererror').html('<img src="images/justjoined/loader.gif" alt="loading..." />');
		var dataString='act=register&user='+$('#user_name_ss').val()+'&password='+$('#pass_word_ss').val()+'&email='+$('#user_email').val()+'&country='+$('#country').val()+'&gender='+$('#gender').val()+'&birthdate='+$('#birthdate').val();
		$.ajax({  
		   type: "POST",
		   url: "ajaxbookauth.php",
		   data: dataString,
		   dataType: "html",
		   success: function(data){
			if(data=='success'){
				$('#confirmregister').show('slow');
				$('#register').hide('fast');
				$('#bookbtn').attr('type', 'submit');  $('#signupr').attr('type', 'button');
			}else{
				$('#bookregistererror').html(data);
			}
		   }
		});
	return false;
}
function delUpdate(id){
		$('#profileUpdatesContent').html('<img src="images/loading.gif" style="display:block; margin:0 auto;" />');
		var dataString='action=delupdate&updateid='+id;
		$.ajax({  
		   type: "POST",
		   url: "ajaxupdate.php",
		   data: dataString,
		   dataType: "html",
		   success: function(data){
				$('#profileUpdatesContent').html(data);
		   }
		});
	return false;
}

	function delCommentUpdate(id, commentid){
		var dataString='action=delcommentupdate&commentid='+commentid+'&updateid='+id;
		$('#commentbtn'+id).hide('slow');
		$('#commentloading'+id).show('slow');
		$.ajax({  
		   type: "POST",
		   url: "ajaxupdate.php",
		   data: dataString,
		   dataType: "html",
		   success: function(data){
				$('#txt'+id).html(data);
				$('#commentbtn'+id).show('slow');
				$('#commentloading'+id).hide('slow');
		   }
		});
		return false;
	}

var oldcomments = "";
function showCommentsUpdates(name, id){
	if(oldcomments != ""){
		$(oldcomments).hide('slow');
	}
	if(oldcomments == name){
		oldcomments = "";
	}else{
		oldcomments = name;
		$(name).show('slow');
		var dataString='action=showcomments&updateid='+id;
		$('#commentloading'+id).show('slow');
		$.ajax({  
		   type: "POST",
		   url: "ajaxupdate.php",
		   data: dataString,
		   dataType: "html",
		   success: function(data){
				$('#txt'+id).html(data);
				$('#commentloading'+id).hide('slow');
		   }
		});
		$(name).bind("mouseleave",function(){
			$(this).hide('slow');
		});
	}
}

	function updateComment(id){
		var dataString='action=commentupdate&comment='+$('#commentarea'+id).val()+'&updateid='+id;
		$('#commentbtn'+id).hide('slow');
		$('#commentloading'+id).show('slow');
		$.ajax({  
		   type: "POST",
		   url: "ajaxupdate.php",
		   data: dataString,
		   dataType: "html",
		   success: function(data){
				$('#txt'+id).html(data);
				$('#commentbtn'+id).show('slow');
				$('#commentloading'+id).hide('slow');
		   }
		});
		return false;
	}

function hoverImg(thumb, img){
	$("#proimg").attr("src", thumb);
	$("#mmm").attr("href", img);
}

function changeProfileColor(color){
	$(".profileBox").css('backgroundColor', color);
	$(".r1").css('backgroundColor', color);
	$(".r2").css('backgroundColor', color);
	$(".r3").css('backgroundColor', color);
	$(".r4").css('backgroundColor', color);
	
	$(".r1").css('border-color', color);
	$(".r2").css('border-color', color);
	$(".r3").css('border-color', color);
	$(".r4").css('border-color', color);

}

function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
var open="";
function showFunction(id){
	if($('#'+id).css('display') == 'none'){
		if(open != ""){
			$('#'+open).hide('fast');
		}
		$('#'+id).show('slow');
		open = id;
	}else{
		$('#'+id).hide('slow');
	}
}

function requestvideo(providersid,seekersid,seekeremail,morethanone){
	var SID= seekersid;
	var ok = true;
	$('#requestvideobutton').attr("disabled", true);
	$('#requestvideobutton').attr("value", "Requesting video...");
	$('#errorvreq').html('');
	if (  (document.getElementById(seekeremail).value==null || document.getElementById(seekeremail).value=="")  && (seekersid==null || seekersid=="0")  )	{
		ok = false;
		$('#errorvreq').html('Please enter an email');
		$('#requestvideobutton').attr("disabled", false);
		$('#requestvideobutton').attr("value", "Request a video");
	}
	else if (emailvalidation (document.getElementById(seekeremail).value)==false && (seekersid=="0" || seekersid==null) )
	{
	ok=false;
	$('#errorvreq').html('Enter a valid  email');
	$('#requestvideobutton').attr("disabled", false);
	$('#requestvideobutton').attr("value", "Request a video");
	}
	if(ok){
		var link="seekermessagetoprovider.php?action=video&PID="+providersid+"&SID="+SID+"&SEMAIL="+document.getElementById(seekeremail).value+"&NOPICS="+morethanone;
		$.ajax({
		  url: link,
		  success: function(data) {
			$('#requestvideobutton').attr("disabled", false);
			$('#requestvideobutton').attr("value", "Request a video");
			$('#errorvreq').html(data);
		  }
		});
	}
}

function readMore(){
	if($('#descmore').css('display') == 'none'){
		$('#readmore').html('read less');
		$('#desctrunc').hide('fast');
		$('#descmore').show('slow');
	}else{
		$('#readmore').html('read more');
		$('#descmore').hide('fast');
		$('#desctrunc').show('slow');
	}
}

function bookHour(object){
	if(!$('#'+object+' :checkbox').attr('checked')){
		$('#'+object).addClass('hourSelected');
	}else{
		$('#'+object).removeClass('hourSelected');
	}
	$('#'+object+' :checkbox').attr('checked', !$('#'+object+' :checkbox').attr('checked'));
	if(!get_Time($('#'+object+' :checkbox').val())){
		$('#'+object).removeClass('hourSelected');
	}
}

$(document).ready(function() {
		$('#colorSelector').ColorPicker({
			color: '#0000ff',
			onShow: function (colpkr) {
				$(colpkr).fadeIn(500);
				return false;
			},
			onHide: function (colpkr) {
				$(colpkr).fadeOut(500);
				return false;
			},
			onChange: function (hsb, hex, rgb) {
				setCookie("profilecolor","#"+hex, 30);
				$('#colorSelector div').css('backgroundColor', '#' + hex);
				changeProfileColor('#' + hex);
			},
			onSubmit: function (hsb, hex, rgb, el) {
				setCookie("profilecolor","#"+hex, 30);
			}
		});
	$('#birthdate').datepicker({
		changeMonth: true,
		changeYear: true,
		dateFormat: 'yy-mm-dd',
		yearRange: '-100:+1'
	});
	 $("a[rel=gallery]").fancybox({
		'overlayOpacity'	:	0.7,
		'overlayColor'		:	'#000',
		'zoomSpeedIn'		:	500,
		'zoomSpeedOut'		:	500
	}); 
	  //Get our elements for faster access and set overlay width
  var div = $('div.sc_menu'),
               ul = $('ul.sc_menu'),
               // unordered list's left margin
               ulPadding = 15;

  //Get menu width
  var divWidth = div.width();

  //Remove scrollbars
  div.css({overflow: 'hidden'});

  //Find last image container
  var lastLi = ul.find('li:last-child');

  //When user move mouse over menu
  div.mousemove(function(e){

    //As images are loaded ul width increases,
    //so we recalculate it each time
    var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

    var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
    div.scrollLeft(left);
  });
});
