/*
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(){
 
 if(jQuery('#popup3').size()>0)jQuery('#popup3').remove();
 width = jQuery(document).width();
 height = jQuery(document).height();
 var top = jQuery(window).scrollTop()+jQuery(window).height()/3;
 
 html = ''
 +'<div id="popup3" class="popup3 popup" style="width:'+width+'px;height:'+height+'px;"><div class="popup3-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);
 
 }
init_loader3 = init_loader;
function exit_loader(){
 jQuery('#popup3').remove();
 }
exit_loader3 = exit_loader;

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(); }
  });
 }

/*######################################################################*/
/*POPUP1 - FIXED CENTER*/
function popup1(content,width){
 var width = (typeof(width)=='undefined' ? 605 : width );
 if(typeof(content)=='string'){ innerhtml = content; }
 else if(is_elem(content)){ innerhtml = content.innerHTML; }
 
 if(jQuery('#popup1').size()>0)jQuery('#popup1').remove();//ALLOW ONLY ONE popup1 AT A TIME
 
 html = ''
 +'<div id="popup1" class="popup1 popup"><div class="popup1-bgnd"></div><table class="popup1-table"><tr><td>'
 +'<div id="popup1-inner" style="width:'+width+'px;">'
 +'<div class=\'popup-close\' onclick=\'popup1_close();\'></div>'
 +innerhtml
 +'</div></td></tr></table></div>';
 jQuery(document.body).prepend(html);
 
 if(jQuery.browser.msie&&jQuery.browser.version.substr(0,1)<7)jQuery(window).bind('scroll',ie6_popup1_onscroll);
 }

function popup1_close(){ jQuery('#popup1').remove(); }

function ie6_popup1_onscroll(event){
 if(jQuery('#popup1').size()<1){jQuery(window).unbind('scroll',ie6_popup1_onscroll);}
 else{document.getElementById('popup1').style.top = document.body.parentElement.scrollTop;}
 }

/*######################################################################*/
/*NEW! POPUP3 - FIT VERT/HORIZ CENTER*/
function popup3(innerhtml,innerw){
 var innerw = (typeof(innerw)=='undefined' ? 590 : innerw ); 
 
 if(jQuery('#popup3').size()>0)jQuery('#popup3').remove();//ALLOW ONLY ONE POPUP AT A TIME
 width = jQuery(document).width(); height = jQuery(document).height();
 
 html = ''
 +'<div id="popup3" class="popup3 popup" style="width:'+width+'px;height:'+height+'px;"><div class="popup3-bgnd"></div>'
 +'<div id="popup3-inner" style="width:'+innerw+'px;visibility:hidden;top:'+jQuery(window).scrollTop()+'px;"><div class="popup3-inner-bgnd"></div>'
 +'<div class=\'popup-close\' onclick=\'popup3_close();\'></div>'
 +innerhtml
 +'</div></div>';
 jQuery(document.body).append(html);
 jQuery('#popup3-inner').css({'visibility':'visible'});
 jQuery(window).bind('resize',popup3_resize).bind('scroll',popup3_scroll);
 //FOLLOWING TOP CALC FAILS IN IE (NOT IMPLEMENTED)
 //var top = ( totalHeight(jQuery('#popup3-inner')) < jQuery(window).height()
 //? jQuery(window).scrollTop()+jQuery(window).height()/2-totalHeight(jQuery('#popup3-inner'))/2
 //: jQuery(window).scrollTop() );
 var top = jQuery(window).scrollTop()+50;
 jQuery('#popup3-inner').css({'top':top+'px'});
 }
function popup3_close(){
 jQuery('#popup3').remove();
 jQuery(window).unbind('resize',popup3_resize).unbind('scroll',popup3_scroll);
 }
