$(document).ready(function()
{
	$_GET = getURLString();
	$_HASH = getURLHash();
	
	/*******************************
	*        Page scrolling        *
	*******************************/
	
	// jQuery animated sliding contact form
	$('.link_contact, #contact-us, #meeting').click(function()
	{ 
		if($('.link_contact').attr('href') != undefined)
		{
			var speed = 0;
			var top = $('.contact').css().offset().top;
			
			$mainheight = parseInt($('.main').height());
			$contactheight = parseInt($('.contact').height())+1;
			
			$('.main').css({'height':$mainheight+$contactheight+'px'});
			$('.footer').css({'bottom':$contactheight+'px'});
			$('.contact').css({'bottom':'0px'});
			
			$('.link_contact').addClass('link_contact_active');
			$('.link_contact_active').removeClass('link_contact');			
			$('.link_contact_active').removeAttr('href');
			$('html, body').stop().animate({scrollTop: top}, speed,	'linear');
		}
		return false;
	});
	
	// Scroll page animation for services page "every step" link
	$('a[href="#our-process"]').click(function()
	{ 
		var speed = 500;
		var top = $('#our-process').offset().top
		
		$('html, body').stop().animate({scrollTop: top}, speed,	'linear');			
		return false;
	});
	
	// Scroll page animation for Index page "Learn more" link
	$('a[href="#why-choose-us"]').click(function()
	{
		var speed = 500;
		var top = $('#why-choose-us').offset().top;
		
		$('html, body').stop().animate({scrollTop: top}, speed,	'linear');			
		return false;
	});
	
	/*******************************
	*     Slideshow animations     *
	*******************************/
	
	// Index page "Images" slideshow animation
	$('#client-slideshow #slides').cycle
	({
		fx:     'fade',
		speed:   300,
		timeout: 6100,
		containerResize: false,
		next:   '#start',
		startingSlide: Math.floor(Math.random()*$('.slide').length)
	});
	
	// Index page "Our Services at a Glance" slideshow animation
	$('#services-glance .slideshow').cycle
	({
		timeout: 0,
		containerResize: false,
		startingSlide: Math.floor(Math.random()*$('#services-glance .slideshow div').length)
	});
	
	// Work page "Web Portfolio" articles slideshow animation
	initialiseWorkSlideshow();
	
	// Services page "Our services in Detail" slideshow animation
	initialiseServicesSlideshow();
	
	/*******************************
	*           Tooltips           *
	*******************************/
	
	// Index page "Why Choose Us?" hover tooltips
	$('#why-choose-us ul li a').hoverIntent(function()
	{			
		fadeInSpeed = 500;
		fadeOutSpeed = 500;
		$tooltipSlides = $('#tooltips img');
		
		hoverid = $(this).attr('id');
		imagename = hoverid;
		
		$tooltipSlides.each(function()
		{
			if($(this).is(':visible'))
				currentSlide = $tooltipSlides.index(this);
		});
		
		$tooltipSlides.each(function()
		{
			$imagesrc = $(this).attr('src');
			$startpos = $imagesrc.lastIndexOf('_')+1;
			$endpos = $imagesrc.length-4;					
			$image = $imagesrc.substring($startpos,$endpos);
			
			if($image == imagename)
				newSlide = $tooltipSlides.index(this);
		});
		
		if(currentSlide != newSlide)
			changeSlide($tooltipSlides, currentSlide, newSlide, fadeInSpeed, fadeOutSpeed);
		
		return false;
	},
	function(){});
	
	// Services page "Service principles" tooltips
	$('#service-principles ul li a').hoverIntent(function()
	{
		fadeInSpeed = 500;
		fadeOutSpeed = 500;
		$tooltipSlides = $('#service-tooltips img');
		
		hoverid = $(this).attr('id');
		if(hoverid.indexOf('tooltip') != -1)
		{
			imageid = hoverid.split('-',2);
			imagename = imageid[1];
			
			$tooltipSlides.each(function()
			{
				if($(this).is(':visible'))
					currentSlide = $tooltipSlides.index(this);
			});
			
			$tooltipSlides.each(function()
			{
				$imagesrc = $(this).attr('src');
				$startpos = $imagesrc.lastIndexOf('_')+1;
				$endpos = $imagesrc.length-4;					
				$image = $imagesrc.substring($startpos,$endpos);
				
				if($image == imagename)
					newSlide = $tooltipSlides.index(this);
			});
			
			if(currentSlide != newSlide)
				changeSlide($tooltipSlides, currentSlide, newSlide, fadeInSpeed, fadeOutSpeed);
		}
		return false;
	},
	function(){});
	
	// Services page "Website Design" tooltips
	$('#website-design ul li a').hoverIntent(function()
	{
		fadeInSpeed = 500;
		fadeOutSpeed = 500;
		$tooltipSlides = $('#design-tooltips img');
		
		hoverid = $(this).attr('id');
		if(hoverid.indexOf('tooltip') != -1)
		{
			imageid = hoverid.split('-',2);
			imagename = imageid[1];
			
			$tooltipSlides.each(function()
			{
				if($(this).is(':visible'))
					currentSlide = $tooltipSlides.index(this);
			});
			
			$tooltipSlides.each(function()
			{
				$imagesrc = $(this).attr('src');
				$startpos = $imagesrc.lastIndexOf('_')+1;
				$endpos = $imagesrc.length-4;					
				$image = $imagesrc.substring($startpos,$endpos);
				
				if($image == imagename)
					newSlide = $tooltipSlides.index(this);
			});
			
			if(currentSlide != newSlide)
				changeSlide($tooltipSlides, currentSlide, newSlide, fadeInSpeed, fadeOutSpeed);
		}
		return false;
	},
	function(){});
	
	/*******************************
	*         Other scripts        *
	*******************************/
	
	// jQuery contact form default values
	initialiseContactForm();
	
	// jQuery set Tooltips starting slide
	initialiseTooltips();
	
	// jQuery blend, top and bottom page navigation fade effect
	/*if(!(/MSIE/.test(navigator.userAgent)) && !(/Mobile/.test(navigator.userAgent)))
	{
		//$('.navi .links ul li a:not(.link_contact)').blend();
		$('.navi .links ul li a').blend();
	}*/
	
	// Index page "Why Choose Us?" hover tooltips prevent default services page link on iphone
	if(/Mobile/.test(navigator.userAgent))
	{
		$('#why-choose-us ul li a').click(function(){ return false; });
		$('#service-principles ul li a').click(function(){ return false; });
		$('#website-design ul li a').click(function(){ return false; });
	}
});

