/*
OBLONGTECH JAVASCRIPT POPUP CODE VERSION4 UPDATED AUG2011
REQUIRES =>JQUERY 1.6.2 + OBLONGTECH-CORE-3
JQUERYUI REQUIRED FOR POPUP2

EDITED BY Dustin 2011-08-19 15:40
*/

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); }

jQuery.fn.extend({
 left:function(){ return this.position().left; }
,top:function(){ return this.position().top; }
//,width:function(){ return this.width()+parseInt(this.css('padding-left'),10)+parseInt(this.css('padding-right'),10); }
//,height:function(){ return this.height()+parseInt(this.css('padding-top'),10)+parseInt(this.css('padding-bottom'),10); }
 });

/*######################################################################*/
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();
 }

/*######################################################################*/
/*NEW! POPUP4 - FIT VERT/HORIZ CENTER*/
function popup4(innerhtml,w,bool){
 var innerw = (typeof(w)=='number' ? 'width:'+w+'px;' : (typeof(w)=='string'? false : 'width:800px;' ) );
 var bool = (typeof(bool)=='undefined' ? true : bool );
 var classname = (!innerw&&typeof(w)=='string' ? ' '+w : '' );
 
 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'+classname+'" style="width:'+width+'px;height:'+height+'px;"><div class="popup-bgnd"></div>'
 +'<div class="popup" 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()+100;
 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()+100;
 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'});
 }

/*######################################################################*/
/*BACKWARDS COMPATABILITY*/
function popup1(innerhtml,innerw){
 popup4(innerhtml,innerw,false);
 }
function popup1_close(){
 jQuery('#popup1-inner').parents('.popup-wrap').remove();
 jQuery(window).unbind('resize',popup4_resize).unbind('scroll',popup4_scroll);
 }
function popup1_recalc(){
 jQuery('#popup4')
 .css({'width':100+'%','height':100+'%'})
 .css({'width':jQuery(document).width()+'px','height':jQuery(document).height()+'px'});
 var top = jQuery(window).scrollTop()+100;
 jQuery('#popup4 .popup').css({'top':top+'px'});
 }

popup3 = popup4;

/*######################################################################*/
/*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();
		init_loader();
		jQuery.get(data['cms_url'],data['send_data'],function(data){
			remove_targeter();
			popup4(data,500);
   }); 
  
  }).data('linked_elem',e);

jQuery(document).bind('mouseover',{
 j:jQuery(e)//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){
  var cancel = false;
  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(!cancel&&!jQuery.contains(this,event.target)&&this!=event.target){ event.data.a(event,this); }
  //else{ event.stopPropagation(); }
  });
 }

