$('html').addClass('js');

var next_image = 0;

$(document).ready(function() {

	var h = $('#visible').is(':hidden');
	var running = false;
	//console.log(running);

	$('html').removeClass('js');
	
	
	if($("#prev").attr("href") === "#"){
		$("#prev").addClass("disabled");	
	}
	
	if($("#next").attr("href") === "#"){
		$("#next").addClass("disabled");	
	}
	
	if($("#prevteam").attr("href") === "#"){
		$("#prevteam").addClass("disabled");	
	}
	
	if($("#nextteam").attr("href") === "#"){
		$("#nextteam").addClass("disabled");	
	}
	
	$("#prevpost").click(function(){
		var postId = $(this).attr("data-id");
		$.ajax({
		   type: "POST",
		   url: "postfeed/get_me_the_prev_post",
		   async: false,
		   data: {currentid:postId},
		   success: function(reply){	
				var replyObject = $.parseJSON(reply);
				
				if(!$.isEmptyObject(replyObject))
				{
					$("#nextpost, #prevpost").removeClass("disabled");
					$(".content-text").fadeOut("slow", function()
					{
						$("#prevpost").attr("data-id",replyObject.id);
						$("#nextpost").attr("data-id",replyObject.id);
						$(".content-text h2").html(replyObject.title);
						$(".content-text p").remove();
						$(".content-text").append(replyObject.news);
						$(".content-text").fadeIn("slow");	
						if(replyObject.isfirst){
							$("#prevpost").addClass("disabled");
						}
					});
				}
				else
				{
					$("#prevpost").addClass("disabled");
				}					
			}
		});
		return false;
	});
	
	$("#nextpost").click(function(){
		var postId = $("#prevpost").attr("data-id");
		$.ajax({
		   type: "POST",
		   url: "postfeed/get_me_the_next_post",
		   async: false,
		   data: {currentid:postId},
		   success: function(reply){
				var replyObject = $.parseJSON(reply);
				if(!$.isEmptyObject(replyObject))
				{
					$("#nextpost, #prevpost").removeClass("disabled");						
					$(".content-text").fadeOut("slow", function(){
						$("#prevpost").attr("data-id",replyObject.id);
						$("#nextpost").attr("data-id",replyObject.id);
						$(".content-text h2").html(replyObject.title);
						$(".content-text p").remove();
						$(".content-text").append(replyObject.news);				
						$(".content-text").fadeIn("slow");
						if(replyObject.islast){
							$("#nextpost").addClass("disabled");
						}
					});
				}
				else
				{
					$("#nextpost").addClass("disabled");
				}	
		   }
		});
		return false;
	});
	
	$("#prevpromopost").click(function(){
		var postId = $(this).attr("data-id");
		$.ajax({
				   type: "POST",
				   url: "postfeed/get_me_the_prev_promo_post",
				   async: false,
				   data: {currentid:postId},
				   success: function(reply){
						var replyObject = $.parseJSON(reply);
						if(!$.isEmptyObject(replyObject)){
							$("#nextpromopost, #prevpromopost").removeClass("disabled");
							$(".content-text").fadeOut("slow", function(){
								$("#prevpromopost").attr("data-id",replyObject.id);
								$("#nextpromopost").attr("data-id",replyObject.id);
								$(".content-text h2").html(replyObject.title);
								$(".content-text p").remove();
								$(".content-text").append(replyObject.promotion);
								$(".content-text").fadeIn("slow");
								
								if(replyObject.isfirst){
									$("#prevpromopost").addClass("disabled");
								}
							});
						}
						else
						{
							$("#prevpromopost").addClass("disabled");
						}					
				   }
			});
		return false;
	});
	
	$("#nextpromopost").click(function(){
		$("#next, #prev").removeClass("disabled");
		var postId = $("#prevpromopost").attr("data-id");
		$.ajax({
		   type: "POST",
		   url: "postfeed/get_me_the_next_promo_post",
		   async: false,
		   data: {currentid:postId},
		   success: function(reply){
				var replyObject = $.parseJSON(reply);
				if(!$.isEmptyObject(replyObject)){
					$("#nextpromopost, #prevpromopost").removeClass("disabled");
					$(".content-text").fadeOut("slow", function(){
						$("#prevpromopost").attr("data-id",replyObject.id);
						$("#nextpromopost").attr("data-id",replyObject.id);
						$(".content-text h2").html(replyObject.title);
						$(".content-text p").remove();
						$(".content-text").append(replyObject.promotion);
						$(".content-text").fadeIn("slow");
						if(replyObject.islast){
							$("#nextpromopost").addClass("disabled");
						}
					});
				}
				else
				{
					$("#nextpromopost").addClass("disabled");
				}
		   }
		});
		return false;
	});
	
	$("#nextteam").click(function(ev){
		ev.preventDefault();
		if($(this).attr("href") != "#"){
			$.ajax({
			   url: $(this).attr("href"),
			   async: false,
			   type: "POST",
			   data: {ajax:true},
			   success: function(reply){
					var replyObject = $.parseJSON(reply);

					if(!$.isEmptyObject(replyObject)){
						
						$("#nextteam, #prevteam").removeClass("disabled");
						$(".content-text").fadeOut("slow", function(){
							$(".content-text .content-header h2").html(replyObject.role);
							$(".content-text .team-name h3").html(replyObject.name);
							$(".content-text .team-text").html(replyObject.content);
							if(replyObject.next_page_id != undefined && replyObject.next_page_id != "")
							{
								$("#nextteam").attr("href", replyObject.next_page_id);
							}
							else
							{
								$("#nextteam").attr("href", "#");
								$("#nextteam").addClass("disabled");
							}
							if(replyObject.previous_page_id != undefined && replyObject.previous_page_id != "")
							{
								$("#prevteam").attr("href", replyObject.previous_page_id);
							}
							else
							{
								$("#prevteam").attr("href", "#");
								$("#prevteam").addClass("disabled");
							}
							$(".team-img").html("<img src='"+uri+""+replyObject.photo[0].folder+"/"+replyObject.photo[0].id+"."+replyObject.photo[0].type+"'>");
							$(".content-text").fadeIn("slow");
						});
					}	
				}
			});
			}else{
				$(this).addClass("disabled");
			};
			return false;
	});
	
	$("#prevteam").click(function(ev){
		ev.preventDefault();
		if($(this).attr("href") != "#"){
			$.ajax({
			   url: $(this).attr("href"),
			   async: false,
			   type: "POST",
			   data: {ajax:true},
			   success: function(reply){
					var replyObject = $.parseJSON(reply);
					
					if(!$.isEmptyObject(replyObject)){
						$("#nextteam, #prevteam").removeClass("disabled");
						$(".content-text").fadeOut("slow", function(){
						
							$(".content-text .content-header h2").html(replyObject.role);
							$(".content-text .team-name h3").html(replyObject.name);
							$(".content-text .team-text").html(replyObject.content);
							$(".team-img").html("<img src='"+uri+""+replyObject.photo[0].folder+"/"+replyObject.photo[0].id+"."+replyObject.photo[0].type+"'>");
							
							if(replyObject.next_page_id != undefined && replyObject.next_page_id != "")
							{
								$("#nextteam").attr("href", replyObject.next_page_id);
							}
							else
							{
								$("#nextteam").attr("href", "#");
								$("#nextteam").addClass("disabled");
							}
							if(replyObject.previous_page_id != undefined && replyObject.previous_page_id != "")
							{
								$("#prevteam").attr("href", replyObject.previous_page_id);
							}
							else
							{
								$("#prevteam").attr("href", "#");
								$("#prevteam").addClass("disabled");
							}
							$(".content-text").fadeIn("slow");
						
						});
					}
				}
			});
		}
		else
		{
			$(this).addClass("disabled");
		}
		return false;
	});
	
	
	/********* Bathrooms and Kitchens pages use these ***********/
	
	$("#prev").click(function(ev){
	
		if (running == false)
		{
			$("#bg").show();
			ev.preventDefault();	
			if($(this).attr("href") != "#"){
				running = true;
				
				$("#next, #prev").removeClass("disabled");
				
					$.ajax({
					   url: $(this).attr("href"),
					   async: false,
					   type: "POST",
					   data: {ajax:true},
					   success: function(reply){
							var replyObject = $.parseJSON(reply);
							if(!$.isEmptyObject(replyObject)){

											$(".content-text").fadeOut("slow", function(){
											
												$(".content-text").html(replyObject.content);
												
												if(replyObject.next_page_id != undefined && replyObject.next_page_id != "")
												{
													$("#next").attr("href", replyObject.next_page_id);
												}
												else
												{
													$("#next").attr("href", "#");
													$("#next").addClass("disabled");
												}
												if(replyObject.previous_page_id != undefined && replyObject.previous_page_id != "")
												{
													$("#prev").attr("href", replyObject.previous_page_id);
												}
												else
												{
													$("#prev").attr("href", "#");
													$("#prev").addClass("disabled");
												}
												$(".content-text").fadeIn("slow");								
											});
											
									if( $.browser.opera )
									{
										$(".operabg img").attr("src", page+replyObject.background_img[0].folder+"/"+replyObject.background_img[0].id+"."+replyObject.background_img[0].type);
									}
								
									else
									{
										$("#bg_next img").attr("src", page+replyObject.background_img[0].folder+"/"+replyObject.background_img[0].id+"."+replyObject.background_img[0].type);
									}
									//set preloader attr to next image
										if( $.browser.opera )
										{
											$(".operabg_next").fadeOut("slow", function(){

											next_image = $(".operabg img").attr('src');
											
											$(".operabg_next img").attr("src", next_image);
											$(".operabg_next").show();
											});
										}
										else
										{	
											$("#bg").fadeOut("slow", function(){
											//fadeout background image
											
												next_image = $("#bg_next img").attr('src');
												//get the src attribute of the next image
												
												$("#bg img").attr("src", next_image);
												
												
												//set loaded image (.bg_next) to be the main image (.bg)
											});
										};
									};
								}
							});
					
					running = false;
					//console.log(running);
			}
		};
		return false;
	});
	
	$("#next").click(function(ev){
		if (running == false)
		{
			running = true;
			$("#bg").show();
			ev.preventDefault();
			if($(this).attr("href") != "#"){
				$("#next, #prev").removeClass("disabled");
				$.ajax({
				   url: $(this).attr("href"),
				   async: false,
				   type: "POST",
				   data: {ajax:true},
				   success: function(reply){
						var replyObject = $.parseJSON(reply);

						if(!$.isEmptyObject(replyObject)){
								
								
								if( $.browser.opera )
								{	
									$(".operabg img").attr("src", page+replyObject.background_img[0].folder+"/"+replyObject.background_img[0].id+"."+replyObject.background_img[0].type);
								}
								//do replacement for opera
								else
								{
									$("#bg_next img").attr("src", page+replyObject.background_img[0].folder+"/"+replyObject.background_img[0].id+"."+replyObject.background_img[0].type);
								}
								
								
								if($(".imgstopreload").length<1){
									$("body").append('<div class="imgstopreload"></div>');
									$(".imgstopreload").hide();
								}
								
								$(".imgstopreload").empty();
								
								if(replyObject.nextimagepreload != null){
									var nextImg = new Image();
									nextImg.src = replyObject.nextimagepreload;
									$(".imgstopreload").append(nextImg);
								}
								
								if(replyObject.previmagepreload != null){
									var prevImg = new Image();
									prevImg.src = replyObject.previmagepreload;
									$(".imgstopreload").append(prevImg);
								}
								
								$(".content-text").fadeOut("slow", function(){
										
									$(".content-text").html(replyObject.content);
									
									if(replyObject.next_page_id != undefined && replyObject.next_page_id != "")
									{
										$("#next").attr("href", replyObject.next_page_id);
									}
									else
									{
										$("#next").attr("href", "#");
										$("#next").addClass("disabled");
									}
									if(replyObject.previous_page_id != undefined && replyObject.previous_page_id != "")
									{
										$("#prev").attr("href", replyObject.previous_page_id);
									}
									else
									{
										$("#prev").attr("href", "#");
										$("#prev").addClass("disabled");
									}
									$(".content-text").fadeIn("slow");								
								});
								
								if($.browser.opera )
								{
									$(".operabg_next").fadeOut("slow",function(){
											next_image = $(".operabg img").attr('src');
											$(".operabg_next img").attr("src", next_image);
											
											$(".operabg_next").show();								
									});
								}
								else
								{	
									$("#bg").fadeOut("slow", function(){
									//fadeout background image
									
										next_image = $("#bg_next img").attr('src');
										//get the src attribute of the next image
										
										$("#bg img").attr("src", next_image);
										
										//set loaded image (.bg_next) to be the main image (.bg)
									});
								}
							};
						}
					});
			}
			else
			{
				$(this).addClass("disabled");
			}
			running = false;
		}
		return false;
	});
	
	$('.hide, .show').live("click",function(e) {
		if($('#visible').is(':hidden'))
		{
			$('#visible').show();
			$('#invisible').hide();
		}
		else
		{
			$('#visible').hide();
			$('#invisible').show();
		}
		
		h= $('#visible').is(':hidden');
	
		$.ajax({
			type: "POST",
			url: "hidden",
			data: ("hidden="+h)
		});
	});

	$( "#drag" ).draggable({ handle: ".dragHere", containment: "html", scroll: false });
	$( "#drag" ).bind( "dragstop", function(event, ui) {
	  var l = document.getElementById('drag').style.left;
	  var t = document.getElementById('drag').style.top;
	  
	  $.ajax({
			type: "POST",
			url: "position",
			data: ("left="+l+"&top="+t)
		});
	});

	if( $.browser.opera){
	  $('#bg').addClass("operabg");
	  $('#bg_next').addClass("operabg_next");
	  $('#bg, #bg_next').removeClass("image-holder").removeAttr("id");
	  $("body").css("overflow", "hidden");
	}

	$(".nav").live("click",function(e){
	// load home page on click
		e.preventDefault();
		$(".content-menu li").removeClass("active");
		var loadPage = $(this).attr('href');
		var part = "sub-"+loadPage.substring(loadPage.lastIndexOf('/') + 1);
		$('.content-text').fadeOut('slow', function(){
			$("#loaded").load(loadPage+' .content-text', function(){
				$("#"+part).addClass("active");
				$("#loaded").hide().fadeIn('slow');		
			});	
		});		
	});
	
	if ($('.about-menu li').hasClass('active'))
	{
		$(".about-menu li").removeClass("active");
		var current_url = $(location).attr('href');
		var current_part = "sub-"+current_url.substring(current_url.lastIndexOf('/') + 1);
		$("#"+current_part).addClass("active");
	}


	/*******************    Form Validation   *********************/

	$(".input input[type=text],.input textarea").live("blur",function(){
		validateInput($(this))
	});
	
	$("#contact-brochure-form").live("submit",function(event){
		if(validateForm($(this))){
			event.preventDefault();	
			brochureformSuccess()
		}else{
			event.preventDefault();	
			formFailure()
		}
	});
	
	$("#contact-visit-form").live("submit",function(event){	
		if(validateForm($(this))){
			event.preventDefault();	
			visitformSuccess()
		}else{
			event.preventDefault();	
			formFailure()
		}
	});
});

