$(document).ready(function() {	

	//select all the a tag with name equal to modal
	$('a[name=modal]').live('click',function(e) {
		fade = 500;
		BoxWidth = 700;
		BoxHeight = 400;
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('YDialog');
		var title = $(this).attr('title');
		var wh = $(this).attr('wh');
		if (wh) {
			wh=wh.split(":");
			BoxWidth = wh[0];
			BoxHeight = wh[1];
		}
		
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(fade);	
		$('#mask').fadeTo("slow",0.8);	

		$('#boxes #dialog').css("width",BoxWidth);
		$('#boxes #dialog').css("height",BoxHeight);
		$('#dvBoxContain').css("width",BoxWidth-50);	
		//$('#dvBoxContain').css("height",BoxHeight-50);
		
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
					              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
		
		//transition effect
			$(id).fadeIn(fade);
		
		$(id).html('<img class="loadingImg" src="images/loading.gif">'); 
		//$(id).html('<div id="dvBoxContain" style="height:' + (BoxHeight-30) + 'px">Simple Modal Window</div><div id="dvBoxTitle">'+title+'</div><div id="dvBoxClose"><a href="#" class="close"><img align="right" src="images/bt_close.png"></a></div>'); return false;

		var target = $(this).attr('href');
		var aTarget = new Array();
		aTarget=target.split(":");
		$.post('callajax.php',
			{mod : aTarget[0],
			metode : aTarget[1] ,
			id : aTarget[2] },
				function(response){
				  //$(id).html('<div id="dvBoxContain" style="height:' + (BoxHeight-40) + 'px">'+response+'</div><div id="dvBoxTitle">'+title+'</div><div id="dvBoxClose"><a href="#" class="close"><img align="right" src="images/bt_close.png"></a></div>');
					$(id).html('<div id="dvBoxClose"><a href="#" class="close">&nbsp;</a></div><div id="dvBoxContain" style="height:' + (BoxHeight-40) + 'px">'+response+'</div><div id="dvBoxTitle">'+title+'</div>');
				  return false;
				}
		);
		window.scrollTo(0,0);
	});
	
	//if close button is clicked
	$('#dvBoxClose').live('click',function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask').fadeOut(fade); $('.window').fadeOut(fade);
		
	});		
	
	//if mask is clicked
	//$('#mask').live('click',function () { $(this).fadeOut(fade); $('.window').fadeOut(fade); });			
	
});

