/* [ Main Script repository for MVKTheMovie.com ] */

$.extend($.fn, {
   observe: function($observed, event, callback){
      var $this = $(this);
      $observed.bind(event, function(e){ callback.apply($this, [e]); });
      return $this;
   }
});


// Initialization
$(function(){
	
	MVK.MainNav.init();
	// MVK.ScrollPanel.init();
	
	// Launch external links in new window
	$("a[rel='external']").bind("click.external", function(){
		window.open(this.href);
		return false;
	});
	
	// Open Home page content
	$("li.nav-home a").click();
	// Also clicking on Title shows homepage content
	$("div.mainHeader > a[href='#Home']").click(function(){
		$("li.nav-home a").click();
		return false;
	});
	
});

// Obj declarations
MVK = {
	MainNav: {
		_contentArea: "div.textContainer",
		_navItems: "div.contentContainer > div.nav > ul > li",
		_opened: ".opened",
		functions: {
			openPanel: function(){

				// Remove scrollbars from any remaining panels (can do this a more efficient way -- find last selected and target it directly)
				try { $(".panel").jScrollPaneRemove(); } catch(e) {}

				var $this = $(this),
					$panel = $($this.attr("href"));
				MVK.MainNav.$navItems.find("a").removeClass("selected");

				if (MVK.MainNav.$opened && (MVK.MainNav.$opened != $panel)) {
					MVK.MainNav.$opened.stop().fadeOut("fast");
					MVK.MainNav.$opened[0].scrollTop = 0;
				};
				$panel[0].scrollTop = 0;
				$panel.stop().fadeIn("fast", function(){

					if($panel.find("div.panel-inner").height() > MVK.MainNav.$contentArea.height()){
						$panel.jScrollPane({
							scrollbarWidth:11,
							scrollbarMargin:10,
							showArrows:true,
							dragMaxHeight:50
							/*,
							scrollbarOnLeft:true*/
						});

						// MVK.ScrollPanel.$control.trigger("show.scrollPanel");
					} else {
						try { $panel.jScrollPaneRemove(); } catch(e) {}
						// MVK.ScrollPanel.$control.trigger("hide.scrollPanel");
					};
				});
				MVK.MainNav.$opened = $panel;
				$(MVK.MainNav).trigger("opened.panel");
				$this.addClass("selected").blur();

				return false;
			},
		},
		init: function(){
			this.$contentArea = $(this._contentArea);
			this.$navItems = $(this._navItems);
			this.$navItems
				.find("a[rel='panel']")
					.bind("click.MainNav", MVK.MainNav.functions.openPanel)
					.end()
		}
	},
	
	ScrollPanel: {
		_control: "div.textContainer > div.control",
		scrollInterval: null,
		init: function(){
			this.$control = $(MVK.ScrollPanel._control);
			this.$control
				.bind("hide.scrollPanel", MVK.ScrollPanel.hide)
				.bind("show.scrollPanel", MVK.ScrollPanel.show)
				.find("a")
					.bind("mouseout.scrollPanel", function(){
						clearInterval(MVK.ScrollPanel.scrollInterval);
					})
					.filter(".control-up")
						.bind("mouseover.scrollPanel", function(){
							MVK.ScrollPanel.scrollInterval = setInterval(MVK.ScrollPanel.up, 20);
						})
						.end()
					.filter(".control-down")
						.bind("mouseover.scrollPanel", function(){
							MVK.ScrollPanel.scrollInterval = setInterval(MVK.ScrollPanel.down, 20);
						})
		},
		up: function(){
			if(!MVK.MainNav.$opened) { return false; };
			MVK.MainNav.$opened[0].scrollTop --;
			return false;
		},
		down: function(){
			if(!MVK.MainNav.$opened) { return false; };
			MVK.MainNav.$opened[0].scrollTop ++;
			return false;
		},
		hide: function(){ MVK.ScrollPanel.$control.stop().hide(); },
		show: function(){ MVK.ScrollPanel.$control.stop().show(); }
	},
	
};