function validateForm(form){
	var valid = true;
	var inputs = form.find("input[type=text],textarea");
	
	$.each(inputs,function(index,elem){		
		if($(this).val() == ""){
			$(this).addClass("textInput-error");
			valid = false;
		}else if(valid){
			$(this).removeClass("textInput-error");
			
			
			/*********** issue here*****/
			if($(this).attr("name") == "email"){
				
				valid = checkEmail($(this).val());
				
				if(valid){
					$(this).removeClass("textInput-error");
				}else{
					$(this).addClass("textInput-error");
				}
			}
		}
	});	
	return valid;
};
		
function validateInput(input){
	var valid = true;

	if(input.val() == ""){
		input.addClass("textInput-error");
		valid = false;
	}else{
		input.removeClass("textInput-error");
		
		if(input.attr("name") == "email"){
			valid = checkEmail(input.val());
			
			if(valid){
				input.removeClass("textInput-error");
			}else{
				input.addClass("textInput-error");
			}
		}
	}
	return valid;
};
		
function brochureformSuccess(){
	var name = $('#validateBrName').val();
	var address = $('#validateBrAddress').val();
	var phone = $('#validateBrPhone').val();
	
	$("#brochureSubmit input").attr('disabled', 'disabled');

	$.ajax({
	   type: "POST",
	   url: $('#contact-brochure-form').attr('action'),
	   data: "name="+name+"&address="+address+"&phone="+phone+"",
	   success: function(msg){
	   $("#content-text-holder").fadeOut("slow", function(){
				if ($("#message-failure").is(":visible")){
					$("#message-failure").fadeOut("slow", function(){
						$("#message-success").fadeIn("slow").delay(3000).fadeOut("slow", function(){
						$("#content-text-holder").fadeIn("slow")
						$("#visitSubmit input").removeAttr('disabled');
						$("#contact-brochure-form input").not(':button, :submit, :reset, :hidden').val('');
						});
					});	
				}
				else
				{
					$("#content-text-holder").fadeOut("slow", function(){
						$("#message-success").fadeIn("slow").delay(3000).fadeOut("slow", function(){
						$("#content-text-holder").fadeIn("slow")
						$("#visitSubmit input").removeAttr('disabled');
						$("#contact-brochure-form input").not(':button, :submit, :reset, :hidden').val('');
						});
					});
				}
				$("#brochureSubmit input").removeAttr('disabled');
				$("#contact-brochure-form input, #contact-brochure-form textarea").not(':button, :submit, :reset, :hidden').val('');

			});
		}
	});	
};

