/**
 *	mediaburst
 *	JavaScript
 *
 *	@library		jQuery 1.4.4
 *	@since			30/11/2009
 *	@author			philthompson.co.uk
 *	@copyright		philthompson.co.uk/mediaburst.co.uk
 *
 */
 
jQuery.noConflict();
 
jQuery(document).ready(function(){

	setBrowser();

	blogAuthorComment();
	
	prepBlogImagesPopup();
	
	sourceCode();
	
	initColorbox();
	
	formFocus();
	
	forumForms();
	
	clickableBlocks();
	
	ieHr();
	
	sexyScroll();
	
	//showThanks();
	
	aboutGallery();
	
	emailSignupValidation();
	
	
});

/**
 *	setBrowser
 *	Append browser type to the body class
 *	Look we all no this isn't good pratice, so it won't be used
 *	for owt extensive @ see ieHr() at the bottom of this file
 */
function setBrowser(){

	var browser = null;
	var version = jQuery.browser.version.substr(0, 1);
	
	jQuery.each(jQuery.browser, function(i) {
		
		if(jQuery.browser.mozilla){
			browser = 'firefox';
		} else if(jQuery.browser.msie){
			browser = 'ie';
		} else if(jQuery.browser.opera){
			browser = 'opera';
		} else if(jQuery.browser.safari){
			browser = 'safari';
		}
		
	});
	
	browser = browser + " " + browser + version;
	
	jQuery('body').addClass(browser);
	
}


/**
 *	blogAuthorComment
 *	add right class to apply the CSS border-radius on author comments
 */
function blogAuthorComment(){
	
	jQuery('body.post li.comment-author-admin, body.post li.byuser').addClass('curve');
}


/**
 *	formFocus
 *	use CSS to highlight the relevant form label on focus
 */
function formFocus(){
	jQuery('input, select').focus(function(){
		jQuery('label').removeClass('active');
		var elementID = jQuery(this).attr('id');
		jQuery('label[for="' + elementID + '"]').addClass('active');
	});
}


/**
 *	prepBlogImagesPopup
 *	Add/remove releavnt class so colorbox will popup links
 */
function prepBlogImagesPopup(){

	var extensions = new Array();
	extensions[0] = '.png';
	extensions[1] = '.gif';
	extensions[2] = '.jpg';

	jQuery('body.post .entry a').each(function(){
		var linkHref = jQuery(this).attr('href');
		if(linkHref.indexOf(extensions[0]) > 0 || linkHref.indexOf(extensions[1]) > 0 || linkHref.indexOf(extensions[2]) > 0){
			jQuery(this).addClass('popup').attr('rel', 'gallery');
		}
	});
	
	jQuery('body.post .sociable a').each(function(){
		jQuery(this).removeClass('popup').attr('rel', '');
	});
	
}

/**
 *	initColorbox()
 *	Add hooks to certain links so colorbox works
 *	Warning: lightboxes fall over in mobile browsers 
 */
function initColorbox(){
	
	// don't run if it's a mobile browser
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf('mobile') == -1) {

		jQuery('a.popup').colorbox({
			maxWidth: 860,
			maxHeight: 600,
			scalePhotos: false
		});
	}
}


/**
 *	forumForms
 */
function forumForms(){
	jQuery('form.postform').addClass('default');
	jQuery('body#topic-page .post').addClass('curve');
}


/**
 *	clickableBlocks
 *	make elements marked as 'clickable' 
 *	go to the first link in them on click
 */
function clickableBlocks(){
	
	/*jQuery('#FeaturedStudies li, #APIProductsList li').addClass('clickable');

	jQuery('.clickable img').click(function(){
		var link = jQuery(this).parent().find('a:first').attr('href');
		window.location = link;
	});*/
}

/**
 *	ieHr
 *	Replace horizontal rules (<hr>) with divs for IE6 and IE7
 */
function ieHr(){
	jQuery('body.ie6 hr, body.ie7 hr').each(function(){
		jQuery(this).wrap('<div class="hr" />');
	});
	
}


/**
 *	sourceCode
 *	Improve sorucecode listings - add a clickbale link which
 *	pops up the raw code in a new window
 */
function sourceCode(){
	
	var link;
	var targetHref;
	var i = 1;
	var sourecode;
	var myWin;

	
	link = '<p class="sourcecode-options"><a href="#" class="download-sourcecode" title="Code opens in a new window">Download this code</a></p>';
	jQuery(link).insertBefore('pre.sourcecode');
	
	// give each source code sample an ID attribute
	jQuery('.hidden-sourcecode').each(function(){
		jQuery(this).attr('id', 'CodeSample-' + i);
		i++;
	});
	
	// makes ure link of the copy and download links oint
	// at the relevant hidden sourcecode
	jQuery('a.download-sourcecode').each(function(){
		targetHref = jQuery(this).parent().next().next('.hidden-sourcecode').attr('id');
		jQuery(this).attr('href', '#' + targetHref);
	});
	
	
	// Popup window of the code in plain text
	jQuery('a.download-sourcecode').click(function(){
		
		sourcecode = jQuery(jQuery(this).attr('href')).html();
	
		myWin = window.open("","myWin","menubar,scrollbars,height=400px,width=600px"); 
		myWin.document.write(
'<html><head><title>Source code</title></head><body><pre>' + sourcecode + '</pre></body></html>') ;
		return false;
	});

	
	
	
	
	
}

/**
 *	sexyScroll
 *	sexier scroll to the top of the page
 */
function sexyScroll(){
	jQuery('#PreFooter a').click(function(){
	jQuery('html, body').animate({
		scrollTop: 0
		}, 
		'slow');
		return false;
	});
}

/**
 *	showThanks()
 */
function showThanks(){
	jQuery('#ContactForm form').hide();
	jQuery('#ContactThanks').show();
}


/**
 *	aboutGallery()
 */
function aboutGallery(){

	var gallery = jQuery('body.about #Gallery');
	
	if(gallery.length === 0){
		return;
	}	
	
	
	// Mark active thumbnail
	jQuery('#Gallery li a[href="' + jQuery('#MainImage').attr('src') + '"]').parent().addClass('active');
	
	// Click one image and show and use it to replace the main image
	jQuery('#Gallery li a').click(function(e){		
		e.preventDefault();
		jQuery('#Gallery li.active').removeClass('active');
		jQuery(this).parent().addClass('active');
		jQuery('#MainImage').attr('src', jQuery(this).attr('href'));		
	});
	
	// Preload images
	jQuery('#Gallery li a').each(function(){
		jQuery('<img/>')[0].src = jQuery(this).attr('href');
	});
	
}

/**
 *	emailSignupValidation
 */
function emailSignupValidation(){
	
	var signupForm = jQuery('#SignUpForm'),
		signupEmail = jQuery('#Email'),
		validationMessage = 'Make sure you enter an email address.';

	if(signupForm.length === 0 || signupEmail.length === 0){
		return;
	}
	
	//alert('sign up validation!');
	
	// don't submit form for invalid emails
	signupForm.submit(function(e){
		
		
		
		if(signupEmail.val().length === 0){ // empty email address
			
			e.preventDefault();
			signupEmail.parent().addClass('error').append('<label class="error">' + validationMessage + '</span>');

		} else if(1){ // invalid email address
			
		}
		
		
	});
	
	// Hide error message as user goes back to edit mistake
	jQuery('#Email').focus(function(){
		jQuery(this).parent().find('label.error').slideUp().remove();
	});
	
	
}

