﻿//LayerCancel || rel = HideLoadBar
//ShowLayer(this)
var ShowLayer, RefreshLayerSize	//function(defined through the code)

$(function () {

	//conf
	var _coverOpacity = 0.3,
	_loadingOpacity = 1,
	_animDuration = 100,
	_layerFrameName = "frameLayer",
	_closeKeyword = "CloseLayer",
	_defaultDimensions = [200, 100],
	_bodersSize = [24, 88],
	_hScrollW = 20

	window.layer = {}
	var _this = this,
	$cover = $('.Cover'),
	$layer = $('.FrameLayerContainer'),
	$container = $layer.find('.Container'),
	$loading = $layer.find('.Loading'),
	$btnTriggers = $('[target="' + _layerFrameName + '"]'),
	frame = window[_layerFrameName],
	$btnCloses	//inside of Layer
	function myExp(array, exp) {
		return eval(exp.split('{').join('array[').split('}').join(']'))
	}
	$cover.css({ opacity: 0 })

	function HideLoading(callBack) {
		if (!callBack) callBack = function () { }
		$loading.fadeOut(_animDuration, callBack)
	}
	window.layer.HideLoading = HideLoading;

	//Loading GUI
	function loadingGUI(boolOn, callBack) {
		if (!callBack) callBack = function () { }
		var $layerBody = $(frame.document.body) || $(frame), width, height
		$layerBody.css({ position: 'absolute' })
		width = myExp([$layerBody.width(), _defaultDimensions[0], $container.width()], "({0}>{1} && {2}!=0)?{0}:{1}")
		height = myExp([$layerBody.height(), _defaultDimensions[1], $container.width()], "({0}>{1} && {2}!=0)?{0}:{1}")
		width = myExp([width, $(window).width()], "{0}+" + _bodersSize[0] + ">{1}?{0}-({0}-{1}+" + _bodersSize[0] + "):{0}") + (height > $(window).height() ? _hScrollW : 0) + 'px'
		height = myExp([height, $(window).height()], "{0}+" + _bodersSize[1] + ">{1}?{0}-({0}-{1}+" + _bodersSize[1] + "):{0}") + 'px'
		$loading.css({ height: height })
		if (boolOn) {
			$('.LayerDress').css({ display: 'block' })
			$container.fadeIn()
			parentCorrectionInGoogleChrome(width, height)
			$container.animate({ width: width, height: height }, _animDuration, '', function () {
				$loading.fadeTo(_animDuration, _loadingOpacity, callBack)
			})
		} else {
			$layerBody.css({ display: 'none' })
			$('.LayerDress').css({ display: 'none' })
			$container.animate({ width: width, height: height }, _animDuration, '', function () {
				$layerBody.css({ display: 'block' })
				HideLoading(callBack)
				parentCorrectionInGoogleChrome(width, height)
			})
		}
	}
	function parentCorrectionInGoogleChrome(width, height, CallBack) {
		width = parseInt(width) + _bodersSize[0] + "px"; height = parseInt(height) + _bodersSize[0] + "px";
		$container.parents("table:first").css({ width: width, height: height })
	}
	window.layer.loadingGUI = loadingGUI

	ShowLayer = function (obj) {
		var href = (typeof (obj) == 'string') ? obj : obj.href
		$cover.css({ display: 'block', height: ($(window).height() - 1) + 'px' })
		$cover.fadeTo(_animDuration, _coverOpacity, function () {
			$layer.css({ display: 'block' })
			$layer.find("td:first").css({ height: ($(window).height() - 1) + 'px' })	//resolve ie6 bug
			loadingGUI(true, function () {
				frame.location = href
			})
		})
	}
	RefreshLayerSize = function () {
		loadingGUI(false)
	}

	//Triggers
	$btnTriggers.click(function () {
		ShowLayer(this)
		return false
	})

	function CloseBtnClick() {
		frame.location.href = "about:blank"
		var $layerBody = $(frame.document.body) || $(frame)
		$('.LayerDress').css({ display: 'block' })
		$layerBody.css({ display: 'none' })
		$container.animate({ height: '0px', width: '0px' }, _animDuration, '', function () {
			//			frame.document.body.innerHTML=""
			$layer.css({ display: 'none' })
			$cover.fadeOut(_animDuration, function () {
				$cover.css({ opacity: 0, display: 'none' })
			})
		})
	}
	$('.FrameLayerContainer .' + _closeKeyword).click(CloseBtnClick)

	//Onload
	$("iframe", $container).load(function () {
		loadingGUI(false)
		var $layerBody = $(frame.document.body)
		//Inner Triggers
		$("a[href!='javascript:;'][href!=''], input[type='submit']", frame.document.body).click(function () {
			var jsKeyword = 'javascript:', sender = this, isLink = (this.tagName.toLowerCase() == 'a')
			if (this.LayerCancel || (typeof (this["rel"]) != 'undefined' && this.rel.indexOf('HideLoadBar') > -1)) {
				var isCancel = this.LayerCancel ? this.LayerCancel : 'HideLoadBar'
				this.LayerCancel = 0
				return (isCancel == "HideLoadBar" ? true : false)
			}
			loadingGUI(true, function () {
				if (isLink) {
					if (sender.href.indexOf(jsKeyword) == 0) {
						frame.eval(sender.href.substr(jsKeyword.length).split('%20').join(' '))
					} else {
						loadingGUI(true, function () {
							frame.location = sender.href
						})
					}
				} else {
					sender.LayerCancel = "HideLoadBar"
					sender.click()
				}
			})
			return false
		})
		//Close Btn
		$btnCloses = $("." + _closeKeyword, frame.document.body)
		$btnCloses.attr('href', 'javascript:')
		$btnCloses.click(CloseBtnClick)
	})
	$(window).resize(function () {
		loadingGUI(false);
		$cover.css({ height: ($(window).height() - 1) + 'px' })
		$layer.find("td:first").css({ height: ($(window).height() - 1) + 'px' })	//resolve ie6 bug
	})
})
