/*
OBLONGTECH JAVASCRIPT POPUP CODE VERSION3 UPDATED OCT2010
REQUIRES =>JQUERY 1.4.2 + OBLONGTECH-CORE-3
JQUERYUI REQUIRED FOR POPUP2

EDITED BY Dustin 2010112919322
*/

function init_loader(){
 
 width = jQuery(document).width();
 height = jQuery(document).height();
 var top = jQuery(window).scrollTop()+jQuery(window).height()/3;
 
 html = ''
 +'<div id="popup-loader" class="popup-wrap" style="width:'+width+'px;height:'+height+'px;"><div class="popup-bgnd"></div>'
 +'<div style="text-align:center;position:relative;top:'+top+'px;"><img src="css/ajax-loader.gif" style="margin:0px auto;"/></div>'
 +'</div>';
 jQuery(document.body).append(html);
 
 }
function exit_loader(){
 jQuery('#popup-loader').remove();
 }

function totalWidth(j){ return j.width()+parseInt(j.css('padding-left'),10)+parseInt(j.css('padding-right'),10); }
function totalHeight(j){ return j.height()+parseInt(j.css('padding-top'),10)+parseInt(j.css('padding-bottom'),10); }

/*######################################################################*/
/*CREATE ELEMENT HIGHLIGHTER*/
function targeter(e){
 jQuery('#target-label,#target-top,#target-left,#target-bottom,#target-right').remove();
 d = 2;
 
 j = jQuery(e);
 w = j.width()+parseInt(j.css('padding-left'),10)+parseInt(j.css('padding-right'),10);
 h = j.height()+parseInt(j.css('padding-top'),10)+parseInt(j.css('padding-bottom'),10);
 offset = j.offset(); x = offset.left; y = offset.top;
 
 jQuery('body').append(''
 +'<div id="target-label">&nbsp;EDIT&nbsp;</div>'
 +'<div id="target-top"></div>'
 +'<div id="target-right"></div>'
 +'<div id="target-bottom"></div>'
 +'<div id="target-left"></div>');
 
 jQuery('#target-top,#target-bottom').css({'width':w+2*d+'px','height':d+'px','top':y-d+'px','left':x-d+'px'});
 jQuery('#target-right,#target-left').css({'width':d+'px','height':h+2*d+'px','top':y-d+'px','left':x-d+'px'});
 jQuery('#target-right').css({'left':x+w+'px'});
 jQuery('#target-bottom').css({'top':y+h+'px'});
 
 jQuery('#target-label').css({'top':y+'px','left':(x+w-totalWidth(jQuery('#target-label')))+'px'})
 .bind('click',function(){
		data = jQuery(jQuery(this).data('linked_elem')).data();
		
		jQuery.get(data['cms_url'],data['send_data'],function(data){
			remove_targeter();
			popup3(
			'<a class=\'popup-close\' onclick=\'popup1_close();\' title=\'Click here to cancel changes and close this popup.\'></a>'
			+data);
   });
  
  }).data('linked_elem',e);
  
jQuery(document).bind('mouseover',{
 j:jQuery('.spotlight')//JQUERY ELEMS
,x:jQuery('#target-label').get()//EXCLUDE ARRAY
,a:function(event,elem){//ACTION

 var link = jQuery('#target-label').data('linked_elem');
 if(link==elem){ remove_targeter(); }

 }},mass_level_event);

 }
 
function remove_targeter(){
 jQuery(document).unbind('mouseover',mass_level_event);
 jQuery('#target-label,#target-top,#target-left,#target-bottom,#target-right').remove();
 }
function mass_level_event(event){
 event.data.j.each(function(i){
  for(var i=0;i<event.data.x.length;i++){ if(jQuery.contains(event.data.x[i],event.target)||event.data.x[i]==event.target){ cancel = true; } }
  if(!jQuery.contains(this,event.target)&&this!=event.target){ event.data.a(event,this); }
  //else{ event.stopPropagation(); }
  });
 }

