/**
 * xperia.js
 */
(function($){
	$(function(){
		//start url
//		var startUrl = "http://dcm-gate-dev.mobilejst.com/Xperia/AND/";
//		var startUrl = "first.html";
var startUrl = "Xperia/AND/index.html";
		
		//scroll pane
		var $pane = $("div#xperia_pane");
		//iframe
		var $iframe = $("iframe#xperia_content");
		//loading
		var $loading = $("div#xperia_loading");
		
		//jScrollPane options
		var jScrollPaneOptions = {
			showArrows:true,
			maintainPosition:false,
			scrollbarWidth:13,
			arrowSize:21
		};

		//bind action to iframe 
		$iframe
			.unload(onUnloadIframe)
			.load(onLoadIframe)
			//load start url
			.attr("src", startUrl)
		;

		//show loading
		$loading.show();

		/**
		 * adjust iframe height
		 */
		function adjustIframeHeight(){
			//alert("adjustIframeHeight");
			$iframe
				//first, set 0
				.height(0)
				//next, adjust to iframe content
				.height($($iframe.contents()).height());
		}

		/**
		 * iframe unload action
		 */
		function onUnloadIframe(e){
			//remove scroll pane, hide pane
			$pane.jScrollPaneRemove();
			//hide pane
			$pane.hide();
			//show loading
			$loading.show();
		}

		/**
		 * iframe load action
		 */
		function onLoadIframe(e){
			//hide loading
			$loading.hide();
			//show pane
			$pane.show();
			//adjust iframe height
			adjustIframeHeight();

			//check anchors
			var $ancs = $($iframe.contents()).find("a, area");
			$ancs.click(function(e){
				var $anc = $(this);
				var target = $anc.attr("target");
				var href = $anc.attr("href");
				var prevent = false;
				//target not _self
				if(target && target != "_self"){
					alert("target="+target);
					prevent = true;
				}
				//href starts with http: or https:
				if(href){
					if(href.match(/^https?:/)){
						alert("href="+href);
						prevent = true;
					}
					if(href.match(/.+\.wmv($|\?.*|#.*)/)){
						alert("href="+href);
						prevent = true;
					}
				}
				if(prevent){
					//stop default
					e.preventDefault();
					return false;
				}
			});

			//check forms
			var $forms = $($iframe.contents()).find("form");
			$forms.submit(function(e){
				var $form = $(this);
				var target = $anc.attr("target");
				var action = $anc.attr("action");
				var prevent = false;
				//target not _self
				if(target && target != "_self"){
					alert("target="+target);
					prevent = true;
				}
				//action starts with http: or https:
				if(action){
					if(action.match(/^https?:/)){
						alert("action="+action);
						prevent = true;
					}
				}
				if(prevent){
					//stop default
					e.preventDefault();
					return false;
				}
			});

			//bind jScrollPane
			$pane.jScrollPane(jScrollPaneOptions);

			//unbind jscrollPane focus action of iframe(scroll to element top)
			$iframe.unbind("focus");
		}

	});
})(jQuery);
