var downFile = {
	theHrefString: 'downfile.php?f=',
	replacesHaldus: {1:/http\:\/\/www\.limestonefunds\.eu\/haldus\//gi , 2:/http\:\/\/limestonefunds\.eu\/haldus\//gi},
	replaces: {1:/http\:\/\/www\.limestonefunds\.eu\//gi , 2:/http\:\/\/limestonefunds\.eu\//gi},
	popupId: 'dFilePop',

	init: function(){
		$("a[href*='"+downFile.theHrefString+"']").each(function(){
			var theloc = $(this).attr('href');
			for(var i in downFile.replacesHaldus){
				theloc = theloc.replace(downFile.replaces[i],'../');
			}
			for(var i in downFile.replaces){
				theloc = theloc.replace(downFile.replaces[i],'');
			}
			$(this).attr('href','javascript:void(0);').click(function(){downFile.pop(theloc,$(this))});
		});
		$(document.body).prepend(downFile.design());
	},
	design: function(){
		return '<div style="display:none; position:absolute; z-index: 100; background:#FFFFFF; border: 1px solid #cccccc; width: 310px; font: 12px/14px Arial; color:#595959;  margin: 0 auto; padding: 15px; " id="'+downFile.popupId+'" ><div style="text-align: right;"><a href="javascript:downFile.close();" style="text-decoration: none;color: #595959;">X</a></div><div class="dfContent"></div></div>';
	},
	
	pop: function(addr,el){
		
		var offs = el.offset();
		var left = offs.left+el.width()+10;
		if(left+$('#'+downFile.popupId).width() > $(window).width()-35){
			left = $(window).width() - $('#'+downFile.popupId).width() -35;
		}
		$('#'+downFile.popupId).show().css({'top': offs.top+'px', 'left': left+'px'});
		$('#'+downFile.popupId+' .dfContent').html('Please Wait...');
		$.get(addr, function(data){
			$('#'+downFile.popupId+' .dfContent').html(data);
		});

		
	},
	
	close: function(){
		$('#'+downFile.popupId).hide();
	},
	
	submit: function(){
		if(downFile.check()){
			$('#downFileForm').submit();
		}
	},
	
	check: function(){
		if($('#dfName').val() != '' && $('#dfCompany').val() != '' && $('#dfEmail').val().indexOf('@') > -1){
			return true;
		} else {
			alert('"Name", "Email" and "Company name" must be filled correctly!');
			return false;
		}
	}
	
	
}

$(document).ready(function(){
	downFile.init();
});

