var isIE = document.all?true:false;
var isMac = navigator.platform.toLowerCase().indexOf('mac') >= 0?true:false;  
var isFF = navigator.userAgent.toLowerCase().indexOf('firefox') >= 0?true:false;  
var isSafari = navigator.userAgent.toLowerCase().indexOf('safari') >= 0?true:false;

function preview(imagesid,resultid) {
  var param = '';
  if ( typeof imagesid != 'undefined' ) {
    if ( typeof resultid != 'undefined' ) {
      if ( resultid == 'lightbox' ) {
        param = 'imagesid='+imagesid+'&from_lightbox=1';
      }
      else {
        param = 'imagesid='+imagesid+'&resultid='+resultid;
      }
    } else {
      param = 'imagesid='+imagesid;
    }
  } 
 var winHandle = window.open('/preview.ep?'+param, ''+imagesid+'' , 'toolbar=no, location=no, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=yes, copyhistory=no, width=850, height=600, dependent' );
winHandle.focus();
 
}

function windowheight() {
  if ( window.innerHeight )  { // all except Explorer
    return window.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    return document.documentElement.clientHeight;
  }
  else if (document.body) { // other Explorers
    return document.body.clientHeight;
  }
 else return 0;
}
function windowwidth() {
  if ( window.innerWidth )  { // all except Explorer
    return window.innerWidth;
  }
  else if (document.documentElement && document.documentElement.clientWidth) { // Explorer 6 Strict Mode
    return document.documentElement.clientWidth;
  }
  else if (document.body) { // other Explorers
    return document.body.clientWidth;
  }
 else return 0;
}

function resizeStatic2 ( lb ) {
  var nav_height  = document.getElementById("Static1")?document.getElementById("Static1").offsetHeight:0;
  var foot_height = document.getElementById("Static3")?document.getElementById("Static3").offsetHeight:0;
  the_lightbox    = document.getElementById('lightbox');
  var win_height  = windowheight();
  var lb_height   = 0;
  var con_height = 0;

  var bug = 20;
  
  if ( lb == 'on' || ( the_lightbox && measure2num(the_lightbox.style.height) > 0 ) ) {
    lb_height = measure2num(getLbHeight() ) + bug ;
  }
  con_height  = win_height - (nav_height + foot_height + lb_height + bug ); 
  document.getElementById("Static2").style.height = con_height + "px";
/* alert ( "nav_h: " + nav_height +"\n" +
      "foot_h: " + foot_height + "\n" +
      "win_h: " + win_height  + "\n" +
      "lb_h: "+ lb_height + "\n" +
      "cookie: " + getCookie("userheight") + "\n" +
  "Übrig: " + document.getElementById("Static2").style.height ); */
  return true;
}
function measure2num ( the_value ) {
  var zahl = parseInt( the_value );
  if ( isNaN( zahl ) ) {
    return false ;
  } else {
    return zahl; 
  }
}
function toggle_all ( formname, boxname ) {
  if ( document.forms[formname].elements[boxname] ) { // gibt es überhaupt eine?
    if ( document.forms[formname].elements[boxname].length ) { // gibt es mehrere?
      i = document.forms[formname].elements[boxname].length;
      for ( n = 0; n < i; n++ ) {
        document.forms[formname].elements[boxname][n].checked = document.forms[formname].elements[boxname][n].checked?false:true
      }  
    } else {  // nur eins
      document.forms[formname].elements[boxname].checked = document.forms[formname].elements[boxname].checked?false:true;;
    }
  } 
}
function update_lightbox ( action , theItem ) {
   var source_lightbox = parent.document.forms['lightbox_change'].elements['change_lightbox'].value;
   var dest_lightbox = '';
   switch (action) {
     case 'move':
       dest_lightbox = document.getElementById('to_lb2').value;
       break;
     case 'copy':
       dest_lightbox = document.getElementById('to_lb').value;
       break;
     case 'del_many':
   }
   if ( source_lightbox == dest_lightbox ) {
     return false;
   }
   var theForm = document.forms['lightbox_action'];
   theForm.elements['lbtool_action'].value = action;
   theForm.elements['to_lb'].value = dest_lightbox;
   var targets = new Array();
   $A(theForm.elements['imagesid']).map( function (val) { if (val.checked) { targets.push( theItem + val.value) } } );
   
   var toUpdate = $(parent.frames['basketinfo'].document.getElementById('progress') );
   new Ajax.Request( '/funcs/func_lbtool.ep', { onComplete: function(req){ toUpdate.innerHTML = req.responseText },
                                                asynchronous:true, 
                                                parameters:Form.serialize(theForm)});

   switch (action) {
     case 'del_many':
     case 'move':
       targets.map (function ( item ) { Effect.Fade(item) }); 
       break;
     case 'copy':
       targets.map (function ( item ) { Effect.Pulsate(item,{pulse: 2, duration: 1, from: 0.7}) });
       break;
   }
   return false;
 }
 function update_basket_note ( transid ) {
  var note = $F('basket_note_' + transid);
  new Ajax.Updater($(parent.frames['basketinfo'].document.getElementById('progress')), '/funcs/func_basket.ep', 
                   { parameters: {basket_note: note , transid: transid, basket_action: "add_note"}
                    });
 }