function visitformSuccess(){
	var name = $('#validateName').val();
	var email = $('#validateEmail').val();
	var phone = $('#validatePhone').val();
	var visit = $('input:radio[name=visit]:checked').val(); 
	
	$("#visitSubmit input").attr('disabled', 'disabled');
	
	$.ajax({
	   type: "POST",
	   url: $('#contact-visit-form').attr('action'),
	   data: "name="+name+"&email="+email+"&phone="+phone+"&visit="+visit+"",
	   success: function(msg){
			
			if ($("#message-failure").is(":visible")){
				$("#message-failure").fadeOut("slow", function(){
					$("#message-success").fadeIn("slow").delay(3000).fadeOut("slow", function(){
						$("#content-text-holder").fadeIn("slow")
						$("#visitSubmit input").removeAttr('disabled');
						$("#contact-visit-form input").not(':button, :submit, :reset, :hidden').val('');
					});
				});	
			}
			else
			{
				$("#content-text-holder").fadeOut("slow", function(){
					$("#message-success").fadeIn("slow").delay(3000).fadeOut("slow", function(){
						$("#content-text-holder").fadeIn("slow")
						$("#visitSubmit input").removeAttr('disabled');
						$("#contact-visit-form input, #contact-visit-form textarea").not(':button, :submit, :reset, :hidden').val('');
					});
				});
			}
			
		}
	});	
};

function formFailure(){
	if ($("#message-success").is(":visible")){
		$("#message-success").fadeOut("slow", function(){
			$("#message-failure").fadeIn("slow");
		});	
	}
	else{
		$("#content-text-holder").fadeOut("slow", function(){
			$("#message-failure").fadeIn("slow");
		});
	}
};

function checkEmail(email)
{
	var regex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	return regex.test(email); 
}

function test_bg(){
		if(next_image == $("#bg img").attr('src'))
		{
			$("#bg").show();
			clearInterval();
		}
	};
