﻿var ua=navigator.userAgent
var dom=(document.getElementById)? true : false
var ie4=(document.all&&!dom)? true : false
var ie5_5=((ua.indexOf("MSIE 5.5")>=0 || ua.indexOf("MSIE 6")>=0)&&ua.indexOf("Opera")<0)? true : false
var ns4=(document.layers&&!dom)? true : false
var current_event;
var current_X,current_Y;
var offsxy=6;
var xmlReqSample;

function ShowSample(detailcompid){HideAddToMyPortfolio();
    var comp = document.getElementById(detailcompid);
    if(comp){
        var detail = comp.value;
        var details = detail.split("|||");
        var headcomp = document.getElementById("spnTitle");
        var matcomp = document.getElementById("spnMaterial");
        var imgcomp = document.getElementById("imgBigSample");
        var desccomp = document.getElementById("spnDescription");
        var hdnSampleId=document.getElementById("hdnSampleId");
        if(headcomp!=null && matcomp!=null && imgcomp!=null && desccomp!=null && hdnSampleId!=null){
            headcomp.innerHTML = details[0];
            matcomp.innerHTML = details[1];
            imgcomp.src = details[3];
            desccomp.innerHTML = details[2];
            hdnSampleId.value = details[4];
        }
        GalleryModal.Show();
    }
}

//LoadMyPortfolio();

function LoadMyPortfolio()
{
    var requestUrl="AddSampleToMyPortfolio.aspx?dtype=company"
    XmlReqSample=CreateXmlReq();
    if(XmlReqSample){    
    XmlReqSample.onreadystatechange=getMyPortfolio;
    XmlReqSample.open("POST",requestUrl);
    XmlReqSample.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    XmlReqSample.send("Load=Portfolio");}
}

function getMyPortfolio()
{   
    if(XmlReqSample.readyState==4)
    {
        if(XmlReqSample.status==200)
        {
            //alert(XmlReqSample.responseText);
            document.getElementById('hdnMyPortfolioSamples').value=XmlReqSample.responseText;
        }
    }
}

// The Following function gets the Left of an object
function getObjectLeft(obj,finalid){
    if(finalid!=null && obj.id.toUpperCase().replace(/^\s+|\s$/,"")==finalid.toUpperCase().replace(/^\s+|\s$/,"")) return 0;
    return obj.offsetLeft + (obj.offsetParent ? getObjectLeft(obj.offsetParent,finalid) : obj.x ? obj.x : 0);
}
// The Following function gets the Top of an object
function getObjectTop(obj,finalid){
    if(finalid!=null && obj.id.toUpperCase().replace(/^\s+|\s$/,"")==finalid.toUpperCase().replace(/^\s+|\s$/,"")) return 0;
    return (obj.offsetParent ? obj.offsetTop + getObjectTop(obj.offsetParent,finalid) : obj.y ? obj.y : 0);    
}

function AddToPortfolio(img){
    try{
    var SampleId;
    SampleId=document.getElementById("hdnSampleId").value;
    e=current_event
    var curleft=curtop=0
    obj=document.getElementById("imgPopup");
    curleft = getObjectLeft(obj,"divPortfolio");
    curtop = getObjectTop(obj,"divPortfolio");    
    
    var hdnsampleIds=document.getElementById("hdnMyPortfolioSamples");
    
    var sids=hdnsampleIds.value;
    if(sids.indexOf(SampleId)==-1)
    {
        if(sids=='')
        {
            sids=SampleId;
        }
        else
        {
            sids = sids + '|||' + SampleId ;
        }
        hdnsampleIds.value=sids;        
    }
    
    var sampleIds = sids.split("|||");
       
    var sampleCount=sampleIds.length;
    var tooltip=document.getElementById('hmpopupDiv');
    e=e?e:window.event    
    var txt='<table border="0" cellspacing="0" cellpadding="0" bgcolor="#000000"><tr><td><table border="0" cellpadding="0" cellspacing="0" bgcolor="#000000" width="100%"><tr><td class="defaultfont" align="left" nowrap height="20px"><font color=#ffffff size="2"><b>&nbsp;Added to My Portfolio&nbsp;</b></font></td><td><a href="javascript:HideAddToMyPortfolio();"><img border="0" src="images/popup_close.gif" width="17" height="17"></a></td></tr></table></td></tr><tr><td bgcolor="#E9E9EA"><table cellspaceing="0" cellspadding="0" width="100%"><tr><td class="defaultfonts" align="left">' + sampleCount + ' Sample(s) in My Portfolio.</td></table></td></tr></table>'
    var tip='<table border="1" cellpadding="0" cellspacing="0" style="border:solid 1px #666666; border-collapse:collapse;" ><tr valign="top"><td>'+txt+'</td></tr><tr><td bgcolor="#CECDCC" align="center"><a href="Saved-PortFolio.aspx" class="header">View My Portfolio</a></td></tr></table>'
    var off = 15;
    if(ns4){
        tooltip.document.write(tip)
        tooltip.document.close()
        var toolheight = tooltip.offsetHeight ? tooltip.offsetHeight : 0;
        tooltip.left=curleft
        tooltip.top = curtop - (toolheight - off);
    }else{
        tooltip.innerHTML=tip;
        var toolheight = tooltip.offsetHeight ? tooltip.offsetHeight : 0;
        tooltip.style.left=curleft+"px"
        tooltip.style.top = curtop - (toolheight - off) +"px"
    }
    with(tooltip){ns4 ? visibility="show" : style.visibility="visible"}
    XmlReqSample=CreateXmlReq()
    var requestUrl="AddSampleToMyPortfolio.aspx?dtype=company"
    if(XmlReqSample){
        XmlReqSample.onreadystatechange=HandleToMyPortfolio;
        XmlReqSample.open("POST",requestUrl);
        XmlReqSample.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        XmlReqSample.send("SampleId="+SampleId);
    }
}
catch(err){alert(err)}
}

