var ImageArray = [];
var pos = 0;
var wait = false;

function _wait(flag) {
	try {
		$ = jQuery;
		if (flag) {
			wait = true;
			$('#wait').stop(true,true).fadeIn(50);
			$('.button').addClass('disabledB').attr('readonly','readonly');
			$('.text').addClass('disabledT').attr('readonly','readonly');
		} else {
			wait = false;
			$('#wait').stop(true,true).fadeOut(50);
			$('.button').removeClass('disabledB').removeAttr('readonly');
			$('.text').removeClass('disabledT').removeAttr('readonly');
		}
	} catch(e) { return false; }
}

jQuery(document).ready(function($) {

	/* Home floorplans */
	floorPre = new Image();
	floorPre.onload = function() { $('#floorplan img').fadeIn(1000); };

	var hideStInfTimer;
	floorPre.src = siteurl+'/wp-content/themes/Pier59/images/floorplan.png';
	$('.hiddenTwo').unbind('mouseenter mouseleave').hover(function(){
		var iddd = $(this).attr('id');
		if (!$(this).hasClass('noInf')) {
			if ($('#img'+iddd).size()>0) {
				var pre = new Image();
				pre.onload = function() { $('#studioInfo').html('<img src="'+$('#img'+iddd).attr('src')+'" alt=""/>'); };
				pre.src = $('#img'+iddd).attr('src');
				clearTimeout(hideStInfTimer);
				$('#studioInfo').stop(true,true).fadeIn(100);
			}
		}
		$('.'+iddd).stop(true,true).animate({'opacity':1}, 300);
	},function(){
		var iddd = $(this).attr('id');
		if (!$(this).hasClass('noInf')) hideStInfTimer = setTimeout(function() { $('#studioInfo').stop(true,true).fadeOut(300); }, 200);
		$('.'+iddd).stop(true,true).animate({'opacity':0}, 200);
	});


	/* Gallery */

	if ($(".gallery").length) {
		var galleryLength = $(".gallery ul li").length;

		if (galleryLength) {
			var fancyboxOptions = {onStart: function() { clearTimeout(imageTimeout); }, onClosed: function() { imageTimeout = setTimeout(nextImageClick, 3000); }, overlayColor: '#000', overlayOpacity: 0.75};
			var pos = 0;
			var imageTimeout = null;
	
			var nextImageClick = function() {
				$(".gallery ul li:eq("+pos+")").fadeOut(300, function() {
					pos++;
					if (pos >= galleryLength) pos = 0;
					$(".gallery ul li:eq("+pos+")").fadeIn(300, function() {
						imageTimeout = setTimeout(nextImageClick, 3000);
					});
				});
			}
	
			$('.gallery ul li a').fancybox(fancyboxOptions);
			imageTimeout = setTimeout(nextImageClick, 3000);
		}
	}



	/* Old code */
	
	$('.hidden,.hiddenTwo').css('opacity',0);
	$('.hidden').hover(
		function() { $(this).stop(true,true).animate({'opacity': 0.5}, 300); },
		function() { $(this).stop(true,true).animate({'opacity': 0}, 200); }
	);

	$('.scroll-area').scrollbar({arrows: false});

	if (location.hash) {
		var h = '#'+(location.hash).replace(/^#/,'').toLowerCase().replace(/[^a-z0-9]/ig,'');
		$('.white_red_list a[href='+h+']').trigger('dblclick');
	} else {
		$('#sidebarBackground').css('opacity', 0.7);
	}


	// Contact Form

	$('.contactForm input[type=text],.contactForm textarea').blur(function() {
		if (trim($(this).val()) != '') $(this).removeClass('error');
	});

	$('.contactForm .sendMessage').click(function() {
		var buttonClicked = $(this);
		var buttonClickedP = buttonClicked.parent().parent().parent();
		var errors = 0;

		buttonClickedP.find('input[type=text],textarea').each(function() {
			if (trim($(this).val())=='') {
				errors++;
				$(this).addClass('error');
			} else {
				$(this).removeClass('error');
			}
		});

		if (errors == 0) {
			_wait(true);
			var sForm = $(buttonClickedP).parent().parent().parent();
			var sSuccess = $(sForm).parent().find(".contactSuccess");
			var sSection = buttonClickedP.parent().parent().find('.sSection').val();
			var sName = trim(buttonClickedP.find('.sName').val());
			var sCompany = trim(buttonClickedP.find('.sCompany').val());
			var sEmail = trim(buttonClickedP.find('.sEmail').val());
			var sPhone = trim(buttonClickedP.find('.sPhone').val());
			var sMessage = trim(buttonClickedP.find('.sMessage').val());

			buttonClicked.attr('disabled','disabled').addClass('disableSend');
			$.ajax({
				type: $(sForm).find("form").attr("method"),
				url: $(sForm).find("form").attr("action"),
				data: {contactSection: sSection, name: sName, company: sCompany, email: sEmail, phone: sPhone, message: sMessage},
				success: function(msg) {
					_wait(false);
					$(sForm).fadeOut("medium", function() {
						$(sSuccess).fadeIn("medium", function() {
							buttonClicked.removeAttr('disabled').removeClass('disableSend');
							buttonClickedP.find('.sName').val('');
							buttonClickedP.find('.sCompany').val('');
							buttonClickedP.find('.sEmail').val('');
							buttonClickedP.find('.sPhone').val('');
							buttonClickedP.find('.sMessage').val('');
							setTimeout(function() {
								$(sSuccess).fadeOut("medium", function() { $(sForm).fadeIn("medium"); });
							}, 15000);
						});
					});
				}
			});
		}
	});


	// Equipment code (perhaps to be removed)
	
	$('.eqRow .intCont .text').bind('blur', function() {
		var val = $(this).val().replace(/[^0-9]/ig,'');
		if (val=='' || val<=0) val = 1;
		$(this).val(Number(val));
	});

	$('.eqRow .intCont .text').bind('keyup',function() {
		var val = $(this).val().replace(/[^0-9]/ig,'');
		$(this).val(val);
	});
	
	$('.eqRow .intCont .add').click(function() {
		if (wait) return;
		var val = $(this).parent().find('input[type=hidden]').val()+'|'+$(this).parent().find('.text').val();
		_wait(true);
		$.ajax({
			type: "GET",
			url: siteurl+"/wp-content/plugins/pier59/q.php",
			data: "addEquipment="+encodeURIComponent(val),
			success: function(msg) { _wait(false); }
		});
	});
	
	$('#wishlistlink').click(function() {
		var that = $(this);
		if ($(this).text()=='GO BACK') {
			$(this).text('VIEW WISHLIST');
			$('#contentPageSlider').stop(true,true).animate({'left':'0px'},400);
		} else {
			if (wait) return;
			_wait(true);
			$.ajax({
				type: "GET",
				url: siteurl+"/wp-content/plugins/pier59/q.php",
				data: "getWishlist=1",
				success: function(msg) {
					_wait(false);
					that.text('GO BACK');
					$('.wishlistContainer').html("<h2>Wishlist</h2><div id='wishlist' style='margin-bottom:50px;width:646px;'></div><div class='contactFormContainer' style='margin-bottom:50px;'> <div class='contactForm'> <table cellspacing='0'> <tr> <td style='width:180px;'> <div>Name</div><input class='sName' style='width:158px;' type='text'/> </td> <td style='width:180px;'> <div>Company</div><input class='sCompany' style='width:158px;' type='text'/> </td> </tr> <tr> <td> <div>Email</div><input class='sEmail' style='width:158px;' type='text'/> </td> <td> <div>Phone</div><input class='sPhone' style='width:158px;' type='text'/> </td> </tr> <tr> <td colspan='2'> <div>Message</div><textarea class='sMessage' style='width:340px;height:50px;' rows='3' cols='15'></textarea> </td> </tr> <tr> <td colspan='2' style='text-align:right;'><input class='button sendMessage' type='button' value='Send Wishlist'/></td> </tr> </table> </div> </div>");
					$('#wishlist').html(msg);
					$('#contentPageSlider').stop(true,true).animate({'left':'-720px'},400);
					$('.eqRow .intCont .update').unbind('click').click(function(){
						if(wait)return;
						var val = $(this).parent().find('input[type=hidden]').val()+'|'+$(this).parent().find('.text').val();
						_wait(true);
						$.ajax({
							type: "GET",
							url: siteurl+"/wp-content/plugins/pier59/q.php",
							data: "editEquipment="+encodeURIComponent(val),
							success: function(msg) { _wait(false); }
						});
					});
					$('.wishlistContainer').scrollbar({arrows: false});
				}
			});
		}
		return false;
	});
	
	$('#homehover').click(function() {
		$('#contentSlider').html($('#floorplan_c').html());
		$('#contentSlider').css({'left':0,'display':'none'}).fadeIn(300);
		$('.hiddenTwo').unbind('mouseenter mouseleave').hover(function() {
			var iddd = $(this).attr('id');
			$('.'+iddd).stop(true,true).animate({'opacity':1},300);
		}, function() {
			var iddd = $(this).attr('id');
			$('.'+iddd).stop(true,true).animate({'opacity':0},200);
		});
		return false;
	});
	
	$('.gotoGallerySt').click(function() {
		$('.studioSlider').animate({'left':'0px'},400);
		return false;
	});
	$('.goto360').click(function() {
		$('.studioSlider').animate({'left':'-1440px'},400);
		return false;
	});
	$('.gotoSpecsSt').click(function() {
		$('.studioSlider').animate({'left':'-720px'},400);
		return false;
	});

	$('.ImageClick,.ImageClick').css('opacity',0).hover(function() {
		$(this).stop(true,true).animate({'opacity':0.3},200);
	}, function() {
		$(this).stop(true,true).animate({'opacity':0},500);
	});
	
	var expMaxHeight = 0;
	var lastList = 0;
	if ($('.expandable .white_red_list ul a.selected').size()>0) {
		ul = $('.expandable .white_red_list ul a.selected').parent().parent();
		ul.css('display','block');
		lastList = ul;
	}
	$('.expandable .white_red_list ul').each(function() {
		if($(this).height()>expMaxHeight)
			expMaxHeight = $(this).height();
	});
	$('.expandable .white_red_list ul').height(expMaxHeight);
	$('.expandable .white_red_list a').click(function() {
		var list = $(this).parent().find('ul');
		if (list.size()>0) {
			if (lastList == 0 || lastList!=list) {
				list.css({'height':0,'display':'block'}).animate({'height':expMaxHeight},{ 'duration': 200, 'queue': false },'linear');
				if(lastList != 0)lastList.animate({'height':0},{ 'duration': 200, 'queue': false },'linear',function(){$(this).css({'display':'none'})});
				lastList = list;
			}
			return false;
		}
	});


	/* IE CSS */
	if ($.browser.msie) {
		$(".menu-footer ul li:first, .menu-footer ul li:last").css("margin-left", 0);
		$(".menu-footer ul li:last").css("float", "right");		
	}
});

function showContent(i) {
	if (jQuery('#sidebarlink'+i).hasClass("selected")) {
		jQuery('#sidebarBackground').animate({'opacity': 0.7}, 200);
		clearTimeout(landingTimer);
		jQuery('#contentPageSlider').stop(true,true).animate({'left': 720+'px'}, 400);
		jQuery('.white_red_list a').removeClass('selected');
	} else {
		jQuery('#sidebarBackground').animate({'opacity': 1}, 200);
		clearTimeout(landingTimer);
		jQuery('#contentPageSlider').stop(true,true).animate({'left':'-'+(i*720)+'px'}, 400);
		jQuery('.white_red_list a').removeClass('selected');
		jQuery('#sidebarlink'+i).addClass('selected');
	}
}

function showContentF(i) {
	jQuery('#contentPageSlider').stop(true,true).css({'left':'-'+(i*720)+'px'});
	jQuery('.white_red_list a').removeClass('selected');
	jQuery('#sidebarlink'+i).addClass('selected');
}

var landingTimer;
var landingPos = 0;
function goGalleryGo() {
	if (landingImages.length > 1) landingTimer = setTimeout(cycleImages,5000);
}

function trim(S) {
	S=S.replace(/ /g,' ');
	return S.replace(/(^\s+)|(\s+$)/g, '');
}

function cycleImages() {
	landingPos++;
	if (landingPos>=landingImages.length) landingPos = 0;
	var ima = new Image();
	ima.onload = function() {
		jQuery('.landingGallery img').fadeOut(300,function() {
			jQuery(this).attr('src',landingImages[landingPos]);
			jQuery(this).fadeIn(300);
			landingTimer = setTimeout(cycleImages,5000);
		});
	}
	ima.src = landingImages[landingPos];
}

