var DACW = {
  aSanitize: ["<iFraMe",
              "</iFraMe>",
              "<iframe",
              "</iframe>",
              "</form>",
              "<title",
              "</title",
              "<script",
              "</script>",
              "<%=",
              "%>",
              "alert(<",
              "alert(",
              "?",
              "</",
              '"',
              "<",
              ">",
              ")",
              "(",
              "*",
              "'",
              "`",
              "declare",
              "varchar",
              "exec",
              "width=",
              "height=",
              "src=",
              "IfRamE"],
              
  Search: {
    'Form': null,
    'Submit': null,
    'AdvancedForm': null,
    'AdvancedSubmit':null
  },
  
  init: function(){    
    DACW.Search.Form = $('FormSearch');
    DACW.Search.Submit = $('btnSearchSubmit');
    DACW.Search.AdvancedForm = $('bdySearchForm');
    DACW.Search.AdvancedSubmit = $('btnBodySearchSubmit');
    
    
    $$("img.mid-ad-banner").each(function(imgBanner){
      imgBanner.addEvents({
          'mouseover': function(){
              imgBanner.set('opacity',0.75)
          },
          'mouseout': function(){
              imgBanner.set('opacity',1)
          }
      });
    });
    
    var BannerTips = new Tips('.mid-ad-banner-tip',{'className':'banner-tooltip'});
    
    DACW.ButtonStates([{'Name':DACW.Search.Submit,'Image':'TopSearchButton'},{'Name':$('btnLogIn'),'Image':'LogButton'}]);

    if (DACW.Search.Form){
      DACW.Search.Form.addEvent('submit',function(event){
        event.stop();
        
        document.location = DACW.Search.Form.get('action')+"?Search="+escape($('txtSearch').get('value').replace(/ /gi, "+"));
      });
    };

    if (DACW.Search.AdvancedForm){
      DACW.Search.AdvancedForm.addEvent('submit',function(event){
        event.stop();
        AdvancedSearchVars = {
          'MinPrice': $('cBot').get('value').replace(/,/,"").toInt(),
          'MaxPrice': $('cTop').get('value').replace(/,/,"").toInt(),
          'Search': escape($('txtBSearch').get('value').replace(/ /gi, "+")),
          'Category': $('cCat').get('value').toInt(),
          'Type': $('cType').get('value').toInt(),
          'Descript': $('llDescript').get('checked')
        },
        
        isNaN(AdvancedSearchVars.MinPrice) ? AdvancedSearchVars.MinPrice=0 : AdvancedSearchVars.MinPrice=AdvancedSearchVars.MinPrice;
        isNaN(AdvancedSearchVars.MaxPrice) ? AdvancedSearchVars.MaxPrice=0 : AdvancedSearchVars.MaxPrice=AdvancedSearchVars.MaxPrice;
               
        nCheck = /(^\d+$)|(^\d+\.\d+$)/;
        lEmpty = (AdvancedSearchVars.Search=="" || AdvancedSearchVars.Search=="+");
        lValid = (nCheck.test(AdvancedSearchVars.MinPrice) && nCheck.test(AdvancedSearchVars.MaxPrice));    

        if(!lValid){
          alert("The Min and Max values must be numeric (example: 1.00,1,or 0.10)");
        }else if(lEmpty){
          alert("Please fill in a search keyword.");
        }else{ //!Empty && lValid
          cSearchURL = DACW.Search.AdvancedForm.get('action');
          
          AdvancedSearchVars.Search=="" ? cSearchURL=cSearchURL : cSearchURL=cSearchURL+'&Search='+AdvancedSearchVars.Search;
          AdvancedSearchVars.Category==0 ? cSearchURL=cSearchURL : cSearchURL=cSearchURL+'&cCat='+AdvancedSearchVars.Category;
          AdvancedSearchVars.Type==1 ? cSearchURL=cSearchURL : cSearchURL=cSearchURL+'&cType='+AdvancedSearchVars.Type;
          AdvancedSearchVars.MinPrice==0 ? cSearchURL=cSearchURL : cSearchURL=cSearchURL+'&cBot='+AdvancedSearchVars.MinPrice;
          AdvancedSearchVars.MaxPrice==0 ? cSearchURL=cSearchURL : cSearchURL=cSearchURL+'&cTop='+AdvancedSearchVars.MaxPrice;
          AdvancedSearchVars.Descript==false ? cSearchURL=cSearchURL : cSearchURL=cSearchURL+'&llDes=t';
          
          document.location = cSearchURL;
        };
      });
    };

    aWishListLinks = $$('a.wishdelete');
    if (aWishListLinks){
      aWishListLinks.each(function(cLink){
        cLink.addEvent('click',function(event){
          event.stop();
          
          if (confirm('Are you sure you want to delete this list?')){
            window.location.href = cLink.get('href');
          };
          
        });
      });
    };

		$$('#search form input.txtSearch').set('value','Search').setStyles({
			'text-align': 'right',
			'color': '#CCCCCC',
			'font-weight': 'bold'
		}).addEvent('click',function(){
			if (this.get('value')=='Search'){
				this.set('value','').setStyles({
					'text-align': 'left',
					'color': '#000000',
					'font-weight': 'normal'
				});
			};
		});

  },

  Sanitize: function(cString){
    DACW.aSanitize.each(function(Character,index){
      cString = cString.replace(Character,'');
    });
    
    return cString;
  },
  
  ClearForm: function(oForms){
    oForms.each(function(oClear,index){
      if (oClear.form){
        oClear.form.addEvent('submit', function(event){
          event.stop();
          document.location = oClear.form.get('action');
        });
      };
    });
  },
  
  ButtonStates: function(aButtons){
    aButtons.each(function(Button,index){
        if (Button.Name){
          Button.Name.addEvents({
              'mouseover': function(){
                  Button.Name.setStyle('background','#666666 url(//www.dacardworld.com/images/v2/forms/'+Button.Image+'.png) no-repeat 0px -'+Button.Name.getStyle('height').toInt()+'px');
              },
              'mouseleave': function(){
                  Button.Name.setStyle('background','#666666 url(//www.dacardworld.com/images/v2/forms/'+Button.Image+'.png) no-repeat 0px 0px');
              },
              'mousedown': function(){
                  Button.Name.setStyle('background','#666666 url(//www.dacardworld.com/images/v2/forms/'+Button.Image+'.png) no-repeat 0px -'+(Button.Name.getStyle('height').toInt()*2)+'px');
              }
          });
      };
    });
  }  
};

