var imagesBaseUrl = '/public/images/';
var menuItemImages = ['menu/item-about-on.png', 'menu/item-login-on.png', 'menu/item-portfolio-on.png', 'menu/item-products-on.png', 'menu/item-services-on.png', 'bg/green-light.png'];
var effects = true;
var corners = true;
var obj = null;

$(document).ready(function(){
	effects = allowEffects();
	corners = allowCorners();
	
	//	Preload menu item images
	/*
	if(!(/MSIE (\d+\.\d+);/.test(navigator.userAgent))){
		for(var i = 0; i < menuItemImages.length; i++){
			$(new Image()).load(imagesBaseUrl + menuItemImages[i]);
		}
	}
	*/
	
	
	$('a[rev="ajax-link"]').each(function(i, el){
		path = $(el).attr('href');
    	$(el).attr('href', $(el).attr('href') + ($(el).attr('href').indexOf("?") != -1 ? '&' : "?") + 'view=1');
		
		if(path == window.location.pathname){
			obj = $(el);
		} 
	});
	
	if(effects){ $('#menu-items .icon').css('opacity', 0.5); }
	$('#menu-items li').hover(function(){
		//	Mouse in
		if($(this).hasClass('on-static')){ return; }
		var icon = $(this).children('.icon');
		if(icon){
			if(effects){
				$('.icon').removeClass('on');
				icon.animate({opacity:1}, 'fast', 'swing', function(){ icon.addClass('on');	});
			} else {
				icon.addClass('on');
			}
		} 
	}, function(){
		//	Mouse out
		if($(this).children('.icon').hasClass('on-static')){ return; }
		var icon = $(this).children('.icon');
		if(icon){
			icon.removeClass('on');
			if(effects){ icon.animate({opacity:0.5}, 'normal'); }
		}
	});
	$('#menu-items li').click(function(){
		var link = $(this).children('.label').children('h2').children('a[rev="ajax-link"]');
		if(link){
			if(isVisible($('#popup'))){ closePopup('nohide'); } 
			if($(this).children('.icon').hasClass('on-static')){ 
				return false; 
			}
			$(this).children('.icon').addClass('on-static');
			
			var nW = 150;
			var nX = 425;
			var nY = 70;
			
			switch($(this).attr('id')){
				case 'item-about':
					nW = 500; nX = 220; nY = 30;
					break;
				case 'item-services':
					nW = 850; nX = 60; nY = 270;
					break;
				case 'item-products':
					nW = 600; nX = 125; nY = 330;
					break;
				case 'item-portfolio':
					nW = 650; nX = parseInt($('#page').width() / 2 - nW / 2); nY = 500;
					break;
				case 'item-login':
					nW = 470; nX = 330; nY = 400;
					break;
				case 'item-contact':
					displayContact($(this).children('div.label').children('h2').children('a').attr('href'));
					return false;
			}

			if(isVisible($('#green-light'))){
				$('#green-light').hide('normal', function(){
					$('#gl-content').html('&nbsp;');
				});
			}
			
			if(isVisible($('#popup'))){
				$('#popup').css('height', $('#popup').height() + 'px');
				$('#pb-content').css('height', (parseInt($('#popup').height()) - 30) + 'px');
			}
			
			$('#pb-content').removeClass('loading');
			$('#pb-content').addClass('loading');
			$('#pb-content').html('&nbsp;');
			
			if(isVisible($('#popup'))){
				$('#popup').animate({width: nW, left: nX, top: nY, opacity: 0.5}, 'normal', function(){
					showPopup(link.attr('href'));
				});
			} else {
				$('#popup').css('width', nW);
				$('#popup').css('left', nX);
				$('#popup').css('top', nY);
				$('#popup').css('height', 'auto');
				$('#pb-content').css('height', 'auto');
				$('#popup').fadeIn('normal');
				showPopup(link.attr('href'));
			}
			
			if(isVisible($('#popup-container'))){ $('#popup-container').hide('normal'); }
		}
		return false;
	});
	$('#pb-close-button').click(function(){
		closePopup('normal');		
	});
	if(corners){ $('.rounded').corners('11px'); }
	
	if(obj != null){
		$(obj).trigger('click');
	}
});

function launchContact(){
	$('#item-contact').children('.label').children('h2').children('a').trigger('click');
}

function launchProducts(){
	$('#item-products').children('.label').children('h2').children('a').trigger('click');
}

function launchServices(){
	$('#item-services').children('.label').children('h2').children('a').trigger('click');
}

function showPopup(link)
{
	$('#pb-content').load(link, null, function(responseText, textStatus, req){
		$('#popup').animate({opacity: 1}, 'fast');
		$('#popup').css('height', 'auto');
		$('#pb-content').css('height', 'auto');
		$('#pb-content').removeClass('loading');							
	});
}

function closePopup(speed)
{
	if(!isVisible($('#popup'))){ return; }
	if(isVisible($('#popup-container'))){
		$('#popup-container').html('&nbsp;');
		$('#popup-container').fadeOut('normal'); 
	}
	if(speed != 'nohide'){
		$('#popup').hide(speed);
	}
	$('.on-static').removeClass('on-static');
	$('.on').removeClass('on');
}

function isVisible(obj)
{
	return (obj.css('display') == 'block');
}

function allowEffects(){
	return !(/MSIE (\d+\.\d+)/.test(navigator.userAgent) || /Chrome/.test(navigator.userAgent));
}

