var currentCategory ='';
function buildurl()
{
    var u = BASE_URL + CAD_LIBRARY_URL; 
    if(typeof currentCategory != 'undefined' && currentCategory != '') u +='?cat='+ currentCategory;
    return u;
}
function cadprinting()
{
    var u = document.location.href;
    u = u.split('?');
    u = u[0];
    u+= '?alttemplate=CAD%20List&print=1';

   if(typeof currentCategory != 'undefined' && currentCategory != '') u +='&cat='+ currentCategory;
   w = window.open(u,'print','width=750,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
   w.focus(); 
}
function cadpdf()
{
    var title ='CAD';
    var u = document.location.href;
    u = u.split('?');
    u = u[0];

    u+= '?alttemplate=cad%20list&print=1';
    if(typeof currentCategory != 'undefined' && currentCategory != '') u +='&cat='+ currentCategory;

    var p = '/umbraco/plugins/pdf/export.aspx';
    p+='?title='+ escape(title);
    p+='&url='+ escape(u);
    document.location.href=p;
}
function permalink(node)
{
    document.getElementById(node).style.display='';
    document.getElementById(node+'canvas').style.display='';
    var link = document.getElementById(node +'_link');
    link.value = buildurl();
}
function closePerma(node)
{
    document.getElementById(node).style.display='none';
    document.getElementById(node+'canvas').style.display='none';
}
function cadPreview(name,img)
{
    document.getElementById('file_name').innerHTML = name;
    document.getElementById('preview_img').src =(img)? img:'/media/326/transp.gif';
}
function showPreview(img)
{
 
    document.getElementById('iconwait').style.display='';
    var el = document.getElementById('imgpreview');
    el.style.width = '0px';
    el.style.height =  '0px';

    var el = document.getElementById('bigpreview');
    el.style.width ='300px';
    el.style.height = '300px';
    el.style.marginLeft = "-" + parseInt(el.offsetWidth / 2) + "px";
    el.style.marginTop = "-" + parseInt(el.offsetHeight / 2) + "px";
   
    el.style.display = '';
    var u = '/umbraco/plugins/cadexport/cadPreview.aspx';
    u += '?img='+ img;
    frames['imgpreview'].location.href = u;
}
function iframeLoaded(w,h)
{
   document.getElementById('iconwait').style.display='none';
   var el = document.getElementById('imgpreview');
   el.style.width = w +'px';
   el.style.height =  h +'px';
   
   var el = document.getElementById('bigpreview');
   el.style.width = w +'px';
   el.style.height = (parseInt(h) + 30)+'px';
   el.style.marginLeft = "-" + parseInt(el.offsetWidth / 2) + "px";
   el.style.marginTop = "-" + parseInt(el.offsetHeight / 2) + "px";
}
function closePreview()
{
   var el = document.getElementById('bigpreview');
   el.style.display = 'none';
}

function updatePreviewSize()
{
 var img = document.getElementById('bigimagepreview');
  if(img.src.indexOf('transp.gif') > 0) return;
  alert(img.width +'x'+ img.height);
  document.getElementById('iconwait').style.display='none';
  img.style.display='';
}
function hidePreview()
{
  cadPreview('','');
}

function checkAll(list)
{
    clearDownload();
	for (i = 0; i < list.length; i++)
	{
		list[i].checked = (list[i].checked)? false:true ;
	}
}
function exportZip(list)
{
  frames['cadexport'].location.href = '/umbraco/plugins/cadexport/zip.aspx';
  var cads = '';
  for (i = 0; i < list.length; i++)
  {
    if(!list[i].checked) continue;
    if(cads!='') cads+=',';
    cads+= list[i].value; 
  }
  if(cads =='')
  {
     alert(NO_FILES_SELECTED);
     return;
  }
  msg('','download_wait','677','20');
 
  try
 {
  frames['cadexport'].document.myform.cads.value = cads;
  frames['cadexport'].document.myform.lev.value = '3';
  frames['cadexport'].document.myform.submit();
  }
  catch(err)
  {
    
   alert(err.description);
  }
}

function clearDownload()
{
    ;
}
function downloadReady(path)
{
  document.getElementById('download_selected').style.display='';
  document.getElementById('download_wait').style.display='none';
  frames['caddownload'].location.href = path;
  frames['cadexport'].location.href = '/umbraco/plugins/cadexport/zip.aspx';
}
function loadCategorySelector(placeholder,url)
{
    var u = '/'+ url+'&placeholder='+ placeholder;
    var c = querySt('cat');
    if(c!='') u+= '&cat='+ c;
    u = u.replace('//','/');
    
    var d = doSimpleXMLHttpRequest(u);
    d.addCallbacks(displayCategorySelector);
}
function displayCategorySelector(sel)
{
    var sel = sel.responseText;
    var placeholder = sel.slice(0,sel.indexOf('<'));
    var cat = sel.slice(sel.indexOf('<'),sel.length);

    document.getElementById('placeholder_'+ placeholder).innerHTML = cat;

    var c = querySt('cat');
  
    if(c!='') 
    {
        
        var ph = document.getElementById('cat_'+ placeholder);
        for(var i=0;i < ph.options.length;i++)
        {
           id = '';
           if(placeholder == '1' && typeof(window['cat_4']) != 'undefined') id = cat_4; 
           if(placeholder == '2' && typeof(window['cat_5']) != 'undefined') id = cat_5; 
           if(placeholder == '3' && typeof(window['cat_6']) != 'undefined') id = cat_6;
           
           var currentID = ph.options[i].value.split('|');
           currentID = currentID[currentID.length -1];

           if(currentID == id) 
           { 
              
              ph.selectedIndex = i;
              ph.options[i].selected = true;
              var next = parseInt(placeholder) + 1
             //check there is select for the next level
              try { var d = document.getElementById('placeholder_'+ next); }
              catch(e) {return;}
             loadCategorySelector(next, currentID +'.aspx?alttemplate=cad category');
              return;
           }
        }
      }
}
function updateCADCategory(cat,placeholder)
{
   var sel = cat.options[cat.selectedIndex].value;
   if(sel=='-') return;
   placeholder = parseInt(placeholder) + 1;

  
   if(placeholder == 2 )
   { 
      try { document.getElementById('placeholder_3').innerHTML ='<select size="1"></select>'; }
      catch(e) {;}
   }

   sel  = sel.split('|');
   //var url = sel[0];
   var id = sel[1];
   var url = id +'.aspx?alttemplate=cad category';
   
   
   //load the new select box (only tree levels)
   try {
      var el = document.getElementById('placeholder_'+ placeholder);
      if(el) loadCategorySelector(placeholder,url);
   } catch(e) {;}
   loadCategoryList(id);
}
function loadCategoryList(id)
{
   currentCategory = id;
   var u =CAD_LIBRARY_URL +'?alttemplate=cad%20list&cat='+ id;
   var d = doSimpleXMLHttpRequest(u);
   d.addCallbacks(displayCategoryList);
}
function displayCategoryList(sel)
{
   document.getElementById('dynamic_list').innerHTML = sel.responseText;
}


var currentCategory ='';
function buildurl()
{
    var u = BASE_URL + CAD_LIBRARY_URL; 
    if(typeof currentCategory != 'undefined' && currentCategory != '') u +='?cat='+ currentCategory;
    return u;
}
function cadprinting()
{
    var u = document.location.href;
    u = u.split('?');
    u = u[0];
    u+= '?alttemplate=CAD%20List&print=1';

   if(typeof currentCategory != 'undefined' && currentCategory != '') u +='&cat='+ currentCategory;
   w = window.open(u,'print','width=750,height=600,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no');
   w.focus(); 
}
function cadpdf()
{
    var title ='CAD';
    var u = document.location.href;
    u = u.split('?');
    u = u[0];

    u+= '?alttemplate=cad%20list&print=1';
    if(typeof currentCategory != 'undefined' && currentCategory != '') u +='&cat='+ currentCategory;

    var p = '/umbraco/plugins/pdf/export.aspx';
    p+='?title='+ escape(title);
    p+='&url='+ escape(u);
    document.location.href=p;
}
function permalink(node)
{
    document.getElementById(node).style.display='';
    document.getElementById(node+'canvas').style.display='';
    var link = document.getElementById(node +'_link');
    link.value = buildurl();
}
function closePerma(node)
{
    document.getElementById(node).style.display='none';
    document.getElementById(node+'canvas').style.display='none';
}
function cadPreview(name,img)
{
    document.getElementById('file_name').innerHTML = name;
    document.getElementById('preview_img').src =(img)? img:'/media/326/transp.gif';
}
function showPreview(img)
{
 
    document.getElementById('iconwait').style.display='';
    var el = document.getElementById('imgpreview');
    el.style.width = '0px';
    el.style.height =  '0px';

    var el = document.getElementById('bigpreview');
    el.style.width ='300px';
    el.style.height = '300px';
    el.style.marginLeft = "-" + parseInt(el.offsetWidth / 2) + "px";
    el.style.marginTop = "-" + parseInt(el.offsetHeight / 2) + "px";
   
    el.style.display = '';
    var u = '/umbraco/plugins/cadexport/cadPreview.aspx';
    u += '?img='+ img;
    frames['imgpreview'].location.href = u;
}
function iframeLoaded(w,h)
{
   document.getElementById('iconwait').style.display='none';
   var el = document.getElementById('imgpreview');
   el.style.width = w +'px';
   el.style.height =  h +'px';
   
   var el = document.getElementById('bigpreview');
   el.style.width = w +'px';
   el.style.height = (parseInt(h) + 30)+'px';
   el.style.marginLeft = "-" + parseInt(el.offsetWidth / 2) + "px";
   el.style.marginTop = "-" + parseInt(el.offsetHeight / 2) + "px";
}
function closePreview()
{
   var el = document.getElementById('bigpreview');
   el.style.display = 'none';
}

function updatePreviewSize()
{
 var img = document.getElementById('bigimagepreview');
  if(img.src.indexOf('transp.gif') > 0) return;
  alert(img.width +'x'+ img.height);
  document.getElementById('iconwait').style.display='none';
  img.style.display='';
}
function hidePreview()
{
  cadPreview('','');
}

function checkAll(list)
{
    clearDownload();
	for (i = 0; i < list.length; i++)
	{
		list[i].checked = (list[i].checked)? false:true ;
	}
}
function exportZip(list)
{
  frames['cadexport'].location.href = '/umbraco/plugins/cadexport/zip.aspx';
  var cads = '';
  for (i = 0; i < list.length; i++)
  {
    if(!list[i].checked) continue;
    if(cads!='') cads+=',';
    cads+= list[i].value; 
  }
  if(cads =='')
  {
     alert(NO_FILES_SELECTED);
     return;
  }
  msg('','download_wait','677','20');
 
  try
 {
  frames['cadexport'].document.myform.cads.value = cads;
  frames['cadexport'].document.myform.lev.value = '3';
  frames['cadexport'].document.myform.submit();
  }
  catch(err)
  {
    
   alert(err.description);
  }
}

function clearDownload()
{
    ;
}
function downloadReady(path)
{
  document.getElementById('download_selected').style.display='';
  document.getElementById('download_wait').style.display='none';
  frames['caddownload'].location.href = path;
  frames['cadexport'].location.href = '/umbraco/plugins/cadexport/zip.aspx';
}
function loadCategorySelector(placeholder,url)
{
    var u = '/'+ url+'&placeholder='+ placeholder;
    var c = querySt('cat');
    if(c!='') u+= '&cat='+ c;
    u = u.replace('//','/');
    
    var d = doSimpleXMLHttpRequest(u);
    d.addCallbacks(displayCategorySelector);
}
function displayCategorySelector(sel)
{
    var sel = sel.responseText;
    var placeholder = sel.slice(0,sel.indexOf('<'));
    var cat = sel.slice(sel.indexOf('<'),sel.length);

    document.getElementById('placeholder_'+ placeholder).innerHTML = cat;

    var c = querySt('cat');
  
    if(c!='') 
    {
        
        var ph = document.getElementById('cat_'+ placeholder);
        for(var i=0;i < ph.options.length;i++)
        {
           id = '';
           if(placeholder == '1' && typeof(window['cat_4']) != 'undefined') id = cat_4; 
           if(placeholder == '2' && typeof(window['cat_5']) != 'undefined') id = cat_5; 
           if(placeholder == '3' && typeof(window['cat_6']) != 'undefined') id = cat_6;
           
           var currentID = ph.options[i].value.split('|');
           currentID = currentID[currentID.length -1];

           if(currentID == id) 
           { 
              
              ph.selectedIndex = i;
              ph.options[i].selected = true;
              var next = parseInt(placeholder) + 1
             //check there is select for the next level
              try { var d = document.getElementById('placeholder_'+ next); }
              catch(e) {return;}
             loadCategorySelector(next, currentID +'.aspx?alttemplate=cad category');
              return;
           }
        }
      }
}
function updateCADCategory(cat,placeholder)
{

   var sel = cat.options[cat.selectedIndex].value;
   if(sel=='-') return;
   placeholder = parseInt(placeholder) + 1;

  
   if(placeholder == 2 )
   { 
      try { document.getElementById('placeholder_3').innerHTML ='<select size="1"></select>'; }
      catch(e) {;}
   }

   sel  = sel.split('|');
   //var url = sel[0];
   var id = sel[1];
   var url = id +'.aspx?alttemplate=cad category';
   
   //load the new select box (only tree levels)
   try {
      var el = document.getElementById('placeholder_'+ placeholder);
      if(el) loadCategorySelector(placeholder,url);
   } catch(e) {;}
   loadCategoryList(id);
}
function loadCategoryList(id)
{
   currentCategory = id;
   //var u = CAD_LIBRARY_URL +'?alttemplate=cad%20list&cat='+ id;
   //var d = doSimpleXMLHttpRequest(u);
   //d.addCallbacks(displayCategoryList);

   $.ajax({
       type: "GET",
       url: CAD_LIBRARY_URL,
       data: {'alttemplate':'cad list','cat':id},
       success: function(msg){
         document.getElementById('dynamic_list').innerHTML = msg;
       },
       error: function(req,status,msg){
         //alert(req.responseText);
         document.getElementById('dynamic_list').innerHTML = req.responseText;
       }
   });
   
}
function displayCategoryList(sel)
{
   document.getElementById('dynamic_list').innerHTML = sel.responseText;
}




