function InsBox(config){
	this.bgClass = config['bg_class'];
	this.frontClass = config['front_class'];
	this.linkClass = config['link_class'];
	this.formClass = config['form_class'];
	this.resultLayerId = null;
	this.resultLayerCreated = false;
	this.boxCreated = false;
	this.isBlock = config['block'];
	this.getInstanceWidth = 0;
	this.getClientWidth = 0;
	this.setLeft = 0;	
	
	this.topHeight = function(){
		var checkIe = /*@cc_on!@*/false;
		if (checkIe == 1){
			return document.documentElement.scrollTop + 100;						
		} else {
			return window.pageYOffset + 100;
		}		
	};

	this.getClientWidth = function(){
		var tempChild = $('.boxLinkFront').children();
		var getW = parseInt(tempChild.children().css("width"));
		
		var clientW = 0;
		
		if (window.innerWidth === "undefined") {
			
			clientW = document.body.clientHeight;
		}
		else {
			clientW = document.documentElement.clientWidth;
		}
		
		getW = Math.round((clientW/2) - (getW/2));
		return getW; 
	}
						
					
	this.init = function(){
		
		this.bind();
		var hash = document.location.hash;
		hash = hash.replace('#','');
		if(hash != ''){
			if(hash in InsBoxRoutes){
				if(InsBoxRoutes[hash].indexOf('insbox='+this.linkClass)>-1){
					this.gotoUrl(InsBoxRoutes[hash]);
				}
			}
		}
	};

	this.bind = function(){
		this.unbind();
		$("."+this.linkClass).bind('click', {instance: this}, function(e){
			e.data.instance.gotoUrl($(this).attr('href'));
			return false;
		});

		$("."+this.linkClass+'_close').bind('click', {instance: this}, function(e){
			e.data.instance.close();
			return false;
		});
		
		$("."+this.formClass).bind('submit',{instance: this}, function(e){
			var form = this;
			var method = $(form).attr('method');
			if(!method){
				method = 'POST';
			}
			jQuery.ajax({
				url: $(form).attr('action'),
				type: method,
				data: $(form).serialize(),
				beforeSend  : function() {
					
					$('#'+e.data.instance.getResultLayerId()).show().html('<div class="loading" style="top:'+e.data.instance.topHeight()+'"px; width:400px;"></div>');
				},
				success: function(text) {
					$('#'+e.data.instance.getResultLayerId()).hide().html(text);
					$('#'+e.data.instance.getResultLayerId()).fadeIn('fast');
				this.getInstanceWidth = $('#'+e.data.instance.getResultLayerId()).width();
				
				$('#'+e.data.instance.getResultLayerId()).css({
					"left" : e.data.instance.getClientWidth()+"px",
					"top" : e.data.instance.topHeight()+"px"});		
				$('.boxLinkFront').css({"left" : e.data.instance.getClientWidth()+"px", "top" : e.data.instance.topHeight()+"px"});					
					var tempHeight = $('.boxLinkFront').height();
					var tempHeight = tempHeight + document.body.clientHeight;
					
					$('.boxLinkBg').css("height", tempHeight+"px");					
				}
				
			});
			
			return false;
		});
	};

	this.unbind = function(){
		$("."+this.linkClass).unbind('click');
		$("."+this.formClass).unbind('submit');
	};

	this.createId = function(){
		return 'result_layer_'+Math.floor(Math.random()*10000)+1;
	};

	this.gotoUrl = function(url){
		this.createResultLayer();
		this.createBox();
		var instance = this;
		jQuery.ajax({
			url: url,
			type: 'GET',
			beforeSend  : function() {
				$('#'+instance.getResultLayerId()).show().html('<div style="top:'+instance.topHeight()+'px" class="loading"></div>');
			},
			success: function(text) {
				$('#'+instance.getResultLayerId()).hide().html(text);
				$('#'+instance.getResultLayerId()).fadeIn('fast');
				$('.boxLinkFront').css({"top" : instance.topHeight()+"px", "left" : instance.getClientWidth()+"px"});			
				instance.loaded();	
					var tempHeight = $('.boxLinkFront').height();
					var tempHeight = tempHeight + document.body.clientHeight;
					
					$('.boxLinkBg').css("height", tempHeight+"px");							
			}
		});
		this.updateHash(url);
	};

	this.loaded = function(){
		this.bind();
	};

	this.createResultLayer = function(){
		if(!this.resultLayerCreated){
			$('body').append('<div id="'+this.getResultLayerId()+'"></div>');
		}
		this.resultLayerCreated = true;
	};

	this.getResultLayerId = function(){
		if(!this.resultLayerId){
			this.resultLayerId = this.createId();
		}
		return this.resultLayerId;
	};

	this.destroyResultLayer = function(){
		$("#"+this.getResultLayerId()).remove();
		this.resultLayerCreated = false;
	};

	this.destroyBox = function(){
		$("#bg_"+this.getResultLayerId()).css('display','none');
		$("#front_"+this.getResultLayerId()).css('display','none');
		$("#bg_"+this.getResultLayerId()).remove();
		$("#front_"+this.getResultLayerId()).remove();
		
		$("#ieiframefix").css('display','none').remove();
		this.boxCreated = false;
	};

	this.createBox = function(){
		if(!this.boxCreated){	
			$('body').append('<!--[if ie 6]><iframe id="ieiframefix" frameborder="0" hspace="0"></iframe><![endif]--><div id="bg_'+this.getResultLayerId()+'" class="'+this.bgClass+'" style="display:none"></div>');
			$('body').append('<div id="front_'+this.getResultLayerId()+'" class="'+this.frontClass+'" style="top:'+ this.topHeight() +'px; display:none;"></div>');
	
			$('#front_'+this.getResultLayerId()).append($("#"+this.getResultLayerId()));
			$("#bg_"+this.getResultLayerId()).css('display','block');
			$("#front_"+this.getResultLayerId()).css({
				'display': 'block',
				'top':	 this.topHeight() +'px'
			});
			var tempHeight = $('.boxLinkFront').height();
			var tempHeight = tempHeight + document.body.clientHeight;
					
			$('.boxLinkBg').css("height", tempHeight+"px");	
		}
		if(!this.isBlock){
			$("#bg_"+this.getResultLayerId()).bind('click',{instance:this},function(e){
				e.data.instance.close();
			});
		}
		this.boxCreated = true;
	};

	this.close = function(){
		this.destroyResultLayer();
		this.destroyBox();
		document.location.hash = '#';
	};

	this.updateHash = function(url){
		var hash = null;
		var length = 0;
		var urlArray = url.split('?'); // ucinamy parametry z get'a
		url = urlArray[0];
		urlArray = url.split('.');
		url = urlArray[0];

		var currentRoute = null;
		for(key in InsBoxRoutes){
			currentRoute = InsBoxRoutes[key];
			urlArray = currentRoute.split('.');
			currentRoute = urlArray[0];
			if(url.indexOf(currentRoute) !== null && url.indexOf(currentRoute) != -1){
				if(currentRoute.length > length){
					length = currentRoute.length;
					hash = key;
				}
			}
		}
		if(hash != null){
			window.location.hash = "#"+hash;
		}
	};
}



