function exists(a){
	if(a==null)return false;

	switch(typeof(a)){
		case 'string': return (a!='');
		case 'number': return (a!=0);
		case 'object': 
			if(a.length!=undefined) return a.length>0;
			else{
				for(var b in a)return true;
				return false;
			}
		default: return a;
	}
}


function miniwin(el,w,h){
  w = w || 600;
  h = h || 400;
  window.open(el.href,'_blank','width='+w+',height='+h+',resizable=1,status=0,menubar=0,scrollbars=1');
  return false
}

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> Downloading may take a few minutes depending on your Internet connection speed.  <strong>Please&nbsp;be&nbsp;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;'
   }
}




/*old frm*/
function CN(el,name){var i,list;if(el.className){list=el.className.split(" ");for(i=0;i<list.length;i++)if(list[i]==name)return true}return false}
function rCN(el,name,c){var i,j,curList,newList;if(el.className=="")return;newList=new Array();curList=el.className.split(" ");j=0;for(i=0; i < curList.length;i++)if(curList[i]!=name)newList[i-j]=curList[i];else j++;if(c!=1)el.className=newList.join(" ");else return newList.join(" ")}
function aCN(el,name){if((el.className=="")||(el.className==" "))el.className=name;else el.className += " "+name}
function gH(el){var href;href=el.getAttribute("href");return href.substr(href.lastIndexOf("#")+1)}
function gET(el,n){var a=el.getElementsByTagName(n);if(a.length>0)return a;else return false}
function gEI(i){return document.getElementById(i)}
function gEC(el,class_name,tag_name){var all_obj,ret_obj=new Array(),j=0;if(tag_name==null||tag_name=="")tag_name="div";all_obj=el.getElementsByTagName(tag_name);for(i=0;i<all_obj.length;i++){  if(CN(all_obj[i],class_name)){   ret_obj[j]=all_obj[i];   j++;}}if(ret_obj.length>0)return ret_obj; else return false;}
function gP(el){if(el.offsetParent){var x=0;var y=0;while(el){x += el.offsetLeft;y += el.offsetTop;el=el.offsetParent}return [x,y]}else return [el.x, el.y]}
function gE(e) {return (window.event && window.event.srcElement)?window.event.srcElement:(e && e.target)?e.target:false;}
function gPI(el,name){while (el!=null&&el.parentNode!=null&&el.id!=name){el=el.parentNode}if(el!=null&&el.parentNode!=null)return el;else return false;}
function gPT(el,name) {while (el !=null && el.parentNode != null && el.tagName && el.tagName.toLowerCase() != name) {el = el.parentNode;}if (el !=null && el.parentNode != null)return el;else return false;}
var IE=((navigator.userAgent.toLowerCase().indexOf("opera")==-1)&&navigator.appName=="Microsoft Internet Explorer")?true:false;
if (IE){s=navigator.appVersion;a = s.indexOf("MSIE");b = s.indexOf(";",a);var IEversion = parseFloat(s.substring(a+4,b))}


