if(typeof console == 'undefined'){
	console = {log:function(){}};
}

function gridBG(){
	var body = $(window).getSize();
	var grid = $('grid').getSize();

	if(grid.y < body.y || grid.y > body.y){
		$('grid').setStyles({
			'height': body.y
		});
	}
	
	
	
}
window.addEvent('domready',gridBG);
window.addEvent('resize',gridBG);


/*
	
*/
function pdpSwap(){
	if($$('.item_thumb').length > 0 && $$('.downloadFileSelection').length > 0){
		var thumbs = $$('.item_thumb');
		var downloads = $$('.downloadFileSelection');
		
		// INITIALIZE LOAD STATE
		downloads.addClass('hide');
		downloads[0].removeClass('hide');
		thumbs[0].addClass('active');
		
		// ADD EVENTS
		thumbs.each(function(el,i,group){
			el.addEvent('click',function(e){
				new Event(e).stop();
				
				if(el.hasClass('active')) return false;
				
				$('pdpBigImage').set('src',el.get('rel'));
				
				thumbs.removeClass('active');
				el.addClass('active');
				
				downloads.addClass('hide');
				downloads[i].removeClass('hide');
				
			});			
		});
				
	}
}
window.addEvent('domready',pdpSwap);

/*
	DROPDOWN MENU
*/
function initDropDown(){
	var parents = $$('li.parent_item');
	
	$('navigation').addEvent('mouseleave',removeMenu);
	$('logo').addEvent('mouseenter',removeMenu);
		
	parents.each(function(parent){
		var child = parent.getElement('ul');
		var menu = null;
		
		parent.store('menu',child);
		parent.addEvent('mouseenter',navEnter.bindWithEvent(parent,child));
		if(!child){
			parent.addEvent('mouseleave',navLeave.bindWithEvent(parent));			
		}
	});
}
window.addEvent('domready',initDropDown);

function navEnter(e,menu){
	// CLEAR EXISTING MENUS
	removeMenu();
	
	// IF THERE IS NO MENU TO SHOW RETURN
	if(!this.retrieve('menu')){
		this.addClass('hover');
		return this;
	};

	this.addClass('hover');	

	
	// DUPLICATE THE MARKUP
	var menu = this.retrieve('menu').clone();
	menu.addClass('dropDownMenu');
	
	// POSITION MENU
	var coords = this.getCoordinates($('wrapper'));
	menu.setStyles({
		'top' : coords.top + coords.height,
		'left' : coords.left
	});
	
	// INJECT MENU
	menu.inject($('navigation'));

	// DESTROY MENU ON MOUSE OUT
	menu.addEvent('mouseleave',function(e){
		new Event(e).stop();
		menu.destroy();		
		this.removeClass('hover');	
	}.bindWithEvent(this));
}



function navLeave(){
	this.removeClass('hover');
}

function removeMenu(){
	$$('.hover').removeClass('hover');
	$$('.dropDownMenu').destroy();
}
