var overlay = function(){
    var overlay,
        lightbox = $('.overlay'),
        wh = $(document).height() > $(window).height() ? $(document).height() : $(window).height(),
        so = $(window).scrollTop();

    lightbox.css('display' , 'block');
    lightbox.css('left' , ( $('#wrapper').width() - lightbox.width() ) / 2);
    if ( so + lightbox.height() > wh ) {
        lightbox.css( 'top' , wh - lightbox.height() );
    } else {
        lightbox.css( 'top' , so );
    }
    overlay = $('<div id="overlay"></div>');
    overlay.css( 'position' , 'absolute' )
        .css('top' , 0)
        .css('left' , 0)
        .css('z-index' , 1500)
        .css('background' , '#000')
        .css('width' , $(document).width())
        .css('height' , wh)
        .css('opacity' , 0.5);


    overlay.click(function(){
        $(this).remove();
        $('.overlay').css('display' , 'none');
    });

    $('body').append( overlay );
    return false;
}

$(document).ready(function() {

	$('#cboxClose').live('click', function(){
		$('.overlay, #overlay').hide();
	});

	var link;
    link = $('a[rel="overlay"]');
    if ( link.length === 0 ){ 
		//return false;
	} else {
    	link.click(overlay);
	}
	
	external();
	
	/*
		$('.controls a').click(function(){
			$('.controls a').removeClass('selected');
			$(this).addClass(' selected');
			return false;
		});
	*/	

    $(function() {
		$(".partners .right li a").click(function(){
			$.getJSON("/partners/?ajax&id=" + $(this).parent().attr("class").split("-")[1], function(data)
			{
				$('.overlay .image a').attr('href', data.url);
				$('.overlay .image img').attr('src', '/dyn/partners/' + data.image).attr('alt', data.name);
				$('.overlay h3 a').attr('href', data.url);
				$('.overlay h3 a').html('Visit the ' + data.name + ' website');
				$('.overlay .b p').html(data.description);
				
			});
			$(this).colorbox({
				width:"688px",
				inline:true,
				href:".overlay"
			});
		});
		$(".support .link7").colorbox({
			width:"688px",
			inline:true,
			href:".overlay"}
		);		
    });

	$(".ank").anchor();	

	/* Casestudies Scrollable
	===========================*/
	
	// Init Scrollable	
	$(".scrollable").scrollable({
		size: 1,
		clickable: false
	}).navigator();
	
	// Get access to the API
	var api = $(".scrollable").data("scrollable");

	// Auto scroll interval
	var autoScroll = setInterval(function(){
		
		var total = $('.navi > a').size();
		var currentIndex =	$('.navi > a[class="active"]').index();
		var nextIndex = currentIndex + Number(1);
		
		if (nextIndex > (total - Number(1))){
			nextIndex = 0;	
		}
		
		$('.navi > a').removeClass('active');
		$('.navi > a:eq(' + nextIndex + ')').addClass('active');
		
		
		if (nextIndex == 0){
			api.begin();
		} else {
			api.next();
		}
				
	}, 5000);

	// Clear auto scroll when nav item is clicked
	$('.navi > a').live('click', function(event){
	
		clearInterval(autoScroll);	

	});
	
	/*=========================*/

	$('#list1b').accordion({
		autoheight: false
	});
	
	var accordions = jQuery('#list1b');
	jQuery('#switch select').change(function() {
		accordions.accordion("activate", this.selectedIndex-1 );
	});
	
	jQuery('#close').click(function() {
		accordions.accordion("activate", -1);
	});
	
	jQuery('#switch2').change(function() {
		accordions.accordion("activate", this.value);
	});
	
	jQuery('#enable').click(function() {
		accordions.accordion("enable");
	});
	
	jQuery('#disable').click(function() {
		accordions.accordion("disable");
	});
	
	jQuery('#remove').click(function() {
		accordions.accordion("destroy");
		wizardButtons.unbind("click");
	});

	$('.draw h3').click(function(){
		$('.draw h3').removeClass(' active');
		$(this).addClass(' active');
		return false;
	});
	$(".draw1").click(function(){
		$('.the-project1').slideDown("fast");
		$('.the-project2').slideUp("fast");
		$('.the-project3').slideUp("fast");
		$('.the-project4').slideUp("fast");
		$('.the-project5').slideUp("fast");
		$('.the-project6').slideUp("fast");
	});
	$(".draw2").click(function(){
		$('.the-project1').slideUp("fast");
		$('.the-project2').slideDown("fast");
		$('.the-project3').slideUp("fast");
		$('.the-project4').slideUp("fast");
		$('.the-project5').slideUp("fast");
		$('.the-project6').slideUp("fast");
	});
	$(".draw3").click(function(){
		$('.the-project1').slideUp("fast");
		$('.the-project2').slideUp("fast");
		$('.the-project3').slideDown("fast");
		$('.the-project4').slideUp("fast");
		$('.the-project5').slideUp("fast");
		$('.the-project6').slideUp("fast");
	});
	$(".draw4").click(function(){
		$('.the-project1').slideUp("fast");
		$('.the-project2').slideUp("fast");
		$('.the-project3').slideUp("fast");
		$('.the-project4').slideDown("fast");
		$('.the-project5').slideUp("fast");
		$('.the-project6').slideUp("fast");
	});	
	$(".draw5").click(function(){
		$('.the-project1').slideUp("fast");
		$('.the-project2').slideUp("fast");
		$('.the-project3').slideUp("fast");
		$('.the-project4').slideUp("fast");
		$('.the-project5').slideDown("fast");
		$('.the-project6').slideUp("fast");
	});	
	$(".draw6").click(function(){
		$('.the-project1').slideUp("fast");
		$('.the-project2').slideUp("fast");
		$('.the-project3').slideUp("fast");
		$('.the-project4').slideUp("fast");
		$('.the-project5').slideUp("fast");
		$('.the-project6').slideDown("fast");
	});	
	
});

