
$(function(){

 modal_view = function(id){
  modal.open({url:_root+'view_order?id='+id,title:'View Order',frame_width:760,frame_height:480,padding:true});
  return false;
 }



 modal_signin=function(){
  modal.open({url:_root+'signin?r='+location.href.replace(location.hash,''),title:'Register or Sign in',frame_width:700,frame_height:280,padding:true});
  return false;
 }
 $('a.modal-signin').click(modal_signin);
 if(location.hash=='#signin') modal_signin();

 var modal_wizard=function(){
  modal.open({url:_root+'wizard',title:'Digium Card Selection Wizard',frame_width:520,frame_height:325,padding:false});
  return false;
 }
 $('a.modal-wizard').click(modal_wizard);
 if(location.hash=='#wizard') modal_wizard();

 $('input.text-default').each(function(){
  $(this).attr('default',$(this).attr('rel'));
 }).focus(function(){
  if($(this).val()==$(this).attr('default'))
   $(this).val('');
 }).blur(function(){
  if($(this).val()=='')
   $(this).val($(this).attr('default'));
 });


 var footer_links_width=0;
 $('#footer-links li')
  .each(function(){
   footer_links_width+=$(this).width()+20;
  })
  .filter('.first')
  .css('margin-left',
   ($('#footer-links').width()-footer_links_width)/2
  );
 
 if($('input[name=username]').length) $('input[name=username]').get(0).focus();
 if($('#admin-panel').length) ap();
});

var modal={
 html:'<div id="modal-black"></div><div id="modal-container">'+
  '<div id="modal-tl"></div><div id="modal-t" style="width:%d_width%px;"></div><div id="modal-tr"></div>'+
  '<div id="modal-title"><span>%title%</span> <a href="#" onclick="modal.close();return false;">Close</a></div><iframe src="%url%" width="%frame_width%" height="%frame_height%" frameborder="0" />'+
  '<div id="modal-bl"></div><div id="modal-b" style="width:%d_width%px;"></div><div id="modal-br"></div></div>',
 open:function(o){
  $('#modal-black,#modal-container').remove();
  
  o.width=o.frame_width+(o.padding?14:0);
  o.d_width=o.width-14;
  o.height=o.frame_height+50;
  
  var html=modal.html;
  for(var k in o)
   html=html.replace(new RegExp('%'+k+'%','gi'),o[k]);
  
  $(document.body).append(html);
  $('#modal-black').css({opacity:0.7});
  $('#modal-container').css({
   width:o.width,height:o.height,
   left:($(document.body).width()-o.width)/2,top:($(document.body).height()-o.height)/2
  });
  $('#modal-black,#modal-container').fadeIn(250);
 },
 close:function(){
  $('#modal-black,#modal-container').fadeOut(250,function(){
   $('#modal-black,#modal-container').remove();
  });
 }
};

///////////////////////////////////////////////////////
function ap(){
 var _width=$(document.body).width()-50;
 $('#admin-panel').css({width:_width});
	
	$(window).resize(function(){
	 var _width=$(document.body).width()-50;
		$('#admin-panel').css({width:_width});
	});
}
var popups=[];
function adminpopup(id,a,_h){
	var obj=$('#'+id),_w=$(document.body).width()-250,_h=Math.round($(window).height()*0.5);

	if(popups.length>0 && popups[0]!=id){
		$('#'+popups[0]).hide();
		popups=[];
	}
	if(obj.is(':hidden')){
		obj.css({width:_w,height:_h,left:($(document.body).width()-_w)/2,top:($(window).height()-_h)/2+$(window).scrollTop()}).fadeIn(350);
		popups=[id,a];
	}else{
		$('#'+popups[0]).hide();
		popups=[];
	}

	$(window).scroll(function(){
		if(popups.length>0){
			if($('#'+popups[0]).length)
				$('#'+popups[0]).stop().animate({top:($(window).height()-$('#'+popups[0]).height())/2+$(window).scrollTop()},500);
		}
	});
	return false;
}

