jQuery.noConflict();
jQuery(function($){
	highlightDisabled = false;
	viewHeight = $(window).height();
	
	// setup all pages heights
	setHeight = function(i, elem){
		$elem = $(elem);
		
		if($elem.outerHeight() < viewHeight){
			var height = $.browser.msie ? 'height' : 'min-height';
			$elem.css(height, viewHeight);
		}
	}
	$('.content-slide').each(setHeight);
	$(window).resize(function(){
		$('.content-slide').each(setHeight);
	});
	
	// submenu
	$('#menu li ul').hide();
	
	// hook up menu switching effects
	$('#menu a').click(function(){
		$(this).blur();
		
		var hash = $(this).attr('href');
		if(hash.indexOf('#') != 0){
			return true;
		}
		var position = $(hash).offset().top;
		var selector = $.browser.safari ? 'body' : 'html';
		highlightDisabled = true;
		
		if($.browser.msie && $.browser.version == 6){
			$('#menu>li.active>a').css('background', "url(images/tab-inactive-bg.png)");
			$(this).css('background', "url(images/tab-active-bg.png)");
		}
		
		$('#menu li').removeClass('active');
		$(this).parents('li').addClass('active');
		
		$('#menu>li').not($(this).parents('#menu>li')).find('ul').slideUp();
		
		$(selector).animate({
			scrollTop: position+'px'
		}, 500, 'easeInOutQuart', function(){
			highlightDisabled = false;
			if(!$.browser.safari){
				window.location.hash = hash.replace('#', '');
			}
		});
		
		return false;
	});
	
	$('#menu>li').hover(function(){
		$(this).find('ul').slideDown();
	},function(){
		$(this).not('.active').find('ul').slideUp();
	});
	
	// menu highlight
	$(window).scroll(function(){
		if(highlightDisabled) return;
		
		$('.content-slide').each(function(){
			$el = $(this);
			scrollTop = $(document).scrollTop();
			elemTop = $el.offset().top;
			if(scrollTop <= elemTop && scrollTop + 100 > elemTop){
				$('#menu li').removeClass('active');
				$('#menu li:has(a[href=#'+$el.attr('id')+'])').addClass('active');
			}
		});
	});
	
	$(window).trigger('scroll');
	
	// setup fancybox
	$('.portfolio-item a').fancybox();
	
	//SETUP CONTACT FORM
	$('#contactForm').submit(function(){
		$.post(
			'contactengine.php',
			$(this).serialize(),
			function(data){
				$('#message').html(data).slideDown();
			}
		);
		
		return false;
	});
	
	$('#brochureForm').submit(function(){
		$.post(
			'contactengine.php',
			$(this).serialize(),
			function(data){
				$('#brochure').html(data).slideDown();
			}
		);
		
		return false;
	});
	
});
/**
 *	these are animation functions used by the animate() function
 */
var animFunc1 = function(rows, cols){
	var n = rows*cols;
	for(i = 0; i < n; i++){
		setTimeout(
			"jQuery('#square'+"+i+").stop().animate({'opacity': 1}, "+anim_speed+");", 
			Math.round(Math.random() * anim_speed)
		);
	}
}

var animFunc2 = function(rows, cols){
	var n = rows*cols;
	for(i = 0; i < n; i++){
		setTimeout(
			"jQuery('#square'+"+i+").stop().animate({'opacity': 1}, "+anim_speed+");", 
			i * 70
		);
	}
}

var animFunc3 = function(rows, cols){
	var n = 0;
	for(i = 0; i < cols; i++){
		for(j = 0; j < rows; j++){
			setTimeout(
				"jQuery('#square'+"+(j*10+i)+").stop().animate({'opacity': 1}, "+anim_speed+");", 
				n * 70
			);
			n++;
		}
	}
}

/**
 *	this function is in charge of swapping images and running the animation functions
 */
var animate = function(rows, cols){
	// move current image to background
	jQuery('#billboard').css({
		'background-image': 'url('+images[cur_img]+')'
	});
	
	// set next image as bg to all the animation divs, and set their opacity to 0
	jQuery('#billboard div').css({
		'background-image': 'url('+images[nxt_img]+')',
		'opacity': '0'
	});
	
	// run the effect function
	eval("animFunc"+cur_eff+"("+rows+", "+cols+");");
	
	if(cur_eff == 3) cur_eff = 1; else cur_eff++; // loop effect pointer
	if(images.length - 1 == cur_img) cur_img = 0; else cur_img++; // loop current image pointer
	if(images.length - 1 == nxt_img) nxt_img = 0; else nxt_img++; // loop next image pointer
		
	// clear timeout, just to be sure
	if(typeof t != 'undefined'){
		clearTimeout(t);
	}
	// and run self after specified time
	window.t = setTimeout("animate("+rows+", "+cols+");", interval);
};

jQuery(window).load(function($){
	// no images to swith between, returning
	if(jQuery('#billboard img').size() < 2){
		return;
	}
	
	if(jQuery.browser.msie && jQuery.browser.version == 6){
		jQuery('#billboard').cycle({
			fx: 'fade'
		});
		return;
	}
	
	window.squares = [];
	window.images = [];
	window.cur_img = 0;
	window.nxt_img = 1;
	window.cur_eff = 1;
	window.interval = 8000;
	window.anim_speed = 1000;
	var variant = 68;
	var rows = 340 / variant;
	var cols = 680 / variant;
	var billboard = jQuery('#billboard');
	
	// parse images within the billboard, add their sources to an array and remove them
	jQuery('#billboard img').each(function(){
		images.push(jQuery(this).attr('src'));
		jQuery(this).remove();
	});
	
	// set billboard background
	jQuery('#billboard').css({
		'background-image': 'url('+images[cur_img]+')'
	});
	
	// create divs for animation
	var n = 0;
	for(y = 0; y < rows; y++){
		for(x = 0; x < cols; x++){
			var current = document.createElement('div');
			current.style.backgroundPosition = -x*variant+'px '+(-y*variant)+'px';
			current.id = 'square'+n;
			billboard.append(current);
			squares.push(current.id);
			n++;
		}
	}
	
	jQuery('#billboard div').css({
		'opacity': '0',
		'width': variant+'px',
		'height': variant+'px',
		'float': 'left',
		'position': 'relative',
		'z-index': 1
	});
	
	// and we're off
	setTimeout("animate("+rows+", "+cols+");", interval - anim_speed - 2000);
});