function allowCorners(){
	return !(/MSIE (\d+\.\d+)/.test(navigator.userAgent) || /Chrome/.test(navigator.userAgent) || /Opera/.test(navigator.userAgent) || /Firefox\/2/.test(navigator.userAgent));
}

jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery.easing.easeOutBounce = function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
};

var slideshowTimer = null;
function doSlideshow(objid, start, time){
	if(!$('#' + objid) || !isVisible($('#' + objid))){
		if(slideshowTimer != null){
			clearTimeout(slideshowTimer);
			slideshowTimer = null;
		}
		return;
	}
	
	if(start){
		clearTimeout(slideshowTimer);
		slideshowTimer = null;
	}
	
	var currObj = $('#' + objid + ' li.current');
	var nextObj = currObj.next();
	if($('#' + objid + ' li:last').hasClass('current')){ 
		nextObj = $('#' + objid + ' li:first');
	}

	nextObj.addClass('current');
	nextObj.css('opacity', 0);
	nextObj.css('display', 'block');
	currObj.fadeOut('normal', function(){ $(this).removeClass('current'); });
	nextObj.animate({opacity:1}, 'fast');
	
	slideshowTimer = setTimeout('doSlideshow("' + objid + '", false, ' + time + ');', time);
}

function initInputFields()
{
	$('FORM INPUT.autohide-text, FORM TEXTAREA.autohide-text').each(function(){
		$(this).focus(function(){
			if($(this).val() == $(this).attr('title')){
				$(this).val('');
			}
		});	
		$(this).blur(function(){
			if($(this).val() == ''){
				$(this).val($(this).attr('title'));
			}
		})
		if($(this).val() == ''){
			$(this).val($(this).attr('title'));
		}
	});
}

function initStaticRounded(){ if(corners){ $('.static-rounded').corners('7px'); } }

function initAjaxForm(formid, errorid, hideOnProcessing){
	var form = $('#' + formid);
	if(!form){ return; }
	
	form.submit(function(){
		var formData = $(this).serialize();
		$('#' + errorid).hide();
		
		if(!hideOnProcessing){
			$('input,button,textarea,label').attr('disabled', 'disabled');
			$('input,button,textarea,label').animate({opacity:0.2}, 'normal');
		} else {
			$('input,button,textarea,label').hide();
		}
		
		$(this).children('.processing-request').show('normal');
		$.ajax({
			type: 'POST', url: $(this).attr('action'), data: formData, 
			success: function(responseData){
				form.children('.processing-request').hide('normal');
				if(responseData.substr(0, 7) == 'ERROR: '){
					formReturnFieldsBack(formid, hideOnProcessing);
					formShowError(errorid, responseData.substr(7, responseData.length - 7));
				} else {
					form.html('<p class="success">' + responseData + '</p>');
				}
			},
			error: function(){
				form.children('.processing-request').hide('normal');				
				formShowError(errorid, 'An error occured during processing. Please try again.');
				formReturnFieldsBack(formid, hideOnProcessing);
			}
		});
		return false;
	});
}

function formReturnFieldsBack(formid, hideOnProcessing){
	var controlsPath = '#' + formid + ' input,#' + formid + ' button,#' + formid + ' textarea,#' + formid + ' label';
	if(!hideOnProcessing){
		$(controlsPath).removeAttr('disabled');
		$(controlsPath).animate({opacity:1}, 'normal');
	} else {
		$(controlsPath).show();
	}
}

function formShowError(errorid, errormsg){
	var error = $('#' + errorid);
	if(!error){ return; }
	error.html(errormsg);
	error.show('normal');	
}

function displayContact(href){
	if(isVisible($('#green-light'))){ return; }
	closePopup('fast');
	
	$('#gl-content').html('&nbsp;');
	$('#green-light').show('normal');
	$('#gl-content').addClass('loading');
	$('#gl-content').load(href, null, function(){
		$(this).removeClass('loading');
		if(corners){
			$('ul.ways li a').corners('5px');
			$('#im-address').corners('5px');
		}
		initContact();
	});
	$('#gl-close-button').click(function(){
		$('#green-light').hide('normal', function(){
			$('gl-content').html('&nbsp;');		
		});
		return false;
	});
}

function initContact(){
	var animateOnStart = true;
	$('#content-area .ways .way > a').click(function(){
		if($(this).attr('rel') == 'animated'){
			animateOnStart = false;
		}
		
		if(animateOnStart){
			animateOnStart = false;
			$('#content-area .ways .way > a').attr('rel', 'animated');
			$('#content-area .ways .way > a').animate({paddingTop: 15, paddingBottom: 15}, 'normal', function(){
				$('#content-area .ways').animate({marginTop: 10}, 'normal');			
			});			
		}

		$('#im-address').hide();
		
		if($(this).hasClass('current')){ return; }
		$('#content-area .ways .way > a').removeClass('current');

		if($(this).attr('rev') == 'email'){
			$('#headquarters').hide('fast');
		} else {
			$('#headquarters').show('fast');
		}
		
		$(this).addClass('current');
		$('#content-area .visible').hide().addClass('hidden').removeClass('visible');
		
		var content = $('#' + $(this).attr('rev'));
		content.css('display', 'none');
		content.removeClass('hidden');
		content.addClass('visible');
		content.slideDown('normal');
	});
	
	$('#content-area #im a').click(function(){
		var link = $(this);
		$('#im-address').fadeOut('fast', function(){
			$('#im-address').html(link.html());
			$('#im-address').fadeIn('normal');
		});
		return false;	
	});
}