var contents=[];
var popupProperty = new Object();
popupProperty.background = '#000000';
popupProperty.id='webguru_popup';
popupProperty.shadow = "2px 2px 10px #000000";
var authentic = "<div style='font-family:Arial; padding-top:10px; color:#333333; font-size:8px; width:100%;'></div>";
var closeButton="<div id='close_bt' style='position:absolute; text-align:center; top:0px; right:0px; padding:5px; font-family:Arial; color:#FFFFFF; width:50px; height:15px; cursor:pointer; opacity:0.6;'>close</div>"
function loadComplete()
{
	var c=0;
   	$('a').each(function(n){
						 
						    if($(this).attr('href').indexOf('webguru_popup@')!=-1)
							{						    
						       $(this).attr('rel',c)
							   contents[c] = ($(this).attr('href').split('webguru_popup@')[1]);
							   $(contents[c]).hide();
							   $(this).bind('click',openPopup);
							   c++;
							}
						    
							
							});
	
	$(window).resize(popupResizing)
}



function applyStyle()
{
	dwx = $(window).width();
	dhx = $(document).height();
	
	
	
	$('#'+popupProperty.id).css('position','absolute');
	$('#'+popupProperty.id).css('top','0px');
	$('#'+popupProperty.id+' #shadowbox').css('position','absolute');
	$('#'+popupProperty.id+' #shadowbox').css('background',popupProperty.background);
	$('#'+popupProperty.id+' #shadowbox').css('width',dwx);
	$('#'+popupProperty.id+' #shadowbox').css('height',dhx);
	$('#'+popupProperty.id+' #shadowbox').css('opacity',0.7);
	$('#'+popupProperty.id+' #shadowbox').css('top',0);
	$('#'+popupProperty.id+' #shadowbox').css('left',0);
	
	$('#'+popupProperty.id+' #popup_content').css('position','absolute');
	$('#'+popupProperty.id+' #popup_content').css('background','#FFFFFF')
	$('#'+popupProperty.id+' #popup_content');
	$('#'+popupProperty.id+' #popup_content').css('overflow','hidden');
	$('#'+popupProperty.id+' #popup_content').css('top',0);
	$('#'+popupProperty.id+' #popup_content').css('left',0);
	$('#'+popupProperty.id+' #popup_content').css('-moz-box-shadow',popupProperty.shadow);
	$('#'+popupProperty.id+' #popup_content').css('-webkit-box-shadow',popupProperty.shadow);
	$('#'+popupProperty.id+' #popup_content').html("Loading...");
	
	
}

function createPopup()
{
   if($('#'+popupProperty.id).length)
   {$('#'+popupProperty.id).remove();}
   
   $('body').append('<div style="display:none;" id="'+popupProperty.id+'"><div id="shadowbox"></div><div id="popup_content"></div></div>');
   applyStyle();
	
	
}

function openPopup(e)
{
   //alert(contents[$(this).attr('rel')]);
   e.preventDefault();	
   createPopup();
   W = $(window).width();
   H = $(window).height();
   content = $(contents[$(this).attr('rel')]).html();
   wx = $(contents[$(this).attr('rel')]).width();
   hx = $(contents[$(this).attr('rel')]).height();
   
   content = closeButton+"<div id='"+contents[$(this).attr('rel')]+"'>"+content+"\n\n"+authentic+"</div>"
   //$('#'+popupProperty.id+' #popup_content').html(content)
  
  
 
  var initialProp = {
	  				 width:"10px",
					 height:"10px",
					 left:Math.ceil((W-10)/2)+'px',
					 top: Math.ceil((H-10)/2)+'px'
	  
  					};
  
  
  $('#'+popupProperty.id+' #popup_content').css('width',initialProp.width);

  $('#'+popupProperty.id+' #popup_content').css('height',initialProp.height);
  $('#'+popupProperty.id+' #popup_content').css('left',initialProp.left);
  $('#'+popupProperty.id+' #popup_content').css('top',initialProp.top);
 
 
   $('#'+popupProperty.id+' #popup_content').animate(initialProp,1);
  
   var targetProp = {
	                  width:wx,
					  height:Number(hx+30),
					  left:Number((W-wx)/2),
					  top:Number((H-hx)/2)+$(window).scrollTop(),
   					};
   
$('#popup_content').css('opacity',0);
   $('#'+popupProperty.id+' #popup_content').animate(targetProp,1000,"easeOutCubic",function(){
	 
														$('#'+popupProperty.id+' #popup_content').html(content);
			 var imagetoResize = $('#'+popupProperty.id+' #popup_content').find('table').eq(1).find('img');
			 
			 
			 var adjusthx = 450- 300;
			 if(imagetoResize.width()>450)
			 {
				 $('.popup-bg').animate({width:imagetoResize.width()});
				  $('#popup_content').animate({width:imagetoResize.width()},function(){
													if(parseInt($('#'+popupProperty.id+' #shadowbox').css('width')) < 	imagetoResize.width())
													{
														$('#'+popupProperty.id+' #shadowbox').css('width',imagetoResize.width());
												
													}
																					 
																					 
																					 	});
			 }
			 else
			 {
				  $('#popup_content').animate({width:'450px'});
			 }
			 if(imagetoResize.height()>300)
			 {
				
				  $('#popup_content').animate({height:imagetoResize.height()+adjusthx,opacity:1},function(){
													if(parseInt($('#'+popupProperty.id+' #shadowbox').css('height')) < 	imagetoResize.height())
													{
				$('#'+popupProperty.id+' #shadowbox').css('height',imagetoResize.height()+$(window).scrollTop());
														
													}
																		 
							popupResizing();														 
																					 	});
			 }
			 else
			 {
				
				 				  $('#popup_content').animate({height:"450px",opacity:1});
								  
							popupResizing();	  
								  
								  
			 }
			 
																				$('#close_bt').bind('click',function(){
									   
									   													$('#'+popupProperty.id).hide();
									    
									   
									   											 })
																				
																				});
   
   if((hx+30)>H)
   {
	   dhx = (hx+150);
	  $('#'+popupProperty.id).css('position','absolute'); 
	  $('#'+popupProperty.id+' #shadowbox').css('height',dhx);
   }
   
   
   $('#'+popupProperty.id).fadeIn(700)
}


function popupResizing()
{
  	if($('#'+popupProperty.id).length)
	{
	   W = $(window).width();
	   H = $(window).height();
	   dhx = $(document).height();
	   wx =  $('#'+popupProperty.id+' #popup_content').width();
	   hx = $('#'+popupProperty.id+' #popup_content').height();
	   tgtTop = ((H-hx)/2);
	   if(tgtTop < 0)
	   {
		   tgtTop =0;
	   }
	   $('#'+popupProperty.id+' #popup_content').animate({left:((W-wx)/2)});
	   $('#'+popupProperty.id+' #popup_content').animate({top:tgtTop+$(window).scrollTop()});
	   $('#'+popupProperty.id+' #shadowbox').css('width',W);
	   $('#'+popupProperty.id+' #shadowbox').css('height',dhx);
	   
		
	}
}

$(document).ready(loadComplete);
