/**
 * This file contains the functionality for initializing all the scripts in the
 * site and also there are some main initial settings included here, such as
 * setting rounded corners automatically, setting the Twitter functionality,
 * etc.
 * 
 * @author Pexeto
 */

var Nex = {
	initSite : function() {
		// init the portfolio functionality
		portfolioSetter.init();

		// set the contact form functionality
		//tonicContactForm.set();

		// set the news functionality
		newsSetter.init();

		// load the slider
		Nex.loadSlider();

		// sets the colorbox lightbox
		$("a[rel='lightbox']").colorbox();

		// load the scrolling functionality
		$('#menu ul li a').click(function(event) {
			event.preventDefault();
			var rel = $(this).attr('rel');
			if (rel && $('#' + rel)[0]) {
				$.scrollTo($('#' + rel), 700);
			}
		});

		// fix the IE6 position fixed issue
		if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
			$("#menu_container").css("position", "absolute");
			$(window).scroll(function() {
				$("#menu_container").css("top", $(window).scrollTop() + "px");
			});
		}

		// set the Cufon font replacement
		this.setCufon();

		// set rounded corners
		window.onload = this.setDefaultRoundedCorners;

	},

	/**
	 * Loads the Nivo image slider.
	 */
	loadSlider : function() {
		// load the Nivo slider
		$(window)
				.load(function() {
					$('#slider').nivoSlider( {
						effect : 'random', // Specify sets like:
						// 'fold,fade,sliceDown'
						slices : 20,
						animSpeed : 800,
						pauseTime : 8000,
						startSlide : 0, // Set starting Slide (0 index)
						directionNav : false, // Next & Prev
						directionNavHide : true, // Only show on hover
						controlNav : true, // 1,2,3...
						controlNavThumbs : false, // Use thumbnails for
						// Control
						// Nav
						controlNavThumbsFromRel : false, // Use image rel for
						// thumbs
						keyboardNav : true, // Use left & right arrows
						pauseOnHover : true, // Stop animation while hovering
						manualAdvance : false, // Force manual transitions
						captionOpacity : 0.8, // Universal caption opacity
						beforeChange : function() {
						},
						afterChange : function() {
						},
						slideshowEnd : function() {
						} // Triggers after all slides have been shown
					});

					// remove numbers from navigation
						$('.nivo-controlNav a').html('');

						// center the slider navigation
						var slideNumber = $('.nivo-controlNav a').length;
						var slideLeft = 960 / 2 - slideNumber * 21 / 2;
						$('.nivo-controlNav:first').css( {
							left : slideLeft
						});

						// set corners to the slider
						$('#slider_container')
								.append(
										'<div id="corner_left_top"></div><div id="corner_right_top"></div><div id="corner_left_bottom"></div><div id="corner_right_bottom"></div>');
					});
	},

	/**
	 * Displays the latest tweet- when the Twitter icon in the main menu is
	 * hovered, a tooltip with the tweet is shown.
	 * @param username the Twitter username
	 */
	showTwitterStatus : function(username) {

		$
				.ajax( {
					url : "http://twitter.com/statuses/user_timeline/"
							+ username + ".json?callback=?&count=2",
					dataType : "json",
					timeout : 15000,

					success : function(data) {
						var bubble = $('<div id="twitter_status" class="twt"><div id="twitter_status_top" class="twt"></div><p>' + data[0].text + '</p></div>');

						$("#menu").append(bubble);
						$('#twitter_status').css( {
							marginTop : -10
						}).hide();
						$("#menu_icon_right").hover(
								function() {
									$("#menu").find('#twitter_status').stop()
											.show().animate( {
												marginTop : 0
											});
								},
								function() {
									$("#menu").find('#twitter_status').stop()
											.animate( {
												marginTop : -10
											}).hide();
								});
					}
				});
	},

	/**
	 * Sets the Cufon font replacement.
	 */
	setCufon : function() {
		Cufon.replace('h1');
		Cufon.replace('h2');
		Cufon.replace('h3');
		Cufon.replace('h4');
		Cufon.replace('h5');
		Cufon.replace('h6');
	},

	/**
	 * Calls the function to set the rounded corners by default to all the
	 * elements from the "rounded_corner" class.
	 */
	setDefaultRoundedCorners : function() {
		Nex.setRoundedCorners($('img.rounded_corner'));
		$('.rounded_corner').not('img').corner();

	},

	/**
	 * Sets rounded corners to images.
	 * @param obj an image element
	 */
	setRoundedCorners : function(obj) {

		obj.each(function(i) {
			var width = $(this).width();
			var height = $(this).height();
			var src = $(this).attr('src');
			var divWrapper = $('<div></div>');

			var divClass = 'corner_wrapper';
			var classes = $(this).attr('class').split(' ');
			for ( var i = 0; i < classes.length; i++) {

				if (classes[i] !== 'rounded_corner') {
					divClass += ' ' + classes[i];
				}

			}

			$(this).addClass('hidden').wrap(divWrapper).hide();
			$(this).parent().css( {
				width : width,
				height : height,
				backgroundImage : 'url(' + src + ')'
			}).addClass(divClass).corner('7px');

		});

	}
};