function writeUL(open){var o=goArray(treeDATA,open);document.write('<ul class="tree">'+o+'</ul>');var a=gEI('act');if(a){var u=gPT(a,'ul');if(u){aCN(u,'open');var l=gPT(u,'li');if(l){var b=gET(l,'a');if(b)aCN(b[0],'open');}}}}
function goArray(a,open){var o='';for(var i=0;a[i];i++){o+='<li>';var oc=(a[i][0]==open)?'open':'';var ac=(a[i][0]==open)?'act':'';var href=treeURL(a[i][0],a[i][1]);if(!a[i][2])o+='<span class="ico leaf"></span>';else o+='<a class="ico '+oc+' '+ac+'" onclick="return treeCLK(event,this)" href='+href+' onmouseover="return treeTXT(this)" onmouseout="window.status='+"''"+';return true"></a>';o+=(ac=='')?'<a href="'+href+'">':'<strong id="'+ac+'" class="'+ac+'">';o+=a[i][1];o+=(ac=='')?'</a>':'</strong>';if(a[i][2])o+='<ul class="'+oc+'">'+goArray(a[i][2],open)+'</ul>';o+='</li>';}return o}
function treeURL(a,b){return catURL.replace(/vvIDvv/,a).replace(/vvNAMEvv/,b.replace(/&nbsp;/g," ").replace(/\&amp;/g,"and").replace(/\s/g,"-").replace(/\'/g,"-").replace(/\&/g,"-"))}
function treeTXT(el){var a=(CN(el,'open'))?'close':'open';window.status='Click to '+a+' this category.';return true}
function treeCLK(e,el){var l=gPT(el,'li');var u=gET(l,'ul');if(CN(el,'open')){rCN(el,'open');if(u)rCN(u[0],'open')}else{aCN(el,'open');if(u)aCN(u[0],'open')};return false}


/**/

var RI_Events={
  IsIE:(navigator.userAgent.toLowerCase().indexOf("windows")!=-1&&navigator.userAgent.toLowerCase().indexOf("opera")==-1&&navigator.appName=="Microsoft Internet Explorer")?true:false,
  IsFF:(navigator.userAgent.toLowerCase().indexOf("windows")!=-1&&navigator.userAgent.toLowerCase().indexOf("firefox")!=-1&&"createEvent" in document)?true:false,
  IsInstalled:false,
  IsInstallAble:false,
  /*
  Init, need to be run while page is rendering
  */
  Init:function(){
    if(this.IsIE){
      document.write("<" + "OBJECT classid='clsid:8736C681-37A0-40C6-A0F0-4C083409151C' ID=RI_BHO width=0 height=0 style='position:absolute;left:-2000px'></OBJECT>")
    }
    this.IsInstalled=this._IsInstalled();
    this.IsInstallAble=(this.IsIE||this.IsFF);
  },
  /*
  main comunication function
    Argument 0:    tbr object method name
    Arguments 1-N: method arguments

  */
  Get:function(){
    if(!this.IsInstalled)return null;
    if(this.IsIE){
      var a=[];
      for(var i=1;i<arguments.length;i++){
        a.push("'"+arguments[i]+"'")
      };
      try{
        return eval("RI_BHO."+arguments[0]+"("+a.join(",")+")");
      }
      catch(e){
        return null
      }
    }
    else if(this.IsFF){
      var el=document.createElement("RebateInf_Get");
      for(var i=0;i<arguments.length;i++)
        el.setAttribute("Attribute"+i,arguments[i]);
      document.documentElement.appendChild(el);
      var evt = document.createEvent("Events");
      evt.initEvent("RebateInf_Get", true, false);
      el.dispatchEvent(evt);
      return el.getAttribute("return");
    }
    else return null;
  },
  /*
  internal function
  */
  _IsInstalled: function(){
    var result, name='REBATEINF';
    this.IsInstalled=true;

    if(this.IsIE){
      var r=this.Get('PluginState',name);
      result=(r==0 || r==4);
    }
    else if(this.IsFF){
      result=(!(this.Get('GetFileVer')==null));
    }
    else result=false;
    return result;
  }
};
RI_Events.Init();




$(function(){
 $('a.what').tooltip({alwaysTop:true,dartInCenter:true,openOnClick:true,textGetter:{type:'id'}});
    $.ifixpng(data.apppath + data.imgpath + 'pixel.gif');
    $('img[src$=.png]').ifixpng();

    $('.tree a ~ a').mouseover(function(){var link = $(this);treemenuTimer = setTimeout(function(){link.siblings('a').trigger('click');link=null;}, 200);});
    $('.tree a ~ a').mouseout(function(){clearTimeout(treemenuTimer);});

    var megamenuLast = null;
    $('.menu a').mouseover(function(){var link = $(this);megamenuTimer = setTimeout(function(){openMegamenu(link);link = null},200);});
    $('.menu a').mouseout(function(){clearTimeout(megamenuTimer);});
    $('.menu-block').mouseleave(function(){setTimeout(function(){closeMegamenu(true);},200);});
    $('.megamenu').mouseover(function(){$(this).show('fast');});

    $('.megamenu').css('top', $('.menu').get(0).offsetTop + $('.menu').height());$('.megamenu-close').click(function() {closeMegamenu(true);return false;});
    $('.megamenu').css('top',$('.menu').get(0).offsetTop+$('.menu').height());

  $('.megamenu-close').click(function(){
    closeMegamenu(true);
    return false;
  });

  $(document).click(function(e){
    var target = (e.srcElement)?e.srcElement:e.target;
    
    if(!$(target).is('.megamenu') && $(target).parents('.megamenu').length==0){
      closeMegamenu(true);
    }
  });
  
  function openMegamenu(id) {
        var link = $(id);
        var menu = $('#mega' +link.attr('id'));
        if (megamenuLast != null) {
            closeMegamenu();
            menu.show();

        }
        else {
            menu.show('fast');
            $('.menu').addClass('menu-open');
        }

        link.addClass('megamenu-open');
        megamenuLast = menu;
    }

  function closeMegamenu(last){
    if(megamenuLast!=null){
      megamenuLast.hide(last ? 'fast':null);

      if(last)
        $('.menu').removeClass('menu-open');
       var link_id = megamenuLast.attr('id').replace('mega', '');
       $('.menu a#'+link_id).removeClass('megamenu-open') ;
      megamenuLast=null;
    }
  }

 function formatDate(date){
     return (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear() + ' ' + date.getHours() + ':' + ((date.getMinutes() <= 9) ? '0'+date.getMinutes() : date.getMinutes());
 }

  if($('#actual-datetime').length > 0){

    var d = new Date();
    $('#actual-datetime').html(formatDate(d));
    setInterval(function(){
      var d = new Date();
      $('#actual-datetime').html(formatDate(d));
    },60000);d = null;
  }

  if(exists(data.slider)){
    var width=88+16;
    var si=$('<div id="slider-in2"></div>');
    var addRebate=function(rebate){
      var link=rebate.link;
      if(! /^[a-z]+:\/\//.test(link))
        link=data.apppath+link;
      si.append('<a class="rebate" href="'+link+'" title="'+rebate.name+'<br><strong>'+rebate.label+'</strong>" '+(data.logged ? 'target="_blank"':'')+' '+( (typeof(rebate.track)=='string')?rebate.track:'')+'><img src="'+data.apppath+data.cmspath+rebate.image+'" />'+rebate.label+'</a>');
    }

    for(var i=0;i<data.slider.length;i++){
       addRebate(data.slider[i]);
    }
    var len=data.slider.length;
    
    var left=(data.slider.length)%5;
    if(left!=0){
      for(var i=0;i<5-left;i++){
         addRebate(data.slider[i]);
      }
      len=len+5-left;
    }

    var s=$('<div id="slider"></div>');
    $('<div id="slider-in"></div>').append(si).appendTo(s);

    if(data.slider.length>5){

      for(var i=0;i<5;i++){
         addRebate(data.slider[i]);
      }
      si.css('width',(len+5) * width);
    
      s.hover(function(){
        $(this).addClass('slider-hover');
      },
      function(){
        $(this).removeClass('slider-hover');
      });

      $('<a href="#" class="slider-toleft"></a>').appendTo(s).click(function(){
        var a=$('#slider-in').get(0);
        if(a.scrollLeft==0)a.scrollLeft=a.scrollWidth;
        $('#slider-in').scrollTo({top:0,left:'-=520px'},{speed:400});
        return false;
      });

      $('<a href="#" class="slider-toright"></a>').appendTo(s).click(function(){
        var a=$('#slider-in').get(0);
        if(a.scrollLeft==a.scrollWidth-520)a.scrollLeft=0;

        $('#slider-in').scrollTo({top:0,left:'+=520px'},{speed:400});
        return false;
      });

      $('a',si).tooltip({dartInCenter:true})

     
    }
    else{
      $('<span class="slider-toleft"></span>').appendTo(s);
      $('<span class="slider-toright"></span>').appendTo(s);
    }

    $('#head').append(s);

    
    var more_text=['more','less'];
    $('#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(more_text[1],more_text[0]) )
      }else{
        $('#more').slideDown('normal');
        $('#show_more').html( $('#show_more').html().replace(more_text[0],more_text[1]) )
        $('#more input[type=text]').attr('disabled',false);
       }
       return false;
    });


  }


  $('#type_check').click(function(){
    if(this.checked){
      $('#type_check_fields').slideDown('fast');
      $('#type_paypal_fields').slideUp('fast');
    }
  });

  $('#type_paypal').click(function(){
    if(this.checked){
      $('#type_check_fields').slideUp('fast');
      $('#type_paypal_fields').slideDown('fast');
    }
  });


  if(typeof(countryList)!= 'undefined')
  	$('#cmbCountry').add('#cmbState').chainedSelects( countryList, { nullOption:[' ','[select state]'],grouping:[1,1] })


  $('#cmbCountry').change(function(){
    if($('#cmbState').get(0).disabled){
      $(this).parents('td').removeClass('half');
      $('#cmbState').parents('td').hide();
    }
    else{
       $(this).parents('td').addClass('half');
      $('#cmbState').parents('td').show();
    }
  }).change();

  $('.search2 .textinput').bind('change',function(){
    if($(this).val() == '')
      $(this).removeClass('textinput-filled');
    else
      $(this).addClass('textinput-filled');
  }).trigger('change');

  $('.search2 .textinput').focus(function(){
    $(this).addClass('textinput-filled');
  });

  $('.search2 .textinput').blur(function(){
    if($(this).val() == '')
      $(this).removeClass('textinput-filled');
  });


	var dd=getDownloadDialog(data.apppath+data.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="'+data.apppath+data.imgpath+'steps.gif" /></div>');
	$('a[href$=.exe]').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;
	});


  var banner={
    act:0,
    timer:false,
    cont:null,
    parent:$('#adrotator'),
    playstop:null,
    first:null,
    options:{
      data:[],
      speed:1500,
      scrollSpeed:400
    },

    init:function(options){

      this.options = $.extend(this.options,options);

      this.parent.css({overflow:'hidden',position:'relative',width:'100%'});

      this.cont=$('<div />').css({width:'9000px',position:'relative'}).appendTo(this.parent);
      this.set(0);

      if(this.options.data.length>1){

        this.parent.after('<div id="adrotator-navi" />');

        $('<a href="#" class="adrotator-prev"></a>').click(function(){
          banner.set(-1);
          banner.stop();
          return false;
        }).appendTo('#adrotator-navi');

        $('<a href="#" class="adrotator-next"></a>').click(function(){
          banner.set(1);
          banner.stop();
          return false;
        }).appendTo('#adrotator-navi');
        
        this.playstop=$('<a href="#" class="adrotator-play"></a>').click(function(){
          if(this.className=="adrotator-play"){
            banner.stop();
          }
          else{
            banner.play();
          }
          return false;
        }).appendTo('#adrotator-navi');
        this.play();
      }

      
    },

    set:function(step){
      callback=null;
      var that=this;
      var cur=this.act;
      this.act=this.number(this.act+step);
  
      var pos=this.parent.width()*this.act;
      this.preload(this.act);

      var lastpos=this.parent.width()*this.options.data.length;

      if(this.act==0 && step==1){
        pos=lastpos;
        this.first.css('left',lastpos);
        callback=function(){that.first.css('left',0);that.parent.get(0).scrollLeft=0};
      }

      if(cur==0 && step==-1){
        this.first.css('left',lastpos);
        this.parent.get(0).scrollLeft=lastpos;
        callback=function(){that.first.css('left',0)};
      }
 
      this.parent.scrollTo({top:0,left:pos},this.options.scrollSpeed,callback);
      this.preload(this.number(this.act+ (step ==0 ? 1:step )));

    },

    preload:function(n){
      if(!this.options.data[n].cache){
        var tab=$('<div />').css({position:'absolute',width:this.parent.width(),height:this.parent.height(),left:this.parent.width()*n+'px'}).html(this.options.data[n].html).appendTo(this.cont);
        if(n==0){
          this.first=tab;
        }
        this.options.data[n].cache=true;
      }
    },

    number:function(a){
      if (a < 0) return this.options.data.length-1;
      if (a > this.options.data.length-1) return 0;
      return a;
    },

    play:function(){
      this.timer=setInterval(function(){
        banner.set(1);
      },this.options.speed);
      this.playstop.get(0).className='adrotator-play';
    },
    stop:function(){
      clearInterval(this.timer);
      this.timer=false;
      this.playstop.get(0).className='adrotator-stop';

    }
  };


  if(exists(data.banner)){
    banner.init(data.banner);    
  }



});

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}
}