/*######################################################################*/
/*NEW! POPUP4 - FIT VERT/HORIZ CENTER*/
function popup4(innerhtml,innerw,bool){
 var classname = ( typeof(innerw)=='string' ? ' '+innerw : '' );
 var innerw = ( typeof(innerw)=='number' ? 'width:'+innerw+'px;' : ( typeof(innerw)=='undefined' ? 'width:580px;' : '' ) ); 
 var bool = ( typeof(bool)=='undefined' ? true : bool );
 
 exit_loader();
 if(bool&&jQuery('#popup4').size()>0)jQuery('#popup4').remove();//ALLOW ONLY ONE POPUP AT A TIME
 
 width = jQuery(document).width(); height = jQuery(document).height();
 html = ''
 +'<div id="popup4" class="popup-wrap" style="width:'+width+'px;height:'+height+'px;"><div class="popup-bgnd"></div>'
 +'<div class="popup'+classname+'" style="'+innerw+'visibility:hidden;top:'+jQuery(window).scrollTop()+'px;"><div class="popup-inner-bgnd"></div>'
 +'<div class=\'popup-close\' onclick=\'popup4_close(this);\'></div>'
 +innerhtml
 +'</div></div>';
 jQuery(document.body).append(html);
 jQuery('#popup4 .popup').css({'visibility':'visible'});
 jQuery(window).bind('resize',popup4_resize).bind('scroll',popup4_scroll);
 var top = jQuery(window).scrollTop()+50;
 jQuery('#popup4 .popup').css({'top':top+'px'});
 }
function popup4_close(e){
 jQuery(e).parents('.popup-wrap').remove();
 jQuery(window).unbind('resize',popup4_resize).unbind('scroll',popup4_scroll);
 }
function popup4_resize(event){
 jQuery('#popup4')
 .css({'width':100+'%','height':100+'%'})
 .css({'width':jQuery(document).width()+'px','height':jQuery(document).height()+'px'});
 var top = jQuery(window).scrollTop()+50;
 jQuery('#popup4 .popup').css({'top':top+'px'});
 }
function popup4_scroll(event){
 jQuery('#popup4')
 .css({'width':100+'%','height':100+'%'})
 .css({'width':jQuery(document).width()+'px','height':jQuery(document).height()+'px'});
 }
/*######################################################################*/
/*NEW! POPUP3 - FIT VERT/HORIZ CENTER - NOT UNIQUE*/
function popup1(innerhtml,innerw){
 popup4(innerhtml,innerw,false);
 }
function popup3(innerhtml,innerw){
 popup4(innerhtml,innerw,false);
 }

