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

Template Name: 	OpenFolio
Version:	 	1.0
Release Date: 	August 28, 2010

File:       	script.js
Updated:    	2010-08-28

Copyright (c) 2010 Zoran Juric - http://themeforest.net/user/zoranjuric

-----------------------------------------------------------------------

WARNING! DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!

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

var nav_top = 0; 

var _$d = $(document);
var _$w = $(window);

// Preload images
function preloadImg(image) {
	var img = new Image();
	img.src = image;
}
preloadImg('img/nyro/ajaxLoader.gif');
preloadImg('img/nyro/prev.gif');
preloadImg('img/nyro/close.gif');
preloadImg('img/nyro/next.gif');

// Template init and setup 
_$d.ready(function(){ 
	
	// Add mozilla class to html
	if ($.browser.mozilla) 
		$('html').addClass('mozilla'); /* Can also add: webkit, opera, msie */
	
    // Open an external link in a new window
    $('a[href^="http://"]').filter(function () {
        return this.hostname && this.hostname !== location.hostname;
    }).attr('target', '_blank');

	// Setup navigation
	if ($("nav a").length) {
		$("nav a").click(function (event) {
			event.preventDefault(); 
			$('nav').find('li').removeClass('active');
			var self = $(this);
			var _offset = $(self.attr('href')).offset();
			$("html, body").animate({ scrollTop: _offset.top }, "slow");
			self.parent().addClass('active');
		});
		
		_offset = $('nav').offset();
		nav_top = _offset.top;
	 
		// onScroll event - do we need to show the floating menu?
		_$w.scroll(function () {
			var wst = _$w.scrollTop();
			
			var nav_menu = $('nav ul');
			if (wst >= nav_top) {
				if (!$(nav_menu).hasClass('floating')) {
					$(nav_menu).addClass('floating');
					// IE7 z-index stacking order fix
					if (jQuery.browser.msie && (parseInt(jQuery.browser.version) == 7)) {
						var zIndexNumber = 999;
						$('div.wrapper').each(function() {
							$(this).css('zIndex', zIndexNumber);
							zIndexNumber -= 1;
						});
					}
				}
			} else {
				if ($(nav_menu).hasClass('floating')) {
					$(nav_menu).removeClass('floating');
					$('nav').find('li').removeClass('active');
				}
			}
		});
	}
	
	// Image slider
	if ($('#slider').length)
		$('#slider').nivoSlider({
			effect: 'random',
			animSpeed: 1500,
			pauseTime: 5000,
			startSlide: 0,
			directionNav: true,
			controlNav: true,
			keyboardNav: false,
			pauseOnHover: false
		});
		
	// Featured images carousel
	if ($("#featureCarousel").length) {
		$("#featureCarousel").featureCarousel({
			counterStyle: 2,
			autoPlay: 3000,
			carouselSpeed: 500,
			sidePadding: 10
		});
		// centering...
		$('.blipsContainer').css('left', ((960 - ($('#featureCarousel .feature').length * 12)) / 2) + 'px');
	}
	
	// Any content slider
	if ($('.anythingSlider').length)
		$('.anythingSlider').anythingSlider({
			easing: "easeInOutExpo",
			autoPlay: true,
			delay: 3000,
			startStopped: false,
			animationTime: 600,
			hashTags: false,
			buildNavigation: false,
			pauseOnHover: true,
			startText: "", 
			stopText: "",
			navigationFormatter: function(index, panel) { return "&nbsp;"; }
		});

	
	// Portfolio item animations
	$('.boxgrid').hover(function(){
		$(".boxcaption", this).stop().animate({top: '76px', opacity: .9}, {queue:false, duration:300});
	}, function() {
		$(".boxcaption", this).stop().animate({top: '180px', opacity: 0}, {queue:false, duration:300});
	});

	// Setup nyroModal popup window
	$.nyroModalSettings({
		debug: false,
		zIndexStart: 10000 /* Otherwise IE7 will not show the modal window properly */
	}); 
	
	// News headlines carousel setup
	jQuery('#headlines li:even').addClass('odd');
    jQuery('#headlines').jcarousel({
        vertical: true
    });
	jQuery('#headlines a').click(function(){
		$('.news_entry').hide();
		$($(this).attr('href')).fadeIn('fast');
		$('#news_container').jScrollPane();
		return false;
	});
	jQuery('#headlines a:first').click();
	
	// Validate form on keyup and submit
	$("#contactform").validate({
		rules: {
			name: "required",
			contactemail: {
				required: true,
				email: true
			},
			comments: "required"
		},
		messages: {
			name: "Enter your name",
			contactemail: {
				required: "Please enter a valid email address",
				minlength: "Please enter a valid email address"
			},
			comments: "Your comment/question is missing"
		},
		// the errorPlacement has to take the layout into account
		errorPlacement: function(error, element) {
			error.insertAfter(element.parent().find('label:first'));
		},
		// specifying a submitHandler prevents the default submit
		submitHandler: function() {
			// remove previous error message
			$('#contactform p.error').remove();
			$('#contactform input.btn').fadeOut('fast');
			// submitting data
			jQuery.ajax({
				type: "POST",
				url: 'contact.php', 
				data: $('#contactform').serialize(),
				dataType: 'text',
				success: function(msg){
					$('#contactform input.btn').fadeIn();
					if (msg) {
						$('#contactform').prepend('<p class="error">' + msg + '</p>');
					} else {
						var parent = $('#contactform').parent();
						
						$('#contactform').remove();
						parent.append('<p><b>Message successfully sent.</b></p><p>I very much enjoy interacting with like minded individuals and hearing new business opportunities. Thanks for contacting me. I\'ll be in touch shortly.');
					}
				}
			});
		},
		// set new class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("ok");
		}
	});

	// Style switcher - remove from your final template
	$("div.panel_button").click(function(){
		$("div#panel").animate({
			height: "100px"
		})
		.animate({
			height: "86px"
		}, "fast");
		$("div.panel_button").toggle();
	});
	$("div#hide_button").click(function(){
		$("div#panel").animate({
			height: "0px"
		}, "fast");
	});
	$('div#panel_contents a').click(function() {
		chooseStyle($(this).attr('rel'), 60);
		return false;
	});

	/**
	 * Google map setup
	 * Coordinates: -37.812332, 144.968956
	 * Zoom level: 16
	 */
	if ($('#gmap').length && (typeof google !== "undefined")) {
		var latlng = new google.maps.LatLng(33.605989,-78.97213);
		var mapOptions = {
			zoom: 10,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			scrollwheel: false
		};
		map = new google.maps.Map(document.getElementById("gmap"), mapOptions);
		var marker = new google.maps.Marker({
			position: latlng, 
			map: map
		});
	}
	
});

// background image cache bug for ie6. www.mister-pixel.com/#Content__state=
/*@cc_on   @if (@_win32) { document.execCommand("BackgroundImageCache",false,true) }   @end @*/