function popup3_resize(event){
 jQuery('#popup3')
 .css({'width':100+'%','height':100+'%'})
 .css({'width':jQuery(document).width()+'px','height':jQuery(document).height()+'px'});
 //var top = ( totalHeight(jQuery('#popup3-inner')) < jQuery(window).height()
 //? jQuery(window).scrollTop()+jQuery(window).height()/2-totalHeight(jQuery('#popup3-inner'))/2
 //: jQuery(window).scrollTop() );
 var top = jQuery(window).scrollTop()+50;
 jQuery('#popup3-inner').css({'top':top+'px'});
 }
function popup3_scroll(event){
 jQuery('#popup3').css({'width':100+'%','height':100+'%'}).css({'width':jQuery(document).width()+'px','height':jQuery(document).height()+'px'});
 }

/*######################################################################*/
/*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 popup">'
 +'<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;}
 }

/*######################################################################*/
/*POPUP FLV PLAYER*/

function popupflv(src){
 
 var w = 600; var h = 400;
 
 popup3(''
 +'<div id="popup-flv-player" style="width:'+w+'px;height:'+h+'px;">'
 +'<a href="http://get.adobe.com/flashplayer/" style="background:transparent url(\'gfx/flash-required.gif\') no-repeat center center scroll;width:100%;height:100%;display:block;"></a>'
 +'</div>',w);
 jQuery('#popup3-inner').css({'background-color':'#000000','border':'1px solid #555555'});

 embedflv('popup-flv-player',src,w,h);
 
 }

function insertflv(src,w,h){

 var id='REF_'+rand_string(); while(jQuery('#'+id).size()>0) id='REF_'+rand_string();
 document.write('<span id="'+id+'">' 
 +'<a href="http://get.adobe.com/flashplayer/" style="background:transparent url(\'gfx/flash-required.gif\') no-repeat center center scroll; width:527px; height:164px; display:block;"></a>'
 +'</span>');

 var flashvars = {};
 flashvars.videoPath = src;
 flashvars.imagePath = src+".jpg";
 flashvars.stageW = w;
 flashvars.stageH = h;
 
 flashvars.autoStart = "false";
 flashvars.autoHide = "true";
 flashvars.autoHideTime = "3";
 flashvars.hideLogo = "false";
 flashvars.volAudio = "100";
 flashvars.disableMiddleButton = "false";
 flashvars.playSounds = "false";
 
 flashvars.soundBarColor = "0x666666";
 flashvars.barColor = "0x666666";
 flashvars.barShadowColor = "0xaaaaaa";
 flashvars.subbarColor = "0xffffff";
 
 flashvars.extendVideo = "true";

 var params = {};
 params.bgcolor = "#000000";
 params.wmode = "transparent";
 params.allowFullScreen = "true";
 //params.allowScriptAccess = "sameDomain";
 
 var attributes = {};

 swfobject.embedSWF("flvPlayer.swf", id, w, h, "9.0.0", "", flashvars, params, attributes);
 
 };

function embedflv(id,src,w,h){

 var flashvars = {};
 flashvars.videoPath = src;
 flashvars.imagePath = src+".jpg";
 flashvars.stageW = w;
 flashvars.stageH = h;
 
 flashvars.autoStart = "true";
 flashvars.autoHide = "true";
 flashvars.autoHideTime = "3";
 flashvars.hideLogo = "false";
 flashvars.volAudio = "100";
 flashvars.disableMiddleButton = "false";
 flashvars.playSounds = "false";
 
 flashvars.soundBarColor = "0x666666";
 flashvars.barColor = "0x666666";
 flashvars.barShadowColor = "0xaaaaaa";
 flashvars.subbarColor = "0xffffff";
 
 flashvars.extendVideo = "true";

 var params = {};
 params.bgcolor = "#000000";
 //params.wmode = "transparent";
 params.allowFullScreen = "true";
 //params.allowScriptAccess = "sameDomain";
 
 var attributes = {};

 swfobject.embedSWF("flvPlayer.swf", id, w, h, "9.0.0", "", flashvars, params, attributes);

 }
