//
//uw_picfuncs.js
//Written by Daniel666 2008
//

var ScreenWidth=0;
var ScreenHeight=0;
var ActPicObject;
var BGWidth=0;
var BGHeight=0;
var InitialWidth = 2;
var InitialHeight = 2;
var PrevNextFuncPtr = null;
var PicShown = false;
var ShowNext = false;
var ShowPrev = false;
var BlendInfoOut = false;

var SpeedMS = 10;
var GrowStep = 30;

function uwPicture (PicPath,PicWidth,PicHeight,PicName,PicMaterial,PicSize,PicYear,PicSold)
{
 this.PicPath=PicPath;
 this.PicWidth=PicWidth;
 this.PicHeight=PicHeight;
 this.PicName=PicName;
 this.PicMaterial=PicMaterial;
 this.PicSize=PicSize;
 this.PicYear=PicYear;
 this.PicSold=PicSold;
return this;
}

function uwShowPicture (PicObject,FuncPtr)
{
var Obj;
 PicShown=false;
 PrevNextFuncPtr = FuncPtr;
 ScreenWidth = parseInt(GetScreenSize(1));
 ScreenHeight = parseInt(GetScreenSize(2));
 ActPicObject = PicObject;
 document.getElementById("img_detail").style.left = 40;
 document.getElementById("img_detail").style.top = 40;
 BGWidth = PicObject.PicWidth + 80;
 BGHeight = PicObject.PicHeight + 140;
 CorrectHeightWidth();
 document.getElementById("overlay_box").style.visibility="visible";
 document.getElementById("img_detail").style.visibility="hidden";
 document.getElementById("close_button").style.visibility="hidden";
 document.getElementById("status_loading").style.visibility="hidden";
 document.getElementById("next_prev").style.visibility="hidden";
 document.getElementById("prev_but").style.visibility="hidden";
 document.getElementById("next_but").style.visibility="hidden";
 document.getElementById("info_text").style.visibility="hidden";

 Obj = PrevNextFuncPtr(1,PicObject);
 if (Obj!=null) ShowPrev = true;
 else ShowPrev=false;
 Obj = PrevNextFuncPtr(2,PicObject);
 if (Obj!=null) ShowNext=true;
 else ShowNext=false;

 document.getElementById("image_box").style.width=InitialWidth;
 document.getElementById("image_box").style.height=InitialHeight;
 document.getElementById("image_box").style.left = parseInt(((ScreenWidth-InitialWidth)/2));
 document.getElementById("image_box").style.top = parseInt(((ScreenHeight-InitialHeight)/2));
 document.getElementById("image_box").style.visibility="visible";
 window.setTimeout("uwGrow()",SpeedMS);
}

function CorrectHeightWidth ()
{
var hs;
var hp;
 if (BGWidth>ScreenWidth)
 {
    hp = 40-(parseInt((BGWidth-ScreenWidth)/2));
    if (hp<0)
    {
       hp = 0;
       BGWidth = ActPicObject.PicWidth;
    }
    else BGWidth = ScreenWidth;
    document.getElementById("img_detail").style.left = hp;
 }
 if (BGHeight>ScreenHeight)
 {
    hp = 40-(parseInt((BGHeight-ScreenHeight)/2));
    if (hp<0)
    {
       hp=0;
       if (ActPicObject.PicHeight<ScreenHeight)
       {
          BGHeight = ActPicObject.PicHeight+60;
          if (BGHeight>ScreenHeight) BGHeight-=(BGHeight-ScreenHeight);
       }
       else BGHeight = ScreenHeight;             
    }
    else BGHeight = ScreenHeight;
    document.getElementById("img_detail").style.top = hp;
 }
}

function uwGrow ()
{
var w;
var h;
var growing = true;
 w=parseInt(document.getElementById("image_box").style.width);
 h=parseInt(document.getElementById("image_box").style.height);
 if (w<BGWidth)
 {
    if (w+GrowStep>BGWidth) w = BGWidth;
    else w+=GrowStep;
    document.getElementById("image_box").style.width=w;
 }
 else if (h<BGHeight)
 {
    if (h+GrowStep>BGHeight) h = BGHeight;
    else h+=GrowStep;
    document.getElementById("image_box").style.height=h;
 }
 else growing = false;
 
 document.getElementById("image_box").style.left = parseInt(((ScreenWidth-w)/2));
 document.getElementById("image_box").style.top = parseInt(((ScreenHeight-h)/2));

 if (growing==true) window.setTimeout("uwGrow()",SpeedMS);
 else
 {
    document.getElementById("img_detail").src = ActPicObject.PicPath;
    document.getElementById("img_detail").alt = ActPicObject.PicName;
    document.getElementById("status_loading").style.width=ActPicObject.PicWidth;
    document.getElementById("status_loading").style.height=ActPicObject.PicHeight;
    document.getElementById("status_loading").style.left=document.getElementById("img_detail").style.left;
    document.getElementById("status_loading").style.top=document.getElementById("img_detail").style.top;

    document.getElementById("status_loading").style.visibility="visible";
    window.setTimeout("uwCheckPictureLoaded()",1000);
 }
}