if(/MSIE (\d+\.\d+);/.test(navigator.userAgent))
{
	var ieversion = new Number(RegExp.$1);
	
	if(ieversion <= 8)
	{
		// Rounded corners for Internet Explorer v8.0 and older
		DD_roundies.addRule('.highlight', '4px');
	}
}

function changeSlide(tooltipSlides, currentSlide, newSlide, fadeInSpeed, fadeOutSpeed)
{
	tooltipSlides.eq(newSlide).fadeIn(fadeInSpeed).end().eq(currentSlide).fadeOut(fadeOutSpeed).end();
}

function initialiseTooltips()
{
	$('#tooltips img').hide();
	$('#tooltips img:last').show();
	
	$('#service-tooltips img').hide();
	$('#service-tooltips img:last').show();
	
	$('#design-tooltips img').hide();
	$('#design-tooltips img:last').show();
}

function initialiseContactForm()
{
	$contactheight = parseInt($('.contact').height())+1;
	$('.contact').css({'bottom':'-'+$contactheight+'px'});
	
	$('form input').add('form textarea').focus(function()
	{
		var $this = $(this);

		if(!$this.data('default'))
			$this.data('default', $this.val());
			
		if($this.val() == $this.data('default'))
			$this.val('');
	})
	.blur(function()
	{
		var $this = $(this);

		if($this.val() == '')
			$(this).val($this.data('default')).css('color', '#f0f0f0');
	});
}

function initialiseWorkSlideshow()
{
	if($_HASH['id'])
	{
		var slideNum = 0;
		var currentSlide = 0;
		$('.slideshow-desc > div').each(function()
		{
			splitid = $(this).attr('id').split('-',2);
			articleid = splitid[1];
			
			if(articleid == $_HASH['id'])
				currentSlide = slideNum;
			
			slideNum++;
		});
	}
	else
	{
		currentSlide = Math.floor(Math.random()*$('.slide').length);
	}
	
	$('#work-slideshow #slides').cycle
	({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		containerResize: false,
		next:   '#start',
		startingSlide: currentSlide,
		after:	onAfterWorkSlide
	});
}

