var _$ = function(elmId)
{
	return document.getElementById(elmId);
}

function setEvent(obj, event, fnc)
{
	try {
		return obj.attachEvent(event, fnc);
	} catch (e) {
		return obj.addEventListener(event.replace(/^on/, ''), fnc, false);
	}
}

function InputDefaultValueOnFocus(e)
{
	e = e || window.event;
	elm = e.srcElement || e.target;

	InputDefaultValue.eventFocus(elm);
}

function InputDefaultValueOnBlur(e)
{
	e = e || window.event;
	elm = e.srcElement || e.target;
	
	InputDefaultValue.eventBlur(elm);
}

var InputDefaultValue = {

	'settings' : {},

	'add' : function(elmId, defaultValue, autoClear, required, styles)
	{
	   var elm = _$(elmId);

		if(!elm)
		{
		   return null;
		}
		else
		{
			InputDefaultValue.settings[elm.id] = {
			   'elmId' : elmId,
			   'defaultValue' : defaultValue,
			   'autoClear' : autoClear,
			   'required' : required,
			   'styles' : styles
			};
			
			setEvent(elm, 'onfocus', InputDefaultValueOnFocus);
			setEvent(elm, 'onblur', InputDefaultValueOnBlur);
		
			this.eventBlur(elm);
		}
	},

	'eventBlur' : function(elm)
	{
	   var sett = InputDefaultValue.settings[elm.id];

	   if(!elm.value.length && sett.required)
	   {
	      elm.value = sett.defaultValue;
	   }
	   
	   if(typeof(sett.styles) == 'object' && elm.value == sett.defaultValue)
		{
			var key;
			for(key in sett.styles)
			{
			   elm.style[key] = sett.styles[key];
			}
		}
	},

	'eventFocus' : function(elm)
	{
	   var sett = InputDefaultValue.settings[elm.id];

	   if(elm.value == sett.defaultValue && sett.autoClear)
	   {
	      elm.value = '';
	      if(typeof(sett.styles) == 'object')
			{
				var key;
				for(key in sett.styles)
				{
				   elm.style[key] = '';
				}
			}
	   }
	},
	
	'clear' : function(elm)
	{
		var sett = InputDefaultValue.settings[elm.id];
	   if(elm.value == sett.defaultValue)
	      elm.value = '';
	}
}

$topRightBoxes = null;
$bottomBoxes = null;

$(document).ready( function() {
	$topRightBoxes = $('.top-right-box').find('.box');
	$bottomBoxes = $('.boxies_HP_bottom').find('.box');
	
	$topRightBoxes.each(function(idx){
		var $this = $(this);
		
		if(idx > 0)
			$this.css('display', 'none');
		else
			$this.addClass('active');
					
		$this.find('.box_prev').bind('click', function() {
			var $this = $(this);
			$box = $this.parents('.box');
			
			if($box.prev().size() != 0)
				$prev = $box.prev();
			else
				$prev = $topRightBoxes.last();
				
			if($prev.hasClass('active'))
				return; 
				
			$box.removeClass('active');
			$prev.addClass('active');
			
			$box.fadeOut(800);
			$prev.fadeIn(800);
		});
		
		$this.find('.box_next').bind('click', function() {
			var $this = $(this);
			$box = $this.parents('.box');
			
			if($box.next().size() != 0)
				$next = $box.next();
			else
				$next = $topRightBoxes.first();
				
			if($next.hasClass('active'))
				return;
				
			$box.removeClass('active');
			$next.addClass('active');  
			
			$box.fadeOut(800);
			$next.fadeIn(800);
		});
	});
	
	$bottomBoxes.each( function(idx) {
		$this = $(this);
		$this.css('left', idx * $this.width());
		
		if(idx == 0)
			$this.addClass('active');		
	});
	
	if($bottomBoxes.size() == 1)
	{
		$('.box_hp_nav .box_next').addClass('box_next_0items');
		$('.box_hp_nav .box_prev').addClass('box_prev_0items');
	}
	
	$('.boxies_HP_bottom').find('.box_prev').bind('click', function() {
		
		if($(':animated').size())
			return;		
		
		$box = $bottomBoxes.filter('.active');
		
		if($box.prev('.box').size() != 0)
			$prev = $box.prev('.box');
		else
			$prev = $bottomBoxes.last('.box');
			
		if($prev.hasClass('active'))
			return;
		
		$box.removeClass('active');
		$prev.addClass('active');	
		
		$box.animate( { left : 960 }, 1000, 'swing');
		$prev.css('left', '-960px');
		$prev.animate( { left : 0 }, 1000, 'swing');			
	});
	
	$('.boxies_HP_bottom').find('.box_next').bind('click', function() {

		if($(':animated').size())
			return;
			
		$box = $bottomBoxes.filter('.active');
		
		if($box.next('.box').size() != 0)
			$next = $box.next('.box');
		else
			$next = $bottomBoxes.first('.box');
			
		if($next.hasClass('active'))
			return;
		
		$box.removeClass('active');
		$next.addClass('active');	
		
		$box.animate( { left : -960 }, 1000, 'swing');
		$next.css('left', '960px');
		$next.animate( { left : 0 }, 1000, 'swing');
	});

	$("a[rel^='prettyPhoto']").prettyPhoto({
		social_tools: false,
		overlay_gallery: false
	});
});
