// On DOM load
$(function()
{
	
	Shadowbox.init({
	    // skip the automatic setup again, we do this later manually
	    skipSetup: true
	});

	Shadowbox.setup(".hostingLinks a");

	
	
	SyntaxHighlighter.defaults['tab-size'] = 2;
	SyntaxHighlighter.config.clipboardSwf = '/inc/js/clipboard.swf';
	SyntaxHighlighter.all();
	
	$('#wwdList').css('font-size', '24px');
	
	$('#wwdList').cycle({ 
	    fx:     'scrollUp', 
	    speed:  'slow', 
	    timeout: 4000
	});
	
	$('#devTypes').cycle({
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 3000
	});
	
	$('#services').accordion({ collapsible: true,
							   active: false,
							   autoHeight: false});
	
	$('#contactSubmit').attr('disabled', 'disabled');
	doContactButton();
	
	$('#uploadStatusDialog').dialog({ autoOpen: false });
	
	$('#contactForm').submit(function(){
		if(validateContactForm()) {
			//Input is validated, submit
			$('#uploadStatusDialog').dialog('open');
			$('#uploadStatusDialog').html('<img src="/images/icons/ajax-loader.gif" />');
			//AjaxForm Options
			var contactFormOptions = {   
					target: 		null,
			        url:       		'/ajax/contactEmail.php',   // override for form's 'action' attribute 
			        type:      		'post',
			        success:		function(msg){
										if(msg == 1){
											$('#uploadStatusDialog').html("<p>Your email has been sent. Thanks.</p><p>We will contact you shortly</p>");
											$('#contactForm').hide();
											$('#contactConfirm').show();
										}
										else {
											$('#uploadStatusDialog').html("<p>There was a problem sending your mail.</p><p>Please try again</p>");
										}
									}	
			};		
			$('#contactForm').ajaxSubmit(contactFormOptions);
			return false;
			
		}
		else {
			alert('Please check your form');
			return false;
		}
		
	});
	
	function validateContactForm(){
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		if ($('#contactName').val() == ''){
			return false;
		}
		else if ($('#contactEmail').val() == ''){
			return false;
		}
		else if (!emailReg.test($(this).val())){
			return false;
		}
		else if ($('#contactQuery').val() == ''){
			return false;
		}
		else {
			return true;
		}
	}
	
	function doContactButton(){
		if(validateContactForm()){
			$('#contactSubmit').attr('disabled', '');
		}
		else {
			$('#contactSubmit').attr('disabled', 'disabled');
		}
	}
	
	$('#contactName').keyup(function(){
		if($(this).val() != ''){
			$(this).attr('class', 'inputSuccess');
			$('#contactNameTooltip').hide();
			$('#contactNameSuccess').fadeIn();
		}
		else {
			$(this).attr('class', 'inputError');
			$('#contactNameSuccess').hide();
			$('#contactNameTooltip').fadeIn();
		}
		doContactButton();
	});
	
	$('#contactName').blur(function(){
		if($(this).val() != ''){
			$(this).attr('class', 'inputSuccess');
			$('#contactNameTooltip').hide();
			$('#contactNameSuccess').fadeIn();
		}
		else {
			$(this).attr('class', 'inputError');
			$('#contactNameSuccess').hide();
			$('#contactNameTooltip').fadeIn();
		}
		doContactButton();
	});
	
	$('#contactEmail').keyup(function(){
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

		if(emailReg.test($(this).val()) && $(this).val() != '') 
		{
			$(this).attr('class', 'inputSuccess');
			$('#contactEmailTooltip').hide();
			$('#contactEmailSuccess').fadeIn();
		}
		else {
			$(this).attr('class', 'inputError');
			$('#contactEmailSuccess').hide();
			$('#contactEmailTooltip').fadeIn();
		}
		doContactButton();
	});
	
	$('#contactEmail').blur(function(){
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		if(emailReg.test($(this).val()) && $(this).val() != '') {
			$(this).attr('class', 'inputSuccess');
			$('#contactEmailTooltip').hide();
			$('#contactEmailSuccess').fadeIn();
		}
		else {
			$(this).attr('class', 'inputError');
			$('#contactEmailSuccess').hide();
			$('#contactEmailTooltip').fadeIn();
		}
		doContactButton();
	});
	
	$('#contactQuery').keyup(function(){
		if($(this).val() != ''){
			$(this).attr('class', 'inputSuccess');
			$('#contactQueryTooltip').hide();
			$('#contactQuerySuccess').fadeIn();
		}
		else {
			$(this).attr('class', 'inputError');
			$('#contactQuerySuccess').hide();
			$('#contactQueryTooltip').fadeIn();
		}
		doContactButton();
	});
	
	$('#contactQuery').blur(function(){
		if($(this).val() != ''){
			$(this).attr('class', 'inputSuccess');
			$('#contactQueryTooltip').hide();
			$('#contactQuerySuccess').fadeIn();
		}
		else {
			$(this).attr('class', 'inputError');
			$('#contactQuerySuccess').hide();
			$('#contactQueryTooltip').fadeIn();
		}
		doContactButton();
	});
	
	
	//Portfolio
	
	$('.portfolioLinkImage a').fancybox();
	
	$('#testimonialsContainer').cycle({ 
	    fx:     'fade', 
	    speed:  'slow', 
	    timeout: 10000, 
	    next:   '#next', 
	    prev:   '#previous'
	});
	
	
	//Features
	//Tab the demos
	$('#features').tabs();
	
	//Dialog
	$('#dialogDemo').dialog({ autoOpen: false });
	
	$('#dialogDemoLink').click(function(){
		$('#dialogDemo').dialog('open');
	});
	
	//Lightbox
	$('#featureLightbox a').lightBox();
	
	//Cycle
	$('#featureCycle').cycle();
	
	$('#featureCycle a').lightBox();
	
	$('#cycleCover').click(function(){
		$('#featureCycle').cycle({fx:'cover',
			    				 timeout: 2000});
	});
	$('#cycleFade').click(function(){
		$('#featureCycle').cycle({fx:'fade',
			 					  timeout: 2000});
	});
	$('#cycleToss').click(function(){
		$('#featureCycle').cycle({fx:'toss',
			 					  timeout: 2000});
	});
	$('#cycleZoom').click(function(){
		$('#featureCycle').cycle({fx:'zoom',
			 					  timeout: 2000});
	});
	
	//Gallery
	$('#featureGalleryList').galleria({
		history   : false, // deactivates the history object for bookmarking, back-button etc.
		clickNext : false, // helper for making the image clickable. Let's not have that in this example.
		
		onThumb : function(thumb) { // thumbnail effects goes here
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';
			
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			);
		}

	});
	

	//Draggable
	$('#draggable').draggable({
		helper: 'clone',
		revert: 'invalid',
		opacity: 0.6
	});
	
	//Droppable
	$("#droppable").droppable({
		accept: "#draggable",
		activeClass: 'ui-state-hover',
		drop: function(event, ui) {
			$(this).addClass('ui-state-highlight').find('p').html('Dropped!');
		}
	});

	//Sortable
	$('#sortable').sortable();

	//Datepicker
	$('#datepicker').datepicker();
		
	
	$('#title').keyup(function(){
		//Create SEO friendly URL on the fly
		$.ajax({
			type: "GET",
			cache: false,
			url: "/ajax/friendlyUrl.php?title=" + $(this).val(),
			success: function (msg){
			$('#seoTitle').val(msg);
			}
		});
	});
	
	//Blog
	$('#seoFriendlyDemoLink').fancybox();
	
	
});