function initialiseServicesSlideshow()
{
	if($_GET['slide'])
	{
		var speed = 0;
		var slideNum = 0;
		var currentSlide = 0;
		var top = $('#our-services').offset().top
		$('html, body').stop().animate({scrollTop: top}, speed,	'linear');
		
		$('#our-services .slideshow .slideshow-desc > div').each(function()
		{
			if($(this).attr('id') == $_GET['slide'])
				currentSlide = slideNum;
			
			slideNum++;
		});
	}
	
	$('#our-services .slideshow .slideshow-desc').cycle
	({
		fx:     'fade',
		speed:   300,
		timeout: 0,
		next:   '#start',
		before:	onBeforeServicesSlide,
		startingSlide: currentSlide
	});
}

function onBeforeServicesSlide(curr, next, opts, fwd)
{
	slideID = next.id;
	
	switch(slideID)
	{
		case 'website-development':
		{
			$('#our-services .slideshow #controls #start').css({'background-position':'-0px -531px'});
			$('#our-services .slideshow #controls #start').hover(function()
			{
				$(this).css({'background-position':'-280px -531px'});
			},
			function()
			{
				$(this).css({'background-position':'-0px -531px'});
			});
			break;
		}
		case 'website-design':
		{
			$('#our-services .slideshow #controls #start').css({'background-position':'-0px -591px'});
			$('#our-services .slideshow #controls #start').hover(function()
			{
				$(this).css({'background-position':'-280px -591px'});
			},
			function()
			{
				$(this).css({'background-position':'-0px -591px'});
			});
			break;
		}
		case 'logo-design':
		{
			$('#our-services .slideshow #controls #start').css({'background-position':'-0px -651px'});
			$('#our-services .slideshow #controls #start').hover(function()
			{
				$(this).css({'background-position':'-280px -651px'});
			},
			function()
			{
				$(this).css({'background-position':'-0px -651px'});
			});
			break;
		}
		case 'prices':
		{
			$('#our-services .slideshow #controls #start').css({'background-position':'-0px -711px'});
			$('#our-services .slideshow #controls #start').hover(function()
			{
				$(this).css({'background-position':'-280px -711px'});
			},
			function()
			{
				$(this).css({'background-position':'-0px -711px'});
			});
			break;
		}
		default: break;
	}
}

function onAfterWorkSlide(curr, next, opts, fwd)
{
	if($('.slideshow-desc').length)
	{
		$('.slideshow-desc').cycle(
		{
			timeout: 0,
			fx:     'none',
			startingSlide: opts.currSlide
		});
		
		$currentSlideID = $('.slideshow-desc .work-description').eq(opts.currSlide).attr('id');
		splitid = $currentSlideID.split('-',2);
		articleid = splitid[1];
		
		if($_HASH['id'] == false)
		{
			document.location.hash = 'id='+articleid;
		}
		else if($_HASH['id'] != articleid)
		{
			document.location.hash = 'id='+articleid;
		}
	}
}

function getURLHash()
{
	if(location.hash.length)
	{
		var $_HASH = [];
		var query = location.hash.split('#')[1];
		
		if(query)
		{
			if(query.indexOf('=') != -1)
			{
				getName = query.split('=')[0];
				getValue = query.split('=')[1];
				$_HASH[getName] = getValue;
			}
			else
			{
				return query;
			}
			
			return $_HASH;
		}
		else
		{
			return false;
		}		
	}
	else
	{
		return false;
	}
}

function getURLString()
{
	if(location.href.match(/\?/g))
	{
		var $_GET = [];
		var query = location.search.split('?')[1];
		
		if(query)
		{
			if(query.indexOf('&') != -1)
			{
				getvars = query.split('&');
				
				for(i=0; i < getvars.length; i++)
				{
					getName = getvars[i].split('=')[0];
					getValue = getvars[i].split('=')[1];
					$_GET[getName] = getValue;
				}
			}
			else
			{
				if(query.indexOf('=') != -1)
				{
					getName = query.split('=')[0];
					getValue = query.split('=')[1];
					$_GET[getName] = getValue;
				}
				else
				{
					return query;
				}
			}
			
			return $_GET;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
}

function popUpProperties(inobj)
{
	// Used to display javascript object properties
	op = window.open();
	op.document.open('text/plain');
	for (objprop in inobj) 
	{
		op.document.write(objprop + ' => ' + inobj[objprop] + '\n');
	}
	op.document.close();
}
