var dragapproved=false
var minrestore=0
var initialwidth,initialheight
var ie5=document.all&&document.getElementById
var ns6=document.getElementById&&!document.all

function iecompattest(){
    return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function drag_drop(e){
    if (ie5&&dragapproved&&event.button==1){
        document.getElementById("dwindow").style.left=tempx+event.clientX-offsetx+"px"
        document.getElementById("dwindow").style.top=tempy+event.clientY-offsety+"px"
    } else if (ns6&&dragapproved){
        document.getElementById("dwindow").style.left=tempx+e.clientX-offsetx+"px"
        document.getElementById("dwindow").style.top=tempy+e.clientY-offsety+"px"
    }
}

function initializedrag(e){
    offsetx=ie5? event.clientX : e.clientX
    offsety=ie5? event.clientY : e.clientY
    //document.getElementById("dwindowcontent").style.display="none" //extra
    tempx=parseInt(document.getElementById("dwindow").style.left)
    tempy=parseInt(document.getElementById("dwindow").style.top)

    dragapproved=true
    document.getElementById("dwindow").onmousemove=drag_drop
}

function loadwindow(evt, id, win_size){
    if (!ie5&&!ns6)
        window.open(url,"","width=width,height=height,scrollbars=1")
    else{
        var obj = evt.target || evt.srcElement;
        var position = get_element_position(obj);

        var ww = 12+(win_size)*92;

        document.getElementById("dwindow").style.display='block';
        document.getElementById("dwindow").style.width= ww + 'px';
        document.getElementById("dwindow").style.left =(position.left) + 'px';
        document.getElementById("dwindowcontent").innerHTML = document.getElementById("gc_"+id).innerHTML;
        document.getElementById("dwindow_buttons").style.width= ww + 'px';
        document.getElementById("dwindowcontent").style.width= ww + 'px';       
        document.getElementById("dwindowcontent").style.left= (ie5? 0 : "12px");

        var ele=document.getElementById("dwindow");
        ele.style.top = (obj.offsetHeight + position.top -ele.offsetHeight) + 'px';
    }
}

function maximize(){
    if (minrestore==0){
        minrestore=1 //maximize window
        document.getElementById("maxname").setAttribute("src","restore.gif")
        document.getElementById("dwindow").style.width=ns6? window.innerWidth-20+"px" : iecompattest().clientWidth+"px"
        document.getElementById("dwindow").style.height=ns6? window.innerHeight-20+"px" : iecompattest().clientHeight+"px"
    } else {
        minrestore=0 //restore window
        document.getElementById("maxname").setAttribute("src","max.gif")
        document.getElementById("dwindow").style.width=initialwidth
        document.getElementById("dwindow").style.height=initialheight
    }
    document.getElementById("dwindow").style.left=ns6? window.pageXOffset+"px" : iecompattest().scrollLeft+"px"
    document.getElementById("dwindow").style.top=ns6? window.pageYOffset+"px" : iecompattest().scrollTop+"px"
}

function closeit(){
    document.getElementById("dwindow").style.display="none"
}

function stopdrag(){
    dragapproved=false;
    document.getElementById("dwindow").onmousemove=null;
    //document.getElementById("dwindowcontent").style.display="" //extra
}
function get_element_position(obj) {
    var offset_left = obj.offsetWidth+5;
    var offset_top = 0;
    while (obj) {
        offset_left += obj.offsetLeft;
        offset_top += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return {
        left:offset_left,
        top:offset_top
    }
}

function select_glass_color(sample_id, photo_id, prod_id, gc_id, code){
    var img = document.getElementById('i_'+prod_id);
    img.src='/img/t'+photo_id+'.jpg';
    var hgc = document.getElementById('hgc_'+prod_id);
    hgc.value = gc_id;
    var sample = document.getElementById('l_'+prod_id);
    sample.innerHTML = code;
    sample.style.background="url(/img/t"+sample_id+".jpg)";
}