$(function(){

  var footerAdd = {
    box: null,
    button: null,
    show_always: false,
    not_installed: (RI_Events.IsInstallAble && !RI_Events.IsInstalled),
    stay_hidden: $.cookie('hide_informer') == null ? false : true,
    target: $('body'),

    options: {
      banner_src: '',
      banner_url: '',
      delay: 4000,
      hidden_days: 2
    },

    create:function(options){
      this.options = $.extend(this.options,options);
      this.box = $('<div id="footer-adds">').css({display:'none'});
      var content = $('<div class="content" />').appendTo(this.box);
      this.button = $('<a id="close-add" href="#"></a>');
      this.button.click(function(){
        footerAdd.hide();
        footerAdd.set_disabled(footerAdd.options.hidden_days);
        return false;
      }).appendTo(content);
      var banner_url = $('<a />').attr('href', this.options.banner_url).appendTo(content)
          banner_url.click(function(){footerAdd.track_event('banner_click');})
      var banner_img = $('<img />').attr('src', this.options.banner_src).appendTo(banner_url);

      this.box.appendTo(this.target);
    },

    show: function(delay){
      if(this.show_always){
        this.box.slideDown(delay || 800);
      }else{
        if(this.not_installed && !this.stay_hidden){
          this.box.slideDown(delay || 800);
          this.track_event('banner_view');
        }
      }
    },
    hide: function(time){
      this.box.slideUp(time || 900);
      this.track_event('banner_close');
    },
    set_disabled: function(days){
      $.cookie('hide_informer', 'true', {expires: days, path: '/'})
    },
    show_delayed: function(delay){
      window.setTimeout(function(){footerAdd.show();},delay)
      footerAdd.stay_bottom_IE6();
    },
    track_event:function(event){
      try{pageTracker._trackEvent('HP_RI_banner',event);}catch(e){}
    },
    stay_bottom_IE6:function(){
      if(navigator.userAgent.toLowerCase().indexOf("msie 6") !=-1){
        $(window).scroll(function() {
          if(footerAdd.box.css('display') != 'none'){
              footerAdd.box.css('top', ( ( $(this).scrollTop() + $(this).height() ) -120 ) + "px");
          }
        });
      }
    },
    init: function(options){
      this.create(options);
      this.show_delayed(this.options.delay);
    }
  }
  if(exists(data.footerAdd)){
    footerAdd.init(data.footerAdd);
  }
});