function update_all_basket_notes ( transid ) {
  var note       = $F('basket_note_' + transid);
  var note_boxes = document.getElementsByClassName('basket_note');
  var all_tids   = note_boxes.collect(function(box) {
						               var t_id = box.id.split('_')[2];
                           return "action_transid=" + t_id;
                         }).join('&');
  new Ajax.Updater($(parent.frames['basketinfo'].document.getElementById('progress')), '/funcs/func_basket.ep', 
                   { parameters: {basket_note: note , basket_action: "add_note"},
                     postBody: all_tids + "&basket_note="+note+"&basket_action=add_note",
                     onSuccess: function(){ note_boxes.each(function(box){
                                                      box.value = note; 
                                           });
                                   }
                    });
 }
function check4lbaction(elem) {
  if ( $F(elem) == 'create' ) {
    document.location.href="/index.ep?page=lbtool&amp;lbtool_action=create";
    return false;
  }
  if ( $F(elem) == '' || $F(elem) == null ) { // IE kann noch immer nicht "disabled"
    return false;
  }
  document.getElementById('lightbox_change').submit();
}
function hideIt() {
	  document.getElementById("srPrev").style.display = "none";
}
function preLoadImages(){
  var myimages = new Array(preLoadImages.arguments.length);
  for (i=0;i<preLoadImages.arguments.length;i++) {
    myimages[i]=new Image()
    myimages[i].src=preLoadImages.arguments[i]
  }
}
 var ListFuncs = {
    findByImageId : function(imageid) {
                      return this.find(function(item){
                           return item.id.match(imageid);
                      });
                    },
    highest: function() {
              return this.sortBy(function(item) {
                return item.getHeight();
                }).last();
            }
}

function license_selected() { 
  
  var theForm = document.forms['basket_form'];
  var thePages = new Array;
    
  var checked_Images = $(theForm).getInputs('checkbox', 'action_transid');
  checked_Images.map( function (val) {if (val.checked) {  thePages.push($F('lm_page_4_' + val.value) ) } } );
  thePages = thePages.uniq();
  if ( thePages.size() != 0 ) {
    if ( thePages.size() > 1 ) {
     alert('Right Managed and Royalty Free Images must be licensed in separate actions.');
    } else {
      thePage = thePages[0];
     if ( thePage != '' ) {
       theForm.elements['page'].value = thePage;
       theForm.elements['basket_action'].value = 'license';
       theForm.submit();
      } else {
        alert('these images are obviously already licensed');
     }
    }
  }
  
}