var rootpath='';
var imgpath=rootpath+'img/';
var timerCloseHistory=null;
var lastHistory=null;

var lng={
  name:'en',
  dialog:{
    button:{
      run:'Run',
      open:'Open',
      savefile:'Save File'
    },
    step1:'Click the &ldquo;%BUTTON%&rdquo; button',
    note1:'After the "File Download" window appears, <strong>click on the &ldquo;%BUTTON%&rdquo; button</strong>.',
    note2:'<strong>Important:</strong> Download may take few minutes depending on your Internet connection speed. <strong>Please be patient.</strong>',
    again:'<strong>If you have difficulty downloading, you can <a href="%LINK%">try&nbsp;to&nbsp;download again.</a></strong>',
    step2:'<strong>Follow the instructions</strong> shown during installation.'
   },
   view:{
     less:'&laquo; View Less',
     more:'View More &raquo;'
   },
  more:'more',
  less:'less',
  More:'More',
  Less:'Less',
  clipboard:'The code is in your clipboard now',
  clipboarderror:'Your security settings do not allow you to copy the code to your clipboard.\nPlease copy and paste the code from textbox manually.',
  clipboardtitle:'Copy code to your clipboard',
  historylist:{
    selectedview:'All <strong>%COUNT%</strong> images on this page are selected.',
    selectall:'Select all %COUNT% images in history.',
    selectedall:'All <strong>%COUNT%</strong> images in history are selected.',
    deleteconfirm:'Do you really want to delete selected image(s)?',
    filteruse:'Use advanced filter',
    filterclear:'Clear advanced filter'
  }
}


