


// Plugin --- START --->

(function($){
$.fn.extend({
	hab_planer: function(options) {
		var settings = {
			type: 		"image",
			width:		false,
			height:		false,
			closeBtn:	"",
			onClose: function() {
			}, onComplete: function() {
			}
		}
		var options =  $.extend(settings, options);
		
		$("#plan-bg, .plan-close").live("click", function() {
			$("#plan-container, #plan-bg, .plan-close").remove();
			settings.onClose();
			return false;
		});
		
		function ajax_call(href, width, height) {
			
		}
		
		return this.live("click",function() {
			
			var href = $(this).attr("href");
			var rel = ($(this).attr("rel")) ? $(this).attr("rel") : "plan-main-content";
			
			$("body").append('<div id="plan-container"><div class="plan-close">'+settings.closeBtn+'</div><div id="plan-loading"></div></div><div id="plan-bg"></div>');
			
			if(settings.type == "iframe") {
				settings.width = (settings.width) ? settings.width : 800;
				settings.height = (settings.height) ? settings.height : 600;
				$("#plan-container").append('<iframe width="'+settings.width+'" height="'+settings.height+'" scrolling="auto" frameborder="0" src="'+href+'" hspace="0" name="plan-iframe" id="plan-type"></iframe>');
			
			} else if(settings.type == "image") {
				$("#plan-container").append('<img id="plan-type" src="'+href+'" alt="planriting" />');
			}
			
			$("#plan-container").addClass(rel);
			$("#plan-type").hide();
			$("#plan-type").load(function() {
				$("#plan-type").show();
				$("#plan-loading").remove();
				
				var height = $(window).height();
				var height_img = $("#plan-type").height();
				if(height > height_img) {
					height = height_img;
				} else {
					height = height - 60;
				}
				
				if(!settings.type == "image") { $("#plan-type").css("height",  height+"px").css("width", "auto"); }
				var width = $("#plan-type").width();
				var marginTop = height / 2;
			
				$("#plan-container").css("width",  width+"px").css("marginLeft", "-" + (width/2) + "px");
				return false;
			});
			var height = settings.height / 2;
			$("#plan-container").css("width", settings.width+"px").css("height", settings.height+"px").css("marginLeft", "-" +(settings.width/2)+"px");
			if($(window).scrollTop() != 0) {
				$("#plan-container").css("top", ($(window).scrollTop() + 50)+"px").css("marginTop", "0");
			} else {
				$("#plan-container").css("top", "100px").css("marginTop", "0");
			}
			 if(settings.type == "ajax") {
				
				$.ajax({
					method: "get",url: href, data: "",
					beforeSend: function(){ },
					complete: function(){ $("#plan-loading").remove(); },
					success: function(html) {
					
						$("#plan-container").append('<div id="plan-type" style="width: '+settings.width+'px; height: '+settings.height+'px;">'+html+'</div>');
						settings.onComplete();
					}
				});				
				
				var scrolling = "";
				$("#plan-container").scroll(function(e) {
					scrolling = $(this).scrollTop();
					$(".plan-close").animate({ top: (scrolling + 15)+"px"}, 60);
				});
				return false;
			 }
			
			return false;
		});
	}
});
     
})(jQuery);


// Get Ready --- START --->

$(document).ready(function() {
	$(".bg").width($(window).width()).height($(window).height());
	$(window).resize(function() {
		$(".bg").width($(this).width()).height($(this).height());
	});
	
	$(".movie").hide();
	
	$(".hab-planer").hab_planer({ type: "ajax", width: 640, height: 383 });
	
	
	$(".hab-planer-slider").hab_planer({ type: "ajax", width: 820, height: 630, onComplete: function() {
			
			
			
			var animates = "slide";
			var speed = 300;
			var timer = 2000;
			var ClickStopInterval = true;
			var totWidth = 0;
			var maxCount = 0;
			var count = 1;
			var positions = new Array();
			$('#animate-content .animate').each(function(i){
				positions[i]= totWidth;
				totWidth += $(this).width();
				if(!$(this).width()) {
					alert("Please, fill in width & height for all your images!");
					return false;
				}
				maxCount++;
			});
			
			
			if(count == 1) { $('#prev').addClass("inac"); }
			$('#next a').click(function(e,keepScroll) {
			
				$('#prev').removeClass("inac");

				if(count != maxCount) {
					count++;
					$('#menu-animate ul li a[href='+count+']').trigger("click");
					if(count == maxCount) {						
						$(this).parent().addClass("inac");
						count = maxCount;
					}
					
				}
			
			});
			
			$('#prev a').click(function(e,keepScroll) {
			
				$('#next').removeClass("inac");
				if(count != 1) {
					count--;
					$('#menu-animate ul li a[href='+count+']').trigger("click");
					if(count == 1) {
						$(this).parent().addClass("inac");
						count = 1;
					}
				}
		
			});
			
			
			$('#animate-content').width(totWidth);
			$('#menu-animate ul li a').click(function(e,keepScroll) {
			
				$('li.menuItem').removeClass('act').addClass('inact');
				$(this).parent().addClass('act');
				
				var pos = $(this).parent().prevAll('.menuItem').length;
				count = pos;
				count++;
				
				if(count != maxCount) {						
					$('#next').removeClass("inac");
				} else {
					$('#next').addClass("inac");
				}
				
				if(count != 1) {						
					$('#prev').removeClass("inac");
				} else {
					$('#prev').addClass("inac");
				}

				
				if(animates == "fade") {
					$(".animate").fadeOut(speed, function() {
						$('#animate-content').stop().animate({marginLeft:-positions[pos]+'px'},0);
						$(".animate").fadeIn(speed);
					});
				} else if(animates == "slide") {
					$('#animate-content').stop().animate({marginLeft:-positions[pos]+'px'}, speed);
				}
				
				e.preventDefault();
				// Stopping the auto-advance if an icon has been clicked:
				/*if(!keepScroll && ClickStopInterval == true) { 
					clearInterval(itvl);
				}*/
			});
			
			$('#menu-animate ul li.menuItem:first').addClass('act').siblings().addClass('inact');
		
			/*var current=1;
			function autoAdvance() {
				if(current==-1) return false;
				$('#menu-animate ul li a').eq(current%$('#menu-animate ul li a').length).trigger('click',[true]);
				current++;
			}
			var itvl = setInterval(function(){autoAdvance()}, timer);*/
		}, onClose: function() {
		}
	
	});
	
	
});
