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}


/**/






$(function(){
  $('a.what').tooltip({alwaysTop:true,dartInCenter:true,openOnClick:true,textGetter:{type:'id'}});

	$.ifixpng(data.apppath+data.imgpath+'pixel.gif');

  $('img[src$=.png]').ifixpng();

  var megamenuLast=null;
  $('.menu a[href^=#megamenu-]').click(function(){
    var el=$($(this).attr('href'));

    if(megamenuLast!=null){
       closeMegamenu();
       el.show();

    }
    else{
    	el.show('fast');
      $('.menu').addClass('menu-open');
    }

    $(this).addClass('megamenu-open');
    megamenuLast=el;
    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 closeMegamenu(last){
    if(megamenuLast!=null){
      megamenuLast.hide(last ? 'fast':null);

      if(last)
        $('.menu').removeClass('menu-open');
      $('.menu a[href^=#'+megamenuLast.attr('id')+']').removeClass('megamenu-open');
      megamenuLast=null;
    }
  }

  if($('#actual-datetime').length > 0){
    setInterval(function(){
      var d=new Date();
      $('#actual-datetime').html(d.toLocaleString());
    },100);
  }

  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"':'')+'><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}
}
