var h2;
var w2;
var w;
var h;
var minheight=520;
var minwidth=780;
var maxwidth=1997;
var maxheight=1331;
pic1= new Image(500,500);
if((background.width/background.height)>1.2){
    pic1.src="/js/copyright3.png";
}else if((background.width/background.height)<0.8){
    pic1.src="/js/copyright1.png";
}else{
    pic1.src="/js/copyright2.png";
}


function show_copyright() { 
    document.getElementById('copyimage').src=pic1.src;
}

function hide_copyright() {
    document.getElementById('copyimage').src="/blank.gif";
}

function stretch_copyright(){
    document.getElementById('copyimage').style.position="absolute";
    document.getElementById('copyimage').style.top="0";
    document.getElementById('copyimage').style.left="0";
    document.getElementById('copyimage').width = document.getElementById('bigimage').clientWidth;
    document.getElementById('copyimage').height = document.getElementById('bigimage').clientHeight;
    document.getElementById('copyimage').style.marginLeft = document.getElementById('bigimage').style.marginLeft;
    document.getElementById('copyimage').style.marginTop = document.getElementById('bigimage').style.marginTop;
}

function write_image(image_name, wmargin, hmargin, w, h){
    var stretch='';
    var onmouse='';
    if(tear==0){
        stretch="onload='stretch_copyright();'";
        onmouse="onmouseover='show_copyright();' onmouseout='hide_copyright();'";
    } 
    var image_string="<img id='bigimage' "+stretch+" src='" + image_name + "' "+w+" "+h+" alt='' style='margin-left:"+wmargin+"px;margin-top:"+hmargin+"px;' /><img src='/blank.gif' width='100%' height='100%' "+onmouse+" id='copyimage' />";     
    document.getElementById('image').innerHTML=image_string;
}

function viewport()
{
w = document.getElementById('image').clientWidth;
h = document.getElementById('image').clientHeight;
//alert('width='+w+' and height='+h);

 if (typeof window.innerWidth != 'undefined'){
      fullh = window.innerHeight;
 } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){
      fullh = document.documentElement.clientHeight;
 } else {
       fullh = document.getElementsByTagName('body')[0].clientHeight ;
 }



var image_string='';
var image_height=''
var image_width=''
var hmargin=0;



if( (((w/background.width)*background.height) > ((w/maxwidth)*maxheight)) && (((w/maxwidth)*maxheight) < h) ){
    if (((w/maxwidth)*maxheight)< minheight){
        h=minheight;
    }else{
        h=Math.round((w/maxwidth)*maxheight);
    }
    w2=Math.round(((h/background.height)*background.width)); 
    wmargin=w-w2;
    if(wmargin<0){
        wmargin=0;
    }
    image_height="height='"+h+"px'";
    image_width="width='"+w2+"px'";
}


else if(h<minheight){
    w2=Math.round(((minheight/background.height)*background.width)); 
    wmargin=w-w2;
    if(wmargin<0){
        wmargin=0;
    }
    h=minheight;
    image_height="height='"+h+"px'";
    if(w<minwidth){
        w=minwidth;
    }
    if(w2>w){
        w2=w;
        image_height='';
        h2=Math.round(((w2/background.width)*background.height));
    }
    if(Math.round(((w2/maxwidth)*maxheight))<h){       
        h=Math.round(((w2/maxwidth)*maxheight));
    }
    if(h<minheight){
        h=minheight;
    }
    hmargin=(h-h2)/2;
    image_width="width='"+w2+"px'"; 
}

/*image has a higher ratio than display space */ 
else if (w/h > (background.width/background.height)){
    w2=Math.round(((h/background.height)*background.width));
    wmargin=w-w2;
    image_height="height='"+h+"px'";
}

/*image has a wider ratio than display space */
else if (w/h <= (background.width/background.height)){
    h2=Math.round(((w/background.width)*background.height)); 
    if(h2<minheight){
        h2=minheight;
    }
    wmargin=0;
    image_height="height='"+h2+"px'"; 
    w2=Math.round(((h2/background.height)*background.width));
    if(w<minwidth){
        w=minwidth;
    }
    if(w2>w){
        w2=w;
        image_height='';
        h2=Math.round(((w2/background.width)*background.height));
    }
    if(Math.round(((w2/maxwidth)*maxheight))<h){
        h=Math.round(((w2/maxwidth)*maxheight));
    }
    hmargin=(h-h2)/2;    
    image_width="width='"+w2+"px'";   
}

document.getElementById('scrollable').style.top=(h-152)+"px";
if(document.getElementById('navSub')){
    document.getElementById('navSub').style.bottom=((fullh-(document.getElementById('scrollable').offsetTop)))+5+"px";
}
document.getElementById('prev').style.top=(h-170)+"px";
document.getElementById('next').style.top=(h-170)+"px";
document.getElementById('page_counter').style.top=(h-170)+"px";
document.getElementById('main_prev').style.top=(h+25)+"px";
document.getElementById('main_next').style.top=(h+25)+"px";
document.getElementById('thumb_counter').style.top=(h+25)+"px";
    
var image = new Image();
image.onload = function() {
    write_image(background.image, wmargin, hmargin, image_width, image_height);
    if(w > document.getElementById('bigimage').width){
        document.getElementById('caption').style.width=w+"px";
    }else{
        document.getElementById('caption').style.width=document.getElementById('bigimage').width+"px";
    }
    document.getElementById('caption').style.marginLeft="155px";
    document.getElementById('caption').style.top=(h+20)+"px";

};
image.src = background.image;
}

