var resizeBg = function(){
  var h = self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  var w = self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  var elem = document.getElementById('backgroundImage');
  if(w > h){
  elem.width = w;
  elem.height =w;
  }else{
  elem.height = h;
  elem.width = h;
  }
  /*$('full_width_container').setStyle('height', h);*/
} 

var MenuItem = new Class({

	inbutton: false,
	inpopup: false,

	initialize: function(linkbutton, popup){
	
		this.linkbutton = linkbutton.getElement("img");
        	this.linkbutton.setStyle('display', 'block');
        	this.linkbutton.setStyle('opacity', 0.0001);		
        	
		this.popup = popup;
		
		if($chk(this.popup))
		{
			this.addPopupEvents();
		}
		this.addButtonEvents();
	},
	
	addPopupEvents: function(){
	
		this.popup.addEvent("mouseenter", function(e) {
			this.inpopup = true;
			this.showinfo();
		}.bind(this));
		this.popup.addEvent("mouseleave", function(e) {
			this.inpopup = false;
			this.hideinfo();
		}.bind(this));
	
	},
	
	addButtonEvents: function(){
		this.linkbutton.addEvent("mouseenter", function(e) {
			this.inbutton = true;
			this.showinfo();
		}.bind(this));
		this.linkbutton.addEvent("mouseleave", function(e) {
			this.inbutton = false;
			this.hideinfo();
		}.bind(this));
	},
	
	showinfo: function(){
	
		if(this.inpopup || this.inbutton)
		{		
			if($chk(this.popup))
			{
				this.popup.setStyle("display", "block");
			}
			this.linkbutton.setStyle("opacity", 1);
		}
	
	},
	
	hideinfo: function(){
	
		if(!this.inpopup && !this.inbutton)
		{	
			if($chk(this.popup))
			{
				this.popup.setStyle("display", "none");		
			}
			this.linkbutton.setStyle("opacity", 0.0001);
		}
	
	}

});


function prepare_menu_rollovers() {
    $$('#primary_menu_container a.mainlk').each(function(item, index) {
    
        nextSibling = item.getNext();
    
    	if($chk(nextSibling) && nextSibling.get('tag') == "div")
    	{
    		new MenuItem(item, nextSibling);
    	}
    	else
    	{
    		new MenuItem(item, null);
    	}
    
    });;
}

function top_nav_setup() {

	$$('ul.menu_jsreplacement li a').each(function(item, index) {
		var myString = item.get("text");
		myString = myString.replace(/ (Sunglasses|Watches|Jewellery|Goggles|Accessories)/, "");
		myString = myString.replace(/(LTD)/, "LTD Watches");
		
		item.set("text", myString);			
	}) ;	
}

function reduce_nav_type_text() {
	


	$$('#nav_container ul li a').each(function(item, index) {
		var myString = item.get("text");
		myString = myString.replace(/ (Sunglasses|Watches|Jewellery|Goggles|Accessories)/, "");
		myString = myString.replace(/(LTD)/, "LTD Watches");
		
		item.set("text", myString);			
	}) ;
	
	
}

function reduce_sale_nav_type_text() {
	
	$$('#nav_container ul li a').each(function(item, index) {
		var myString = item.get("text");
		myString = myString.replace(/ (sale)/, "");
		myString = myString.replace(/ (Sunglasses|Watches|Jewellery|Goggles|Accessories)/, "");
		
		item.set("text", myString);			
	}) ;
	
	$$('#nav_container ul li.rem-menu-link').each(function(item, index) {		
		item.setStyle("display", "none");			
	}) ;	
}

function setupHover(_popup, _hover_name) {
	  $(_hover_name).addEvent('mouseover', function() {
		  _popup.show(this);
		  return false;
	  });
	  
	  $(_hover_name).addEvent('click', function() {
		  _popup.show(this);
		  return false;
	  });
	}

function setupClickHover(_popup, _hover_name) {
	  $(_hover_name).addEvent('click', function() {
		  _popup.show(this);
		  return false;
	  });
	}

	var _hovers;

window.addEvent('domready', function() {
	resizeBg();
	top_nav_setup();
	if ($defined($('aCurrencies'))) {
		currenciesPopup = new PopupTip($('currenciesForm'), 'Currencies', 115, 'currencybottom', 207, true);
		setupClickHover(currenciesPopup, 'aCurrencies');
	}
	
	$$('BODY').addEvent('scroll', function(e){ 
		e = new Event(e);
		e.target = $('full_width_container');
		//e.stop();
		
	});
		
});

//resize background on window resize
window.addEvent('resize', function() {
	resizeBg();
});