function HandleToMyPortfolio()
{
}

function atooltip1(){
return ns4 ? document.hmpopupDiv : ie4 ? document.all.hmpopupDiv : document.getElementById('hmpopupDiv')}
function HideAddToMyPortfolio(){
var tooltip=atooltip1()
ns4 ? tooltip.visibility="hide" : tooltip.style.visibility="hidden"}

function CreateXmlReq(){
var objXmlReq
try{
objXmlReq=new ActiveXObject("Msxml2.XMLHTTP")}
catch(e){
try{
objXmlReq=new ActiveXObject("Microsoft.XMLHTTP")}
catch(oc){
objXmlReq=null}}
if(!objXmlReq&&typeof XMLHttpRequest !="undefined"){
objXmlReq=new XMLHttpRequest()}
return objXmlReq}


var ResArr = new Array();
var CommArr = new Array();
function InitGall(divid,bigpath,Arr,GroupId){
    var contcomp = document.getElementById(divid);    
    if(contcomp!=null){
        var imgcomps = contcomp.getElementsByTagName("img");
        var bigroot = bigpath;
        for(el=0;el<imgcomps.length;el++){           
            var currcomp = imgcomps[el];            
            if(currcomp!=null){
                if(currcomp.attributes["gallery"]!=null){
                    if(currcomp.attributes["gallery"].value.toUpperCase()==GroupId.toUpperCase()){
                        var dirs = currcomp.src.split("/");
                        //var filename = bigroot + dirs[dirs.length-1];
                        var hdnDetails = document.getElementById(currcomp.attributes["hdnid"].value).value;
                        var details = hdnDetails.split('|||');
                        var filename = details[3];
                        var name = details[0];
                        //var filename = bigroot + currcomp.attributes["bigimage"].value;
                        //alert(filename);
                        //var name = currcomp.attributes["galname"]?currcomp.attributes["galname"].value:"";
                        var desc = details[2];
                        var sid = details[4];
                        var material = details[1];
                        Arr[Arr.length] = {bigpath: filename, head: name, smallpath: currcomp.src, sid: sid, material: material, description: desc};
                        currcomp.galid = Arr.length - 1;
                        currcomp.style.cursor = "pointer";
                        currcomp.onclick = function(){
                            ShowGallery(this.galid, Arr);
                        }
                    }
                }
            }
        }
    }
}
function ShowGallery(Id, Arr){
    if(Id<Arr.length){
        HideAddToMyPortfolio();
        var btnnext = document.getElementById("btngallerynext");
        var btnprev = document.getElementById("btngalleryprev");
        var imgbig = document.getElementById("imgGalleryBig");
        var header = document.getElementById("tdGalleryHeader");
        var desc = document.getElementById("tdDescription");
        var btnrow = document.getElementById("trgalbuuttons");
        var matcomp = document.getElementById("spnMaterial");
        if(header!=null && imgbig!=null && btnnext!=null && btnprev!=null && btnrow!=null & desc!=null){
            imgbig.gall = Arr;
            btnnext.style.cursor = "pointer";
            btnprev.style.cursor = "pointer";
            btnprev.style.visibility = "visible";
            btnnext.style.visibility = "visible";
            var hidcnt = new Number(0);
            btnrow.style.display = '';
            if(Id==0){
                hidcnt++;
                btnprev.style.visibility = "hidden";
            }
            if(Id==Arr.length-1){
                hidcnt++;
                btnnext.style.visibility = "hidden";
            }
            if(hidcnt==2){
                btnrow.style.display = "none";
            }
            btnprev.img = imgbig;
            btnprev.gall = Arr;
            btnprev.galid = Id-1;
            btnprev.onclick = function(){
                this.img.onload = null;
                this.img.img.onload = null;
                ShowGallery(this.galid,this.gall);
            }
            btnnext.img = imgbig;
            btnnext.gall = Arr;
            btnnext.galid = Id+1;
            btnnext.onclick = function(){
                this.img.onload = null;
                this.img.img.onload = null;
                ShowGallery(this.galid,this.gall);
            }            
            header.innerHTML = "<b>" + Arr[Id].head + "</b>";
            imgbig.header = Arr[Id].head;
            imgbig.alt = "Loading...";
            imgbig.img = new Image();
            matcomp.innerHTML=Arr[Id].material;
            imgbig.img.bigsrc = Arr[Id].bigpath;            
            var hdnSampleId=document.getElementById("hdnSampleId");
            hdnSampleId.value = Arr[Id].sid;
            if(Arr[Id].description.replace(/^\s+|\s+$/,"")!=""){
                desc.innerHTML = "<div style='width:400px;height:100px;overflow:auto;'>" + Arr[Id].description + "</div>";
            }else{
                desc.innerHTML = "";
            }
            imgbig.img.par = imgbig;
            imgbig.onload = function(){
                this.onload = null;
                window.defaultStatus = "Loading " + this.img.bigsrc;
                this.img.onload = function(){
                    this.onload = null;
                    this.par.src = this.src;
                    this.par.alt = this.par.header + " | INNOVATIVE STONE&trade;";
                    window.defaultStatus = "Done";
                }
                this.img.src = this.img.bigsrc;
            }
            window.defaultStatus = "Loading " + Arr[Id].smallpath;
            imgbig.src = Arr[Id].smallpath;
            ShowGalleryWindow();
        }
    }
}