var DACWSiteNavigation = {
	Left: {
		'Children':null,
		SetBackground: function(oElement,cMode){
			if(cMode=='out'){
				oElement.retrieve('ParentLink').setStyle('background','url(//www.dacardworld.com/images/v2/layout/content-left-menu-bg.png) repeat-x 0px 0px');
			}else{
				oElement.retrieve('ParentLink').setStyle('background','url(//www.dacardworld.com/images/v2/layout/content-left-menu-bg.png) repeat-x 0px -25px');
			};
		},
		Toggle: function(oElement){
			if(oElement.retrieve('open')==true){
				oElement.slide('out');
				oElement.store('open',false);
				DACWSiteNavigation.Left.SetBackground(oElement,'out');
			}else{
				oElement.slide('in');
				oElement.store('open',true);
				DACWSiteNavigation.Left.SetBackground(oElement,'in');
			};
		}
	},
	
	Top: {
	  'Children':null	
	},
	
	init: function(cCurrentPage){			
		DACWSiteNavigation.Top.Children = $$('ul.horizontalmenu li ul');
		
		DACWSiteNavigation.Top.Children.each(function(ULElement){
			ULElement.getParent().addEvents({
				'mouseover': function(){
				  if(ULElement.getParent().getFirst('a').hasClass('first') || ULElement.getParent().getFirst('a').hasClass('firstcurrent')){
  					ULElement.getParent().getFirst('a').setStyles({
  					  background: 'url(/images/v2/layout/top-navigation-hover-bg-first.png) no-repeat left',
  					  color: '#FFFFFF'
  					});
				  }else{
  					ULElement.getParent().getFirst('a').setStyles({
  					  background: 'url(/images/v2/layout/top-navigation-hover-bg.png) no-repeat right',
  					  color: '#FFFFFF'
  					});
  				};  				
  				$('mid-navigation').setStyle('z-index','-1');
  			  				
  				ULElement.getParent().getFirst('a').getFirst('img').setProperty('src','/images/v2/layout/top-nav-dingy-hover.gif');
  				
					ULElement.setStyles({
						display:'block'
					});
				},
				'mouseout': function(){
				
        if(ULElement.getParent().getFirst('a').hasClass('current') || ULElement.getParent().getFirst('a').hasClass('firstcurrent')){
            ULElement.getParent().getFirst('a').getFirst('img').setProperty('src','/images/v2/layout/top-nav-dingy-hover.gif');
				  }else{
  					ULElement.getParent().getFirst('a').setStyles({
  					  background: 'url(/images/v2/layout/top-navigation-link-bg.png) no-repeat right',
  					  color: '#0066CC'
  					});
  					
  					ULElement.getParent().getFirst('a').getFirst('img').setProperty('src','/images/v2/layout/top-nav-dingy.gif');
				  };
				    				
				  
  				$('mid-navigation').setStyle('z-index','0');
					ULElement.setStyles({
						display: 'none'
					});
				}
			});
		});
		
		DACWSiteNavigation.Left.Children = $$('ul.child');
		
		DACWSiteNavigation.Left.Children.each(function(Child){
 			Child.slide('hide');
 			Child.store('open',false);
 			
			Child.store('ParentLink',Child.getParent().getParent().getChildren('a'));
			Child.store('ParentMoreLink',$$(Child.retrieve('ParentLink').getChildren('em')));
			
			if (Child.retrieve('ParentLink').get('id')==cCurrentPage){
				Child.slide('show');
				Child.store('open',true);
				DACWSiteNavigation.Left.SetBackground(Child,'in');
			};
			
			Child.retrieve('ParentLink').addEvents({
				'click':function(e){
					if(this.hasClass('sub')){
						e.stop();
						DACWSiteNavigation.Left.Children.each(function(ele){
							if(ele.retrieve('open')==true && !(ele==Child)){
								DACWSiteNavigation.Left.Toggle(ele);
							};
						});
						DACWSiteNavigation.Left.Toggle(Child);
					};
				},
				'mouseover':function(e){
					DACWSiteNavigation.Left.SetBackground(Child,'in');
				},
				'mouseout':function(e){
					if(Child.retrieve('open')==false){
						DACWSiteNavigation.Left.SetBackground(Child,'out');
					};
				}
			});
			
			Child.retrieve('ParentMoreLink').addEvents({
				'click':function(e){
					e.stop();
					DACWSiteNavigation.Left.Children.each(function(ele){
						if(ele.retrieve('open')==true && !(ele==Child)){
							DACWSiteNavigation.Left.Toggle(ele);
						};
					});
					DACWSiteNavigation.Left.Toggle(Child);
				},
				'mouseover':function(e){
					DACWSiteNavigation.Left.SetBackground(Child,'in');
				},
				'mouseout':function(e){
					if(Child.retrieve('open')==false){
						DACWSiteNavigation.Left.SetBackground(Child,'out');
					};
				}
			});
		});
	}
};