jQuery.fn.anchor = function(settings) {
 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}

function ValidateTicket(form)
{
	var errors = new Array();
	
	if($('#subject').val().length < 2) errors.push('subject');
	if($('#name').val().length < 2) errors.push('name');
	if($('#customer').val().length < 2) errors.push('customer');
	if($('#tel').val().length < 2) errors.push('tel');
	if($('#email').val().length < 2) errors.push('email');
	if($('#fault').val().length < 2) errors.push('fault');
	
	if(errors.length > 0)
	{
		$.each(errors,function(key){
			$('label[for="'+ errors[key] +'"]').addClass('error');
		});
		alert('Some fields have not been completed successfully.\nThese fields have been marked in red for your attention.');
	}
	else
	{
		return true;
	}
	return false;
}

function ValidateFeedback(form)
{
	var errors = new Array();
	
	if($('#name', form).val().length < 2) errors.push('name');
	if($('#company', form).val().length < 2) errors.push('company');
	if($('#email', form).val().length < 2) errors.push('email');
	if($('#telephone', form).val().length < 2) errors.push('telephone');
	if($('#details', form).val().length < 2) errors.push('details');
	
	if($('#availability', form).val().length < 2) errors.push('availability');
	if($('#productsuit', form).val().length < 2) errors.push('productsuit');
	if($('#reliability', form).val().length < 2) errors.push('reliability');
	if($('#salessupport', form).val().length < 2) errors.push('salessupport');
	if($('#helpdesksupport', form).val().length < 2) errors.push('helpdesksupport');
	if($('#engineerresponse', form).val().length < 2) errors.push('engineerresponse');
	if($('#engineertimekeeping', form).val().length < 2) errors.push('engineertimekeeping');
	if($('#communication', form).val().length < 2) errors.push('communication');
	if($('#service', form).val().length < 2) errors.push('service');
	if($('#experience', form).val().length < 2) errors.push('experience');
	
	if(errors.length > 0)
	{
		$.each(errors,function(key){
			$('label[for="'+ errors[key] +'"]').addClass('error');
		});
		alert('Some fields have not been completed successfully.\nThese fields have been marked in red for your attention.');
	}
	else
	{
		form.submit();
	}
	return false;
}

function ValidateCallback(form)
{
	var errors = new Array();
	
	if($('#time', form).val().length < 2) errors.push('time');
	if($('#telephone', form).val().length < 2) errors.push('telephone');
	if($('#company', form).val().length < 2) errors.push('company');
	if($('#name', form).val().length < 2) errors.push('name');
	
	if(errors.length > 0)
	{
		$.each(errors,function(key){
			$('label[for="'+ errors[key] +'"]').addClass('error');
		});
		alert('Some fields have not been completed successfully.\nThese fields have been marked in red for your attention.');
	}
	else
	{
		form.submit();
	}
	return false;
}

