/**
 * VoiceAbility : Global JS
 * @version 1.0
 * @requires jQuery Core 1.3+ - http://www.jquery.com/
*/
/*jslint bitwise: true, eqeqeq: true, passfail: false, nomen: false, plusplus: false, undef: true, laxbreak: true */
/*global window, document, $, jQuery, self */

/**
* @namespace Root namespace for holding all objects created for VoiceAbility.
*/
var VA = window.VA || {};

// add hasJS CSS file
document.write('<link href="/assets/style/has_js.css" type="text/css" rel="stylesheet" />');

VA.menu = (function() {
	return {
        init: function() {
			var $mainNav = $('ul.main-nav'),
				dropdownTimeout;
			
            // initialise main nav dropdowns
    		$mainNav.superfish({
				autoArrows: false,
				dropShadows: false
			});
			
			// add classes to nav items
			$mainNav.find("li.level_1").each(function() {
				$(this).addClass($(this).children("a").text().toLowerCase().replace(/ /g, "-"));
			});
			
			// add functionality for What We Do dropdown
			$mainNav.find("li.what-we-do").hover(
				function() {
					$("#what-we-do").fadeIn();
				},
				function() {
					dropdownTimeout = window.setTimeout(function() {
						$("#what-we-do").hide();
					}, 100);
				}
			);
			
			$("#what-we-do").hover(
				function() {
					window.clearTimeout(dropdownTimeout);
					$mainNav.find("li.what-we-do").addClass("sfHover");
				},
				function() {
					$(this).hide();
					$mainNav.find("li.what-we-do").removeClass("sfHover");
				}
			);
		}
	};
})();

VA.searchBox = (function() {    
    function setupDefaultText() {
        // get forms and inputs
        var $forms = $("#headerSearch"),
            $input = $forms.find("#keywords"),
            labelText = $input.siblings("label").text();
            
        // add label text to search input
        $input.val(labelText);
        
        // add class to change text colour for input text in 'hint' mode
        $input.addClass("default");
        
        // add event handlers
        $input.focus(function() {
            // if default text in input matches label text, remove on focus  
            if (this.value === labelText) {
                this.value = "";
            }
            $(this).removeClass("default");
        });
        $input.blur(function() {
            // if input value is blank, replace with default text on blur
            if (this.value === "") {
                this.value = labelText;
            }
            $(this).addClass("default");
        });  
    }

    return {
        init: function() {
            // setup search box default text functionality
            setupDefaultText();
        }
    };
})();

VA.twitter = (function() {
    function buildFeed() {
        // Get latest 3 tweets
        if ($(".twitter").length > 0) {
            $.jTwitter('voiceability_va', 3, 
                function (data) {
                    var $posts = $("#twitterFeed");
                    $posts.empty();
                    $.each(data, function(i, post) {
                        $posts.append('<li class="post">' + post.text + '</li>');
                    });
                }
            );
        }
    }

    return {
        init: function() {
            buildFeed();
        }
    };
})();

VA.bookmarking = (function() {
    function buildBookmarks() {
        // if news article page, inject bookmark span
        $("div.article p.categories").after("<div class='bookmarkWrapper clearfix'><p class='label'><strong>Bookmark this article: </strong><div class='bookmark clearfix'></div></div>");
        // add social bookmarking
        $('div.bookmark').bookmark({sites: ['delicious', 'digg', 'linkedin', 'facebook']});
    }

    return {
        init: function() {
            buildBookmarks();
        }
    };
})();

VA.validation = (function() {
    return {
        init: function() {
            $("div.validate").find("form").validate();
        }
    };
})();

VA.textResize = (function() {
    function setupResizing() {        
        // reset font size
        var original = $('html').css('font-size');
        $("a.reset").click(function() {
            $('html').css('font-size', original);
        });
        
        // increase font size
        $("a.increase").click(function() {
            // get current font size and calculate new size
            var current = $('html').css('font-size'),
                currentNum = parseFloat(current, 10),
                newSize = currentNum * 1.2;
            
            // set new font size
            $('html').css('font-size', newSize);
            
            return false;
        });
        
        // decrease font size
        $("a.decrease").click(function() {
            // get current font size and calculate new size
            var current = $('html').css('font-size'),
                currentNum = parseFloat(current, 10),
                newSize = currentNum * 0.8;
        
            // set new font size
            $('html').css('font-size', newSize);
            
            return false;
        });
    }

    return {
        init: function() {
            setupResizing();
        }
    };
})();

VA.featured = (function() {
	return {
		init: function() {
			if ($("#slideshow").length > 0) {
				$('#slideshow').nivoSlider({
					effect: 'fade',
					directionNav: false,
					pauseTime: 12000
				});
				
			}
		}
	};
})();

VA.Helpers = {
	equalHeights: function($group) {
		var tallest = 0;
		$group.each(function() {
			var thisHeight = $(this).innerHeight();
			if (thisHeight > tallest) {
				tallest = thisHeight;
			}
		});
		
		$group.height(tallest);
	},
	parentHeights: function($group) {
		$group.each(function() {
			var $element = $(this),
				$parent = $element.parent(),
				height = $parent.height() - (parseInt($element.css("paddingTop").replace(/px$/, ""), 10) + parseInt($element.css("paddingBottom").replace(/px$/, ""), 10));
			
			$element.height(height);
		});
	}
}

/*
 * SETUP DOM READY AND LOAD EVENTS HERE
 */
$(document).ready(function () {
	VA.menu.init();
	VA.searchBox.init();
	VA.twitter.init();
	VA.bookmarking.init();
	VA.validation.init();
	VA.textResize.init();
	VA.featured.init();
	
	VA.Helpers.equalHeights($("div.content.home .colLayout .column"));
	VA.Helpers.parentHeights($("div.panel.twitter, div.panel.story"));
});