/*

var InsBox = {
	dialogVisible: false,

	init: function(){
		if(!$("#resultLayer")){
			$("body").append('<div id="resultLayer" style="display:none"></div>');
		}
		
		InsBox.bindBoxes();
		var hash = document.location.hash;
		hash = hash.replace('#','');
		if(hash != ''){
			if(hash in InsBoxRoutes){
				InsBox.loadUrl(InsBoxRoutes[hash]);
			}
		}
	},

	updateHash: function(url){
		var hash = null;
		var length = 0;
		var urlArray = url.split('?'); // ucinamy parametry z get'a
		url = urlArray[0];
		urlArray = url.split('.');
		url = urlArray[0];

		var currentRoute = null;
		for(key in InsBoxRoutes){
			currentRoute = InsBoxRoutes[key];
			urlArray = currentRoute.split('.');
			currentRoute = urlArray[0];
			if(url.indexOf(currentRoute) !== null && url.indexOf(currentRoute) != -1){
				if(currentRoute.length > length){
					length = currentRoute.length;
					hash = key;
				}
			}
		}
		if(hash != null){
			window.location.hash = "#"+hash;
		}
	},

	loadUrl: function(url){
		InsJs.requestUrl(url, $('#resultLayer'), $('#resultLayer'), function(){
			InsBox.bindBoxes();
			if(!InsBox.dialogVisible){
				InsBox.showDialog();
				InsBox.dialogVisible = true;
			}
			InsBox.updateHash(url);
		});
	},

	showContentsOf: function(what){
		$('#resultLayer').html($(what).html());
		InsBox.showDialog();
		return false;
	},

	showDialog: function(){
		$("#insboxBackground").remove();
		$("#insboxFront").remove();

		$("body").append('<div id="insboxBackground"></div>');
		$("body").append('<div id="insboxFront"></div>');
	},

	hideDialog: function(){
		$.unblockUI();
		InsBox.dialogVisible = false;
	},

	bindBoxes: function(){
		$('.boxForm').each(function(){
			$(this).bind('submit',function(){
				var url = $(this).attr('action');
				return InsJs.requestForm(this, $('#resultLayer'), $('#resultLayer'), function(){
					InsBox.bindBoxes();
					if(!InsBox.dialogVisible){
						InsBox.showDialog();
						InsBox.dialogVisible = true;
					}
					InsBox.updateHash(url);
				});
			});
		});

		$('.boxLink').each(function(){
			$(this).bind('click',function(){
				InsBox.bindBoxes();
				var url = $(this).attr('href');
				return InsJs.requestUrl(this, $('#resultLayer'), $('#resultLayer'), function(){
					InsBox.bindBoxes();
					if(!InsBox.dialogVisible){
						InsBox.showDialog();
						InsBox.dialogVisible = true;
					}
					InsBox.updateHash(url);
				});
			});
		});
	}
};
*/
