/* -------------------------------------------------------------- 

main.js by AREA 17

Version date: 27th Sept 2009
Version 0.1 Alpha

-------------------------------------------------------------- */

/* Setting up variables
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
var isSafari = false;
var isSafari3 = false;
var isSafari4 = false;
var isIE = false;
var isIE6 = false;
var isIE7 = false;
var isIE8 = false;
var isMozilla = false;
var isOpera = false;
var isMac = false;
var isIphone = false;

/* What to do when DOM is ready
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
$(document).ready(function(){
	runOnDOMready();
});

/* Run when DOM is ready
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function runOnDOMready() {
	setUpClasses();
	news();
	customDropDown();
	keyPress();
	sectionColHeights();
	toggleFilterTopics();
	searchBox();
	comments();
	resources();
	pagination();
	portfolio();
	subscribe();
	lplogin();
}

/* Setting classes to handle cross browser dom parsing
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function setUpClasses() {
	
	isIE = jQuery.browser.msie;
	isSafari = jQuery.browser.safari;
	
	if (isIE) {
		
		$('li:first-child').addClass("first");
		$('li:last-child').addClass("last");
		$('.aside > .tags-alt').addClass("first");
		
		// styling specific lists
		if ($('.mdash')[0]) {
			$('.mdash').find('li').prepend('&mdash;&nbsp;');
		}
		
		// styling specific lists
		if ($('div.tags').hasClass("inline")) {
			$('div.tags').find('li:not(.last)').find('a').after(',');
		}
		
		// news detail
		$('aside').find('ul:nth-child(2)').addClass("second");
		
	}
	
	if ($('.news')[0]) {
		$('dd:nth-child(1)').addClass("first");
		$('dt:nth-child(2)').addClass("first");
	}
	
	if ($('.zebra-alt')[0]) {
		$('.zebra-alt > ul > li:nth-child(odd)').addClass("odd");
	}
	
	// blockquote
	if ($('blockquote').text() != "") {
		$('blockquote').children('span').prepend('\u201c');
		$('blockquote').children('span').append('\u201d');
	}
	
	tagsInline();
}

function tagsInline() {
	if ((!$("#portfolio-listing")[0]) && $('.tags-inline')[0]) {
		$('.tags-inline').find('li').not('li:last-child').find('a').parent().append('<span class="ietag">&#44;</span>');
	}
}


/* NEWS main functions
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function news() {
	
	$('#top').click(function(e) {
		e.preventDefault();
		$('html,body').animate({scrollTop: 0}, 350);
	});
}



/* Register keypress events
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function keyPress() {
	$(document).keydown(function(e) {
		switch(e.keyCode) {	
			// user pressed "top" arrow
			case 38: $('html,body').animate({scrollTop: 0}, 350);
			break;
		}
	});
}

/* Register blog feed subscription
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function subscribe() {
	if ($('#feedSubscribe')[0]) {
		var elem = $('#feedSubscribe');
		elem.toggle(function(e) {
			e.preventDefault();
			$('#feedForm').fadeIn();
		}, function(e) {
			e.preventDefault();
			$('#feedForm').fadeOut();
		});
		
		var elem2 = $('#submit');
		elem2.click(function(e) {
			var val = $("#feedForm input[type=text]").val();
			if ( val != "" && val != "Your email address")
				$('#feedForm').fadeOut();
		});
		
		if ($("#feedForm input[type=text]").val() != "Your email address") {
			$("#feedForm input[type=text]").addClass("searchFor");			
		}
		$("#feedForm input[type=text]").focus(function(e){
			if ($(e.target).val() == "Your email address") {
				$(e.target).val("").addClass("searchFor");			
			}
		});
		$("#feedForm input[type=text]").blur(function(e){
			if ($(e.target).val() == "") {
				$(e.target).val("Your email address").removeClass("searchFor");
			}
		});
		
	}
}


/* CustomDropDown for team page
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function customDropDown() {
	
	$("div.customDropDown").each(function (i) {
		
		$("this").find("h5").hover(function(){
			$(this).addClass("hover");
		}, function() {
			$(this).removeClass("hover");
		});
		//							 
		var ddShow = false;
		var ddInput = $(this).children()[0];
		var ddDiv = $(this).children()[1];
		//
		ddInput = $(ddInput);
		ddDiv = $(ddDiv);
		//
		ddDiv.hide();
		//
		var clickFunction = $(this).attr("id");
		//
		ddInput.click(
			function () {
				toggleDD();
			}
		);
		$(document).click(
			function (e) {
				if(!$(e.target).parents().hasClass("customDropDown")) {
					ddDiv.hide();
					ddDiv.removeClass("dd-open");
					ddDiv.prev().removeClass("dd-open");
				}
			}
		);
		//
		//
		function toggleDD() {
			ddDiv.toggle();
			ddDiv.toggleClass("dd-open");
			ddDiv.prev().toggleClass("dd-open");
		}
		//
		ddDiv.find("li:first").hide();
	});
}

/* Equals the heights of columns
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function sectionColHeights() {
	
	if ($('.section').find('.cols')) {
	
		var cols = $('.section .cols').not('.noequals .cols');
		cols.each(function(i,val){
			var heights = new Array();
			var nb_col = $(this).find("div.col").size();
			var max = 0;
			for (j= 1; j <= nb_col; j ++) {
				heights[j] = $(this).find("div.col:nth-child(" + j + ")").height();
				if (heights[j] > max) { 
					max = heights[j];
				}
			}
			$(this).find("div.col").height(max);
			$(this).find("p.more").css({bottom: '0', left: '0', width: '100%', position: 'absolute'});
		});
	
	}
}

/* Show/hide toggle for the topics (used in Portfolio)
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function toggleFilterTopics() {
	
	var isDown = false;
	
	$('#filter-by-topics').click(function(e) {
		if (!isDown) {
			e.preventDefault();
			$('#topicsnav').slideDown();
			$('#filter-by-topics').css("backgroundPosition", "0 -25px");
			isDown = true;	
		} else {
			e.preventDefault();
		 	$('#topicsnav').slideUp();
			$('#filter-by-topics').css("backgroundPosition", "0 0");
			isDown = false;
		}
		
	});
	
	$('#filter-by-topics').hover(function() {
		if (!isDown) {
			$('#filter-by-topics').css("backgroundPosition", "0 -25px");
		} 
	}, function() {
		if (isDown) {
			$('#filter-by-topics').css("backgroundPosition", "0 -25px");
		} else {
			$('#filter-by-topics').css("backgroundPosition", "0 0");
		}
	});
	
	if ($('#topicsnav').find('li').hasClass('current')) {
		if (!isDown) {
			$('#topicsnav').show();
			$('#filter-by-topics').css("backgroundPosition", "0 -25px");
			isDown = true;
		}
	}
	
	var show_topics = getUrlVars()["show_topics"];
	if ("1" === show_topics) {
		if (!isDown) {
			$('#topicsnav').show();
			$('#filter-by-topics').css("backgroundPosition", "0 -25px");
			isDown = true;
		}
	}
	
}


/* Handles the search box (blog)
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function searchBox() {

	if ($("#search-blog input[type=text]").val() != "Search the blog") {
		$("blog input[type=text]").addClass("searchFor");			
	}
	$("#search-blog input[type=text]").focus(function(e){
		if ($(e.target).val() == "Search the blog") {
			$(e.target).val("").addClass("searchFor");			
		}
	});
	$("#search-blog input[type=text]").blur(function(e){
		if ($(e.target).val() == "") {
			$(e.target).val("Search the blog").removeClass("searchFor");
		}
	});
}

/* Handles the comments css (blog)
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function comments() {
	
	$('.comments').find('.comment:last-child').css("border", "none");
	var nb = $('.paginator').find('li').size();
	var pos = 0;
	for (var i=1; i<=nb ; i++) {
		if ($('.paginator').find('li:nth-child(' + i + ')').hasClass('paginator')) {
			pos = i - 1;
			$('.paginator').find('li:nth-child(' + pos + ')').find('a').css('border', 'none');
			break;
		}
	}
	
	
	
	$('#recaptcha_reload').attr("src", "/assets/images/icons/recaptcha_reload.jpg");
	$('#recaptcha_reload').attr("width", "17");
	$('#recaptcha_reload').attr("height", "17");
	
	$('#recaptcha_whatsthis').attr("src", "/assets/images/icons/recaptcha_info.jpg");
	$('#recaptcha_whatsthis').attr("width", "16");
	$('#recaptcha_whatsthis').attr("height", "17");
	
	var _tmp = $('ul.contact').find('li.notify').html();
	$('ul.contact').append('<li class="notify">' + _tmp + '</li>');
	$('ul.contact').find('li.verification').prev('li.notify').remove();
	
	$('.stbuttontext').text('Share this post');
	$('#sharethis_0').fadeIn();
	
	$('#submit').hover(function() {
		$('#submit').css('background-position', '0 -25px');
	}, function() {
		$('#submit').css('background-position', '0 0');
	});
	
	
	// Handling the most popular tags on blogs
	
	nb = $('ul.most-popular').find('li').size();
	var onlyFirstTenVisible = false; 
	// if we have more than 11 tags (including "Most Popular Tags")
	if ( nb > 11 ) {
		for (var i=12; i<=nb; i++) {
			$('.most-popular').find('li:nth-child(' + i + ')').not('li.all').hide();
		}
		onlyFirstTenVisible = true;
	}
	
	$('.all').click(function(e) {
		if (onlyFirstTenVisible) {
			e.preventDefault();
			for (var i=12; i<=nb; i++) {
				$('.most-popular').find('li:nth-child(' + i + ')').not('li.all').show();
			}
			$('.most-popular').find('.all').find('a').text('View less tags');
			onlyFirstTenVisible = false;	
		} else {
			e.preventDefault();
			for (var i=12; i<=nb; i++) {
				$('.most-popular').find('li:nth-child(' + i + ')').not('li.all').hide();
			}
			$('.most-popular').find('.all').find('a').text('View more tags');
			onlyFirstTenVisible = true;
		}
		
	});
	
	
}

/* Handles the resources css
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function resources() {
	
	$('#resources').find('.article-content').find('ul').find('li:last-child').css("border", "none");
	
}

/* Handles the blog pagination css
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function pagination() {
	
	var _parent = $('#blog-listing');
	var _next = _parent.find('.next');
	var _prev = _parent.find('.prev');
	var _label = _parent.find('.label');
	
	_next.hover(function() {
		_label.html("Older posts");
	}, function() {
		_label.html("");
	});
	
	_prev.hover(function() {
		_label.html("Newer posts");
	}, function() {
		_label.html("");
	});
	
}

/* Handles url parameters
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

/* Portfolio
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function portfolio()
{
    if ($('#portfolio-listing')[0]) {
		$('.company').each( function() {
			var _company = $(this);
			_company.find('.left').find('a').hover(function() {
				_company.addClass("company-hover");
			}, function() {
				_company.removeClass("company-hover");
			});
		});

	}
}


/* Limited login
–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/
function lplogin() {
	if ($('li.lppwd')[0]) {
		var elem = $('li.lppwd'),
			label = elem.find('label'),
			input = elem.find('input[type=password]');
		
		
		input.focus(function(e){
			if ($(e.target).val() == "") {
				label.hide();
			}
		});
		input.blur(function(e){
			if ($(e.target).val() == "") {
				label.show();
			}
		});
		
		label.click(function(e) {
			e.preventDefault();
			$(this).hide();
			input.focus();
		});
		
		$('input[type=submit]').hover(function() {
			$(this).addClass('submit_hover');
		}, function() {
			$(this).removeClass('submit_hover');
		});
		
	}
}



