jQuery.noConflict();
jQuery(function($){
	var MySite = 
	{
		init:function()
		{
			this.tabs();
			this.contactForm();
			this.coda();
			this.slider();
			this.zoom();
			this.IE6();
			this.caseStudySlider();
			this.formValidation();
		},
			
		tabs:function()
		{
			if($('div.tab').length > 0)
			{
				var tabmenu = $('<ul id="TabNav">');
				
				$('div.tab').each(function()
				{
					var h2 = $(this).find('h2');
					var li = $('<li><a href="'+ window.location.pathname + '#' + $(this).attr('id')+'"><span>' + h2.text()+'</span></a></li>');
					li.appendTo(tabmenu);
					h2.remove();
				});
				$('#Aside').prepend(tabmenu);
				
				$('div.tab')
					
					.hide()
					.eq(0)
					.show();
				
				$('#TabNav a').eq(0).addClass('current')
				.end().click(function(){
					$('div.tab').hide();
					var href = $(this).attr('href');
					var hash = href.split('#');
					$('div#'+ hash[1]).show();
					$('#TabNav a').removeClass('current');
					$(this).addClass('current');
					return false;
				});
			}	
		},
		
		contactForm:function()
		{
			
			if($('#ContactBox').length > 0)
			{
				var CB = $('#ContactBox')
				CB.insertAfter('#Header').css({"position":"absolute", "top":"156px", "width":"100%" });
				var mask = $('<div id="Mask">').css("height", CB.height()+ 45 +"px").insertAfter('#Header');
				
				var closeBtn = $('<a href="#" id="CloseBox"><img src="themes/gpmd/images/close-box.png" alt="Close box"></a>');
				closeBtn.appendTo($('#ContactBox div.container'));
				
				if($('#MessageSent').length == 0 && $('#Form_ContactForm_Captcha').length == 0) mask.hide();
				
				$('#TalkToUs').click(function()
				{
					mask.slideToggle('800');	
					return false;
				});
				
				$('#CloseBox').click(function(){
					mask.slideUp('800');
					return false;
				});	
			}
		},
		
		coda:function()
		{
			if ($('#PortfolioSlider').length > 0) 
			{
				$('#PortfolioSlider div.container').append('<div id="ScrollNav"><a class="prev png" title="Previous" href="portfolio/#">Previous projects</a><a class="next png" href="portfolio/#" title="Next">Next projects</a></div>')
				$('#PortfolioSlideList').cycle({
					fx:"scrollHorz",
					pause:1,
					speed:800,
					timeout:0,
					next:"a.next",
					prev:"a.prev"
				});
			}
		},
		
		slider:function()
		{
			if($('#HomePageSlider').length > 0)
			{
				$('#HomePageSlider ul').cycle({
					fx:"scrollDown",
					pause:1,
					speed:800,
					timeout:6000,
					next:"#NextSlide"
				});
			}
		},
		
		caseStudySlider:function()
		{
			if($('#CaseStudyImages').length || $('#ProfileImages').length)
			{
				$('#Large').cycle({
					speed:  'fast', 
    				timeout: 0, 
    				pager:  '#Thumbnails', 	
					pagerAnchorBuilder: function(idx, slide)
					{ 
         				return '#Thumbnails li:eq(' + idx + ') a'; 
    				},
					before: function(curr, next)
					{
						$('span.pointer').remove();
					},
					after: function()
					{
						$('#Thumbnails a.activeSlide').parent().append('<span class="pointer png"></span>');
					}
				});
				
				$('#Thumbnails a.activeSlide').parent().append('<span class="pointer png"></span>');
			}
		},
		
		formValidation:function()
		{
			if($('#Form_ContactForm').length)
			{
				$('#Form_ContactForm').validate();
			}
			
			if($('#CustomPageCommentInterface_Form_PostCommentForm').length)
			{
				$('#CustomPageCommentInterface_Form_PostCommentForm').validate();
			}
		},
		
		zoom:function()
		{
			if($('a.zoom').length > 0)
			{
				$('a.zoom').fancybox();
			}
		},
		IE6:function()
		{
			$("ul#Main li")
				.hover(
					function(e){$(this).addClass("hover");},
					function(e){$(this).removeClass("hover");});
		}
	}	
	MySite.init();
});