function uwCheckPictureLoaded ()
{
 if (document.getElementById("img_detail").complete==true) uwPresentPicture();
 else window.setTimeout("uwCheckPictureLoaded()",1000);
}

function uwPresentPicture ()
{
var t = (parseInt(document.getElementById("img_detail").style.top)) + ActPicObject.PicHeight;
 if (t>BGHeight) 
 {
    t = BGHeight-60;
    BlendInfoOut = true;
 }
 else  BlendInfoOut = false;
 document.getElementById("info_text").style.top = t;
 document.getElementById("info_text").style.width = ActPicObject.PicWidth;
 document.getElementById("info_text_name").firstChild.nodeValue = PrepareString(ActPicObject.PicName);
 document.getElementById("info_text_sold").firstChild.nodeValue = ActPicObject.PicSold;
 document.getElementById("info_text_infos").firstChild.nodeValue = PrepareString(ActPicObject.PicMaterial + ", " + ActPicObject.PicSize);
 document.getElementById("info_text_year").firstChild.nodeValue = ActPicObject.PicYear;

 document.getElementById("info_text").style.visibility="visible";
 document.getElementById("status_loading").style.visibility="hidden";
 document.getElementById("img_detail").style.visibility="visible";
 document.getElementById("close_button").style.visibility="visible";
 PicShown = true;
}

function PrepareString (String)
{
 String = String.replace(/&quot;/g, "\"");
 return String;
}

function uwClosePicture ()
{ 
 document.getElementById("close_button").style.visibility="hidden";
 document.getElementById("status_loading").style.visibility="hidden"; 
 document.getElementById("next_prev").style.visibility="hidden";
 document.getElementById("img_detail").style.visibility="hidden";
 document.getElementById("overlay_box").style.visibility="hidden";
 document.getElementById("image_box").style.visibility="hidden";
 document.getElementById("prev_but").style.visibility="hidden";
 document.getElementById("next_but").style.visibility="hidden";
 document.getElementById("info_text").style.visibility="hidden";
 PicShown = false;
}

function uwNextPrevPicture (Flag)
{
var w;
var h;
var Obj = PrevNextFuncPtr(Flag,ActPicObject);
 if (Obj!=null)
 {
    PicShown = false;
    document.getElementById("img_detail").style.visibility="hidden";
    document.getElementById("img_detail").style.left = 40;
    document.getElementById("img_detail").style.top = 40;
    document.getElementById("next_prev").style.visibility="hidden";
    document.getElementById("prev_but").style.visibility="hidden";
    document.getElementById("next_but").style.visibility="hidden";
    document.getElementById("info_text").style.visibility="hidden";
    ActPicObject = Obj;
    w=parseInt(document.getElementById("image_box").style.width);
    h=parseInt(document.getElementById("image_box").style.height);
    BGWidth = Obj.PicWidth + 80;
    BGHeight = Obj.PicHeight + 140;
    CorrectHeightWidth();
    if (w>BGWidth) w = BGWidth;
    if (h>BGHeight) h = BGHeight;
    document.getElementById("image_box").style.width=w;
    document.getElementById("image_box").style.height=h;
    document.getElementById("image_box").style.left = parseInt(((ScreenWidth-w)/2));
    document.getElementById("image_box").style.top = parseInt(((ScreenHeight-h)/2));
    document.getElementById("close_button").style.visibility="hidden";

    Obj = PrevNextFuncPtr(1,ActPicObject);
    if (Obj!=null) ShowPrev = true;
    else ShowPrev=false;
    Obj = PrevNextFuncPtr(2,ActPicObject);
    if (Obj!=null) ShowNext=true;
    else ShowNext=false;

    window.setTimeout("uwGrow()",SpeedMS);
 }
}

function uwShowNextPrev (Flag)
{
 if (PicShown==true)
 {
    if (Flag==1)
    {
       document.getElementById("next_prev").style.visibility="visible";
       if (ShowPrev==true) document.getElementById("prev_but").style.visibility="visible";
       else document.getElementById("prev_but").style.visibility="hidden";
       if (ShowNext==true) document.getElementById("next_but").style.visibility="visible";
       else document.getElementById("next_but").style.visibility="hidden";
       if (BlendInfoOut==true)
       {
          document.getElementById("close_button").style.visibility="visible";
          document.getElementById("info_text").style.visibility="visible";
       }
    }
    else
    {
       document.getElementById("next_prev").style.visibility="hidden";
       document.getElementById("prev_but").style.visibility="hidden";
       document.getElementById("next_but").style.visibility="hidden";
       if (BlendInfoOut==true)
       {
          document.getElementById("close_button").style.visibility="hidden";
          document.getElementById("info_text").style.visibility="hidden";
       }
    }    
 }
}