/*######################################################################*/
/*POPUP2 - MULTIPLE DRAGGABLE RESIZABLE - REQUIRES JQUERYUI 1.7.2*/
function popup2(html,id){
 if(jQuery('#popup2').size()<1){
  jQuery('body').prepend(''
  +'<div id="popup2"><div class="popup2-bgnd"></div>'
  +'<div href="javascript:;" title="Click to close all popups" id="popup2-close-all"'
  +' onclick="jQuery(\'#popup2\').css({\'display\':\'none\'})"></div></div>');
  if(jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7){ jQuery(window).unbind('scroll',ie6_popup2_onscroll);}
  }
 if(jQuery('#popup2>#'+id).size()>0){
  jQuery('#'+id+'>*:not(.popup-close,.popup2-inner-bgnd,.ui-drag-handle,.ui-resizable-handle)').remove();
  jQuery('#'+id).append(html).trigger('mousedown',[jQuery('#'+id).get(0)]);
  }
 else{
 jQuery('#popup2').append(
 '<div id="'+id+'" class="popup2">'
 +'<div class="popup2-inner-bgnd"></div>'
 +'<a href="javascript:;" title="Click here and drag mouse to move this popup, release click to drop." class="ui-drag-handle"></a>'
 +'<a href="javascript:;" onclick="close_popup2(\''+id+'\');" title="Click here to close this popup." class="popup-close"></a>'
 +'</div>'
 );
 jQuery('#'+id).eq(0)
.draggable({ scroll:false, snap:false, snapMode:'outer', snapTolerance:40, containment:'document', handle:'.ui-drag-handle'
  ,start:function(event,ui){ }
  ,stop:function(event,ui){ }
  ,drag:function(event,ui){
   if(ui.position.left>=jQuery(window).width()-ui.helper.width()-12){
    ui.position.left = jQuery(window).width()-ui.helper.width()-12;
    };
   if(ui.position.left<=12){
    ui.position.left = 12;
    if(ui.position.left+ui.helper.width()>=jQuery(window).width()-12){
     ui.helper.width(jQuery(window).width()-ui.position.left-12);
     };    
    }
   if(ui.position.top>=jQuery(window).height()-totalHeight(ui.helper)-12){
    ui.position.top = jQuery(window).height()-ui.helper.height()-totalHeight(ui.helper)+ui.helper.height()-12;
    };
   if(ui.position.top<=12){
    ui.position.top = 12;
    if(ui.position.top+totalHeight(ui.helper)>=jQuery(window).height()-12){
     ui.helper.height(jQuery(window).height()-ui.position.top-totalHeight(ui.helper)+ui.helper.height()-12);
     //RESIZE RTE
     //jQuery('.mceIframeContainer iframe',ui.helper).css({'height':0});
     //jQuery('.mceIframeContainer iframe',ui.helper).css({'height':parseInt(ui.helper.height())-parseInt(jQuery('form.item',ui.helper).height())});
     };
    }
   }
  })
  .resizable({ containment:'document', minWidth:300, minHeight:200,
   start:function(event,ui){ jQuery('.draggable-bgnd',ui.helper).css({'z-index':10}); },
   stop:function(event,ui){ jQuery('.draggable-bgnd',ui.helper).css({'z-index':-1}); },
   resize:function(event,ui){
    if(ui.position.left+ui.helper.width()>=jQuery(window).width()-12){
     ui.helper.width(jQuery(window).width()-ui.position.left-12);
     };
    if(ui.position.top+totalHeight(ui.helper)>=jQuery(window).height()-12){
     ui.helper.height(jQuery(window).height()-ui.position.top-totalHeight(ui.helper)+ui.helper.height()-12);
     };
    //RESIZE RTE
    //jQuery('.mceIframeContainer iframe',ui.helper).css({'height':0});
    //jQuery('.mceIframeContainer iframe',ui.helper).css({'height':parseInt(ui.helper.height())-parseInt(jQuery('form.item',ui.helper).height())});
    }
   })
  .append('<div class="popup2-inner">'+html+'</div>')
  .bind('mousedown',function(event,t){
   event = ( window.event || event );
   var target = ( t || event.target || event.srcElement );
			jQuery('.popup2').each(function(i){
			 if(this==target||jQuery.contains(this,target)){
     jQuery(this).removeClass('popup2-unselected');
     if(jQuery('.popup2').index(jQuery(this))<jQuery('.popup2').size()-1)jQuery('#popup2').append(jQuery(this));
     }
    else{ jQuery(this).addClass('popup2-unselected'); }
				});
   })
  .trigger('mousedown',[jQuery('#'+id).get(0)])
  ;
  }
 }

function close_popup2(id){
 jQuery('#'+id).remove();
 jQuery('#popup2>.popup2').eq(jQuery('#popup2>.popup2').size()-1).removeClass('popup2-unselected');
 if(jQuery('#popup2>.popup2').size()<1) jQuery('#popup2').remove();
 }

function ie6_popup2_onscroll(event){
 if(jQuery('.popup2').size()<1){jQuery(window).unbind('scroll',ie6_popup2_onscroll);}
 else{document.getElementById('popup2').style.top = document.body.parentElement.scrollTop;}
 }