function ValidateContact(form)
{
	var errors = new Array();
	
	if($('#department', form).val().length < 2) errors.push('department');
	if($('#title', form).val().length < 2) errors.push('title');
	if($('#firstname', form).val().length < 2) errors.push('firstname');
	if($('#lastname', form).val().length < 2) errors.push('lastname');
	if($('#email', form).val().length < 2) errors.push('email');
	if($('#preference', form).val().length < 2) errors.push('preference');
	if($('#remarks', form).val().length < 2) errors.push('remarks');
	
	if(errors.length > 0)
	{
		$.each(errors,function(key){
			$('label[for="'+ errors[key] +'"]').addClass('error');
		});
		alert('Some fields have not been completed successfully.\nThese fields have been marked in red for your attention.');
	}
	else
	{
		form.submit();
	}
	return false;
}
			
function UpdatePositions(department)
{
	$('select#position option').remove();
	$('select#position').append('<option value="">Select One</option>');
	
	for(var key in positions[department])
	{
		$('select#position').append('<option value="' + positions[department][key][0] + '">' + positions[department][key][1] + '</option>')
	}
	
	return false;
}

function ValidateInterest(form)
{
	var errors = new Array();
	
	if($('#name', form).val().length < 2) errors.push('name');
	if($('#email', form).val().length < 2) errors.push('email');
	if($('#cv', form).val().length < 2) errors.push('cv');
	
	if(errors.length > 0)
	{
		$.each(errors,function(key){
			$('label[for="'+ errors[key] +'"]').addClass('error');
		});
		alert('Some fields have not been completed successfully.\nThese fields have been marked in red for your attention.');
	}
	else
	{
		form.submit();
	}
	return false;
}

function ValidateInvite(form)
{
	var errors = new Array();
	
	$('label', form).removeClass('error');
	
	if($('#name', form).val().length < 2) errors.push('name');
	if($('#company', form).val().length < 2) errors.push('company');
	if($('#position', form).val().length < 2) errors.push('position');
	if($('#email', form).val().length < 2) errors.push('email');
	if($('#phone', form).val().length < 2) errors.push('phone');
	
	if(errors.length > 0)
	{
		$.each(errors,function(key){
			$('label[for="'+ errors[key] +'"]').addClass('error');
		});
		alert('Some fields have not been completed successfully.\nThese fields have been marked in red for your attention.');
	}
	else
	{
		form.submit();
	}
}

function ValidateRSVP(form){
	var errors = new Array();
	var rqds = new Array('name','surname','company','email','mobile','phone');
	var form = form;
	$.each(rqds , function(r){
		if($('#' + rqds[r], form).val().length < 2) errors.push(rqds[r]);
	});


	if ($('[name="group1"]:checked' , form).val() == 'YesPartner') {
		if($('#guest', form).val().length < 2) errors.push('guest');
	}

	if(errors.length > 0)
	{
		$.each(errors,function(key){
			$('label[for="'+ errors[key] +'"]').addClass('error');
		});
		alert('Some fields have not been completed successfully.\nThese fields have been marked in red for your attention.');
		return false;
	}
	return true;
}

function ValidateEvent(form){
	var errors , rqds
	errors = new Array();
	rqds = new Array('subject','name','company','tel','date','serial','email','date');
	$.each(rqds , function(r){
		if($('#' + rqds[r], form).val().length < 2) errors.push(rqds[r]);
	});

	$.ajax({
		url: '/validate-email.php',
		dataType: 'json',
		data: 'email=' + escape($('#email', form).val()),
		type : 'get',
		success: function(data , text){
			if (data.status == 0) errors.push('email');
			if(errors.length > 0)
			{
				$.each(errors,function(key){
					$('label[for="'+ errors[key] +'"]').addClass('error');
				});
				alert('Some fields have not been completed successfully.\nThese fields have been marked in red for your attention.');
				return false;
			}
			form.submit();
		},
		error : function(d,t,et){
			alert('There was an error submitting the form. Please try later. If this problem persists, please email support@proav.com');
		}
	
	});

	return false;
}

var external = function(){
    var a = $('a');
	$.each(a , function(i , e){
        var href = $(this).attr('href');
		if (href == undefined || href == '#') return false;
        if ( href.match( /[a-zA-Z]{3,}:\/\// ) != null){
			var match = href.match(/[a-zA-Z]{3,}:\/\/[^\/$]*/).toString();
            if (document.location.toString().substr(0,match.length) != match){
                e.setAttribute('target','_blank');
            }
        }
    });
}

