/**
* @author Paulo Martins <phmartins6@gmail.com>
*/

defineSizeBackground();

jQuery(function() {
	Project._init();
});

var Project = {
	
	count: 1,
	interval: '',
	
	_init: function() {
		try {
			Project._resizeBrowser();
			Project.blank();
			Project.sign();
		} catch (e) {
			alert('Error: ' + e.description);
		}
	},
	
	_resizeBrowser: function() {
		$(window).bind('resize', function(){
			defineSizeBackground();
		});
	},
	
	homeGallery2: function(){
		var num_img = $('div.gallery ul.list li').length;
		
		$('div.gallery ul.list li:gt(0)').hide();
		$('div.gallery ul.gallery_pagination').html('');
		
		for(i=1;i<=num_img;i++){
			$('div.gallery ul.gallery_pagination').append("<li><a href='#' id='#"+i+"'>"+i+"</a></li>");
		}
		
		$('div.gallery ul.gallery_pagination li:first a').css('font-weight', 'bold');
		
		
    	setInterval('Project.transitionGallery()',5000);
	},
	
	homeGallery: function(){
		var num_imgs = $('div.gallery ul.list li').length;
		
		for(i=1;i<=num_imgs;i++){
			$('div.gallery ul.gallery_pagination').append("<li><a href='#' id='#"+i+"'>"+i+"</a></li>");
		}
		
		$('div.gallery ul.list li:first').show().addClass('now');
		$('div.gallery ul.gallery_pagination li:first a').css({'font-weight':'bold', color: '#ffffff'});
		
		this.clickGallery();
		Project.interval = setInterval('Project.transitionGallery('+num_imgs+')',5000);
	},
	
	clickGallery: function() {
		var num_imgs = $('div.gallery ul.list li').length;
		
		$('div.gallery ul.gallery_pagination li a').bind('click',function() {
			$('div.gallery ul.list li.now').fadeOut('fast').removeClass('now');
			$("div.gallery ul.list li a[rel='"+$(this).attr('id')+"']").parent().fadeIn('low').addClass('now');
			
			$('div.gallery ul.gallery_pagination li a').css({'font-weight':'normal', color: '#000000'});
			$("div.gallery ul.gallery_pagination li a[id='"+$(this).attr('id')+"']").css({'font-weight':'bold', color: '#ffffff'});
			
			Project.count = $(this).attr('id').split('#')[1];
			
			clearInterval(Project.interval);
			Project.interval = setInterval('Project.transitionGallery('+num_imgs+')',5000);
			return false;
		});
	},
	
	transitionGallery: function(num_imgs) {
		if (this.count < num_imgs)
		{
			$('div.gallery ul.list li.now').fadeOut('fast').removeClass('now')
				.next('li').fadeIn('low').addClass('now');
		}
		else
		{
			$('div.gallery ul.list li.now').fadeOut('fast').removeClass('now');
			$('div.gallery ul.list li:first').fadeIn('low').addClass('now');
		}
		
		if(this.count == num_imgs)
			this.count = 1;
		else
			this.count++;
			
		$('div.gallery ul.gallery_pagination li a').css({'font-weight':'normal', color: '#000000'});
		$("div.gallery ul.gallery_pagination li a[id='#"+this.count+"']").css({'font-weight':'bold', color: '#ffffff'});
	},
	
	blank: function() {
		$("a.blank").click(function(event){
			event.preventDefault();
			window.open(this.href);
		});
	},
	
	sign: function() {
		topwindow = getHeight() - 40;
		leftwindow = getWidth() - 140;
		
		$('span.sign').css({
			top: topwindow,
			left: leftwindow
		})
	}
	
};

function defineSizeBackground() {
	hImage = document.getElementById('imgBackground').height;
	document.getElementById('imgBackground').style.width = getWidth().toString() + 'px';
	hg = parseInt(getHeight().toString());
	perc = hg > hImage ? parseInt(hg*0.15) : 0;
	
	heightImage = hg > (hImage + perc) ? (hImage + perc) : hg;
	
	
	document.getElementById('imgBackground').style.height = heightImage + 'px';
}

function getWidth() {
	return window.innerWidth ? window.innerWidth : /* For non-IE */
		document.documentElement ? document.documentElement.clientWidth : /* IE 6+ (Standards Compilant Mode) */
		document.body ? document.body.clientWidth : /* IE 4 Compatible */
		window.screen.width; /* Others (It is not browser window size, but screen size) */
}

function getHeight() {
	return window.innerHeight ? window.innerHeight : /* For non-IE */
		document.documentElement ? document.documentElement.clientHeight : /* IE 6+ (Standards Compilant Mode) */
		document.body ? document.body.clientHeight : /* IE 4 Compatible */
		window.screen.height; /* Others (It is not browser window size, but screen size) */
}
