var SB_Links,SB_Veil,SB_LoaderImg,SB_Box,SB_Group,SB_Target;

$(document).ready(function(){ 
	imgVeil = new Image();
	imgVeil.src = "/veil.png";
	imgLoader = new Image();
	imgLoader.src = "/ajax-loader.gif";

  SB_Veil=$("<img src='/files/graphic/veil.png' alt='' />").css({
    position: 'absolute',
    top: '0px',
    left: '0px',
    zIndex: '95',
    display: 'none'
  }).click(function(e){
    //e.preventDefault();
    SB_Veil.hide();
    SB_LoaderImg.hide();
    SB_Box.hide();
  });	 
  $(document.body).append(SB_Veil);
  
  SB_LoaderImg=$("<img src='/files/graphic/ajax-loader.gif' alt='' />").css({
    position: 'absolute',
    top: '0px',
    left: '50%',
    zIndex: '96',
    display: 'none'
  }).appendTo($(document.body));
  SB_LoaderImg.css('margin','-'+Math.round(SB_LoaderImg.height()/2)+'px 0px 0px -'+Math.round(SB_LoaderImg.width()/2)+'px');
  
  SB_Box=$("<div><div id='SB_BoxTitle'></div><div id='SB_BoxContent'></div><div id='SB_BoxCaption'></div></div>").css({
    position: 'absolute',
    top: '0px',
    left: '50%',
    zIndex: '97',
    display: 'none',
    border: '2px white solid',
    overflow: 'hidden'
  }).appendTo($(document.body));
	SB_Apply();
});


function SB_Apply() {
  SB_Links=$('a.smartbox, area.smartbox, input.smartbox');
  SB_Links.bind("click", function(e){SB_onClick(e);});
}

function SB_Reload() {
  SB_Links.unbind("click", function(e){SB_onClick(e);});
  SB_Apply();
}

function SB_onClick(e) {
  e.preventDefault();
  SB_Target=$(e.target).closest("a[class=smartbox], area[class=smartbox]");
	var group = SB_Target.get(0).rel;
  SB_Group=(group)?SB_Links.filter("a[rel="+group+"], area[rel="+group+"]"):false;
  
  SB_Veil.css({ // Display veil
    width: $(document).width()+'px',
    height: $(document).height()+'px'
  }).fadeIn("fast",function(e){
    if (SB_Veil.css("display")=='block'||SB_Veil.css("display")=='inline') {
      SB_LoaderImg.css({ // OnfadeIn display Loader Image
        top: ($(document).scrollTop()+Math.round($(window).height()/2))+'px'
      }).show();
      SB_StartLoading();
    }
  });
}

function SB_LoadContent(c) {
  SB_Veil.css({ // Display veil
    width: $(document).width()+'px',
    height: $(document).height()+'px'
  }).fadeIn("fast",function(e){
    if (SB_Veil.css("display")=='block'||SB_Veil.css("display")=='inline') {
      SB_LoaderImg.css({ // OnfadeIn display Loader Image
        top: ($(document).scrollTop()+Math.round($(window).height()/2))+'px'
      }).show();
      var sbimgdiv=SB_Box.find("#SB_BoxContent");
      var sbimg=$(c);
      var border=2;
      SB_Box.css({
        top: '170px',
        width: sbimg.css("width"),
        height: sbimg.css("height"),
        border: '2px black solid',
        margin: '0px 0px 0px -'+Math.round(parseInt(sbimg.css("width"))/2-border-30)+'px',
        background: 'white'
      }).empty().append(sbimg.html()).show();
    }
  });
}

function SB_StartLoading() {
  var sbimgdiv=SB_Box.find("#SB_BoxContent");
  var width=500;
  var height=440;
  var padding=10;
  sbimg=$("#"+SB_Target.get(0).rel).html();
  sbimgdiv.html(sbimg);
  if (SB_Veil.css("display")=='block'||SB_Veil.css("display")=='inline') {
    var border=parseInt(SB_Box.css("border"));
    SB_Box.css({
      top: ($(document).scrollTop()+Math.round($(window).height()/2))+'px',
      width: width,
      height: height,
      background: 'white',
      padding: padding,
      border: '2px white solid',
      margin: '-'+Math.round(height/2-border-padding)+'px 0px 0px -'+Math.round(width/2-border-padding)+'px'
    }).fadeIn();
  }
}