function Flash(){this.movie="";this.img="";this.width="";this.height="";this.alt="";this.bgcolor="transparent";this.ver=7;this.link="";this.tracker="";this.ie=((navigator.userAgent.toLowerCase().indexOf("opera")==-1)&&navigator.appName=="Microsoft Internet Explorer")?true:false;this.className="";this.id="";this.urlparam="url";this.target="_blank";this.install=false;this.flashvars=null}
Flash.prototype.iF=function(i){if(!this.ie)return true;try{var a=new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + i);return true}catch(e){return false}}
Flash.prototype.write=function(m,w,h,a,b){var o=this.HTML(m,w,h,a,b);document.write(o)}
Flash.prototype.param=function(n,v){return '<param name="'+n+'" value="'+v+'" />';}
Flash.prototype.writeBanner=function(i,w,h,l,t,b){this.preBanner(i,null,w,h,l,t,b);var lnk=(this.link==null||this.link=="")?false:true;this.movie=this.img.slice(0,this.img.lastIndexOf('.'))+".swf"+((lnk)?"?"+this.urlparam+"="+escape(this.link):"");this.write();}
Flash.prototype.writeBannerAlt=function(i,m,w,h,l,t,b){this.preBanner(i,m,w,h,l,t,b);var lnk=(this.link==null||this.link=="")?false:true;if(m!=null&&m!='')this.movie=m+((lnk)?"?"+this.urlparam+"="+escape(this.link):"");this.write();}
Flash.prototype.preBanner=function(i,m,w,h,l,t,b){if(i!=null&&i!="")this.img=i;if(l!=null&&l!="")this.link=l;if(t!=null&&t!="")this.tracker=t;if(w!=null&&w!="")this.width=w;if(h!=null&&h!="")this.height=h;if(b!=null&&b!="")this.bgcolor=b;var lnk=(this.link==null||this.link=="")?false:true;if(lnk&&(this.tracker!=null&&this.tracker!=""))this.link=this.tracker+escape(this.link);var a='<img src="'+this.img+'" width="'+this.width+'" height="'+this.height+'" alt="" border="0" class="'+this.className+'" />';this.alt=((!lnk)?a:'<a href="'+this.link+'" target="'+this.target+'">'+a+'</a>');}
Flash.prototype.HTML=function(m,w,h,a,b){if(m!=null&&m!="")this.movie=m;if(w!=null&&w!="")this.width=w;if(h!=null&&h!="")this.height=h;if(a!=null&&a!="")this.alt=a;if(b!=null&&b!="")this.bgcolor=b;var ci=(this.ie)?' classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"':'';var p=(this.ie&&this.install)?' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+this.ver+',0,0,0"':'';var o;var z=this.iF(this.ver);var z1=this.iF(1);if (this.movie!=null&&this.movie!=""&&(this.install||(z1&&z)||(!z1&&!z)))o='<object type="application/x-shockwave-flash" data="'+this.movie+'" width="'+this.width+'" height="'+this.height+'"'+ci+p+' id="'+this.id+'" class="'+this.className+'" >'+this.param("movie",this.movie)+((this.flashvars!=null)?this.param("flashvars",this.flashvars):"")+this.param("quality","high")+this.param("menu","false")+((this.bgcolor=="transparent")?this.param("wmode","transparent"):this.param("bgcolor",this.bgcolor))+this.alt+'</object>';else o=this.alt;return o}
Flash.prototype.writeTo=function(id,m,w,h,a,b){var el=document.getElementById(id);if(el)el.innerHTML=this.HTML(m,w,h,a,b)}



IMTPhistory={
  imageSize:null,
  status:'first',
  needCheck:false,
  contentWidth:610,

  GI:function(id){
    return document.getElementById(id);
  },
  init:function(imageid){
    
    this.checkSize();
    this.initCopyInputs();


  },

  createIco:function(name,fce,text){
    var a=document.createElement('a');
    a.className='ico';
    a.id='ico-'+name;
    a.href='#';
    a.onclick=function(){
      fce.call(IMTPhistory,this);
      return false;
    };

    if(text){
      var b=document.createElement('span');
      b.className='icoin';
      b.innerHTML=text;
      a.appendChild(b);
    }
    return a;
  },
  show:function(id){
    this.GI(id).style.display='block';
  },

  hide:function(id){
    this.GI(id).style.display='none';
  },

  initCopyInputs:function(){

    var inputs=this.GI('sidebar').getElementsByTagName('input');

    var inputsize = inputs[0].offsetWidth;
    var fce=function(input){
      input.onclick=function(){this.select();return false;};

      if(window.clipboardData){
        input.style.width=inputsize - 30 + 'px';
        var a=this.createIco('copy',function(){
          alert( window.clipboardData.setData('text', input.value) ? lng.clipboard : lng.clipboarderror );
        });
        a.title=lng.clipboardtitle;
        var p=input.parentNode;
        p.insertBefore(a,input);
        p.insertBefore(input,a);
      }
    };


    for(var i=0;i<inputs.length;i++){
      fce.call(this,inputs[i]);
    }

  },

  setImageSize:function(){
     this.GI('image').style.width = this.contentWidth +'px';
  },
  checkSize:function(){
    var x=this.GI('image').offsetWidth;


    switch (this.status){
      case 'first':
        if(x==0)
          this.status='loading';
        else if(x<42){
          /* opera and IE may start with 40px or 32px (IE) */
          this.status='loading';
          this.needCheck=true;
          x=0;
        }
        else this.status='cache';
        /* no break passes o loading */

      case 'loading':
        if(x==0  || (x<42 && this.needCheck) ){
          setTimeout(function(){IMTPhistory.checkSize()},100);
          
          if(this.needCheck){
            setTimeout(function(){
              IMTPhistory.status='stop';
            },3000)
          };

          return false;
        }
        else this.status='done';
        break;

      case 'stop':
        this.GI('image').style.width='auto';
        this.status='done';
        break;
    };
  


    this.imageSize={x:this.GI('image').offsetWidth,y:this.GI('image').offsetHeight};

    if(this.imageSize.x > this.contentWidth){
      this.setImageSize();
    };

  }
  
 }



  var historylist={
    totalItems:0,
    itemsDateMax:null,
    itemsDateMin:null,
    selectAllInListId:'#allinlist',

    init:function(){
      var that=this;
      $('.historylist input[type=checkbox]').click(function(){
        el=this;
        if(el.checked) that.check($(el));
        else that.uncheck($(el));

      });
      
      var ch=$('.historylist input[type=checkbox]:checked');
      if(ch.length!=0)
        this.check(ch);

      $('.historylist-select-all').click(function(){
        var ch=$('.historylist input[type=checkbox]');
        var fce=function(el){
          el.checked=true;
          $(el).triggerHandler('click');
        }
       
        ch.each(function(i,n){fce.apply(this,[n])});
        var m=$('<div></div>');
        m.append(lng.historylist.selectedview.replace("%COUNT%",ch.length));
        if(historylist.totalItems>ch.length){
          $(historylist.selectAllInListId).val('0');
          m.append('&nbsp;');
          $('<a href="#">'+lng.historylist.selectall.replace("%COUNT%",historylist.totalItems)+'</a>').click(function(){
            var m2=$('<div></div>');
            $(historylist.selectAllInListId).val('1');
            m2.append(lng.historylist.selectedall.replace("%COUNT%",historylist.totalItems));
            historylist.notify(m2);
            return false;
          }).appendTo(m);

        }
        historylist.notify(m);

        return false;
      });

      $('.historylist-select-none').click(function(){
        var ch=$('.historylist input[type=checkbox]');
        var fce=function(el){
          el.checked=false;
          $(el).triggerHandler('click');
        }
       
        ch.each(function(i,n){fce.apply(this,[n])});
        $(historylist.selectAllInListId).val('');
        historylist.clearNotify();
        return false;
      });

      
      $('#historylist-filter input[type=text]').each(function(i,n){
        var p=$(n).parents('td');
        $(n).width(p.width()-p.find('select').width()- ( $(n).is('.filter-date')?35:28));
      });

      if($.datepick){
        $('#historylist-filter input.filter-date').datepick({
          showOn:'button',
          maxDate:historylist.itemsDateMax,
          minDate:historylist.itemsDateMin,
          dateFormat:"mm/dd/yy",
          buttonText:' ',
          prevText:"&#8249; Prev",
          nextText: 'Next &#8250;',
          onSelect:function(){
            historylist.filterApply();
          }
        });
      }


      $('#historylist-filter-use').click(function(){
        var el=$('#historylist-filter');

        if( el.is('.show')){
          el.removeClass('show');
          $('#historylist-filter-buttons').css('display','none');
          $('#historylist-filter-on').css('display','inline');
          historylist.filterReset();
        }else{
          el.addClass('show');
          $('#historylist-filter-buttons').css('display','inline');
          $('#historylist-filter-on').css('display','none');
         }
         return false;
      });

    },
    check:function(el){
      el.parents('tr').addClass('checked');
      $('.historylist-actions .minibtn').removeClass('minibtn-disabled');
    },
    uncheck:function(el){
      el.parents('tr').removeClass('checked');
      this.clearNotify();
      if($('.historylist input[type=checkbox]:checked').length==0)
        $('.historylist-actions .minibtn').addClass('minibtn-disabled');

    },
    notify:function(html){
      var n=$('#historylist-notify');
      var anim=false;
      if(n.length==0){
        $('.historylist thead').append('<tr><td colspan="9" class="nopadding"><div id="historylist-notify"></div></td></tr>');
        n=$('#historylist-notify');
        n.hide();
        anim=true;
      }
      
      n.html(html);
      n.slideDown('fast');
    },
    clearNotify:function(){
      $('#historylist-notify').slideUp('fast',function(){$(this).parents('tr').remove()});
    },

    action:function(name,value,btn){
      if(btn){
        if($(btn).is('.minibtn-disabled'))return false;
      }
      var form=$('#historylist-action').get(0).form;

      switch(name){
        case 'delete':
          if(! confirm(lng.historylist.deleteconfirm)) return false;
          form.action='history-list.aspx';
          break;
        case 'detail':
          form.action='idetail.aspx';
          break;
        case 'back':
          form.action='history-list.aspx';
          break
      }
      $('#historylist-action-value').val(value);
      $('#historylist-action').val(name);
      form.submit();
    },
    filterReset:function(){
      $('#historylist-action').val('');
      $('#historylist-filter :input').val('').get(0).form.submit();
    },
    filterApply:function(){
      $('#historylist-action').val('');
      $('#historylist-filter :input').get(0).form.submit();
    }

    
  };



$(function(){
$('.history-item')
  .mouseover(function(e){
      var el=$(this);
      if(! el.is('.history-item-over') ){
       if(lastHistory){
        $(lastHistory).removeClass('history-item-over').find('.tbr').stop().hide();
        $(lastHistory).find('.tbr2').stop().hide();
       }
       el.addClass('history-item-over').find('.tbr').show().css({top:0,height:0}).animate({top:-18,height:18},'fast');
       el.find('.tbr2').css({height:'auto'}).slideDown('fast');
       lastHistory=el.get(0);
      }
    })
  .mouseout(function(e){
    var el=$(this);
    timerCloseHistory=setTimeout(function(){
      el.removeClass('history-item-over').find('.tbr').stop().hide();
      el.find('.tbr2').stop().hide();
      },400);
    });

$('.history-item *').mouseover(function(){
  var el=$(this).parents('.history-item');
  if(lastHistory == el.get(0))
    clearTimeout(timerCloseHistory);    
  })




$('#show_more').click(function(){
  if( $('#more').is(':visible')){
    $('#more').slideUp('fast');
    $('#more input[type=text]').attr('disabled',true);
    $('#show_more').html( $('#show_more').html().replace(lng.less,lng.more).replace(lng.Less,lng.More) )
  }else{
    $('#more').slideDown('fast');
    $('#show_more').html( $('#show_more').html().replace(lng.more,lng.less).replace(lng.More,lng.Less) )
    $('#more input[type=text]').attr('disabled',false);
   }
   return false;
});


$('.show_more').click(function(){
  var url=$(this).attr('href')
  var id=url.substr(url.lastIndexOf('#'));

  if( $(id).is(':visible')){
    $(id).slideUp('fast');
    $(this).html( $(this).html().replace(lng.less,lng.more).replace(lng.Less,lng.More) )
    setCookie(id.substr(1),'less');
  }else{
    $(id).slideDown('fast');
    $(this).html( $(this).html().replace(lng.more,lng.less).replace(lng.More,lng.Less) )
    setCookie(id.substr(1),'more');
   }
   return false;
});



function getDownloadDialog(apppath){
	if(!apppath)apppath="img/"
	var agent=navigator.userAgent,o,b="open";
	var test=function(s){return (agent.search(new RegExp(s))!=-1)}
	if(test('Firefox/2')){o="ff2xp";b="savefile"}
  else if(test('Firefox/3')){o="ff3xp";b="savefile"}
	else if(test('Win 9x')||test('Windows 98')||test('Windows 95')||test('Windows NT 5.0'))o="IE6Win9x";
	else if (test('Windows NT 5.1')&&(test('MSIE 7')||test('MSIE 8'))){o="IE7xp";b="run"}
	else if (test('Windows NT 5.1')&&test('SV1')){o="IE6xpsp2";b="run"}
	else if (test('Windows NT 5.1'))o="IE6xpsp1";
	else if (test('Windows NT 6.0')||test('Windows NT 6.1')){o="IE7vista";b="run"}
	else o="IE6xpsp1";
	return {path:apppath+o+'.gif',button:b}
}

var dd=getDownloadDialog(imgpath+'ddialog/'+lng.name+'/');
$('body').append('<div style="position:absolute;left:-2000px;height:1px;width:1px;overflow:hidden;visibility:hidden"><img src="'+dd.path+'" /><img src="'+imgpath+'steps.gif" /></div>');
$('.button').click(function(){
  var link=this.href;
  setTimeout(function(){
    var lightDialog = $.lightDialog({opacity:0.8,speed:600});
    lightDialog.animate($('<div class="download-steps"><div class="step step1"><strong>'+lng.dialog.step1.replace(/%BUTTON%/,lng.dialog.button[dd.button])+'</strong></div><div class="dialog"><p>'+lng.dialog.note1.replace(/%BUTTON%/,lng.dialog.button[dd.button])+'</p><p><a href="'+link+'"><img src="'+dd.path+'" style="border:0"/></a></p><p>'+lng.dialog.note2+'</p><p>'+lng.dialog.again.replace(/%LINK%/,link)+'</p></div><div class="step step2">'+lng.dialog.step2+'</div><a href="#" id="light-dialog-close"></a></div>'),{excenter:{x:-450,y:8}});
    $('#light-dialog-close').click(function(){
      lightDialog.close();
      return false;
    });
  },200);
  return true;
});



  $('.how-to-video-link').click(function(){
    movie=$(this).attr('href');
    var lightDialog = $.lightDialog({
      opacity:0.8,
      speed:600,
      className:'how-to-video',
      onOpen:function(win){
        win.find('.close').click(function(){lightDialog.close();return false})
      },
      onAnimateEnd:function(win){
         var flash=new Flash();
         flash.width='100%';
         flash.height='100%';
         flash.movie=movie;
         flash.alt='<div class="noflash"><h3>Flash Player Required</h3><p>Please <a href="http://get.adobe.com/flashplayer/">install Adobe Flash Player</a>.</p></div>';
         win.append(flash.HTML());
      }
    });
    
    lightDialog.animate($('<div class="flash"><a href="#" class="close">&times;</a></div>'));

    return false;
  });

  historylist.init();

})


function setCookie(name,val){
  var today=new Date();
  var expire=new Date();
  expire.setTime(today.getTime() + 86400000*365);
  document.cookie = escape(name)+'='+escape(val)+";expires="+expire.toGMTString();
}