window.addEvent('domready', function() {
 	DACW.init();
  
	var aRotators = $$('.parent-rotator-child');
	var aRotatorLinks = $$('#parent-rotator-nav li a');
	
	if (aRotators.length>0 && aRotatorLinks.length>0){
		aRotators.setStyle('display','none');
		aRotators[0].setStyle('display','block');
		aRotatorLinks.setStyles({
        'background': "#000099 url(http://www.dacardworld.com/images/v2/layout/RotatorImages.png) repeat-x 0px -314px",
        'color': "#0066CC"
      });
      
		aRotatorLinks[0].setStyles({
        'background': "#000099 url(http://www.dacardworld.com/images/v2/layout/RotatorImages.png) repeat-x 0px -364px",
        'color': "#CC0000"
      });
		
		aRotatorLinks.addEvents({
			'click':function(e){
				e.stop();	      
	      rotatorToggle(aRotators,aRotatorLinks,aRotatorLinks.indexOf(this));
			},
			'mouseover':function(e){	      
	      rotatorToggle(aRotators,aRotatorLinks,aRotatorLinks.indexOf(this));
			}
		}); 
	};
});

function rotatorToggle(Rotators,Links,nCurrent){
  Rotators.each(function(Banner,nindex){
    if(Banner.getStyle('display')=='block'){
      Banner.setStyle('display','none');
      Rotators[nCurrent].setStyle('display','block');
      
      Links[nindex].setStyles({
        'background': "#000099 url(http://www.dacardworld.com/images/v2/layout/RotatorImages.png) repeat-x 0px -314px",
        'color': "#0066CC"
      });
      Links[nCurrent].setStyles({
        'background': "#000099 url(http://www.dacardworld.com/images/v2/layout/RotatorImages.png) repeat-x 0px -364px",
        'color': "#CC0000"
      });
    };
  });
};

function submitonce(theform){
	//if IE 4+ or NS 6+
	if (document.all||document.getElementById){
	//screen thru every element in the form, and hunt down "submit" and "reset"
		for (i=0;i<theform.length;i++){
			var tempobj=theform.elements[i]
			if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset"){
			//disable em
  			tempobj.disabled=true
			};
		};
	};
};