jQuery.noConflict();

jQuery(document).ready(function() {	
											
	/* Cufon font replace */
	Cufon.replace('h1, h2, h3, h4, h5, h6, .navfont', { fontFamily: 'Bebas Neue', hover: false, textShadow: '#fff 1px 1px'});
	Cufon.replace('.dividerbig, .dividersmall', { fontFamily: 'Bebas Neue', hover: false, textShadow: '#fff 1px 1px'}); 
	Cufon.replace('.footertitle_white, .footertitle_blue', { fontFamily: 'Bebas Neue', hover: false, textShadow: '#000 1px 1px'}); 	
				      
	/* Image Captions */			
	jQuery("img.captionmeblue").each(function(){
		var captiontext = jQuery(this).attr('title');
		jQuery(this).wrap("<div class='imgpost'></div>");
		jQuery(this).parent().append("<div class='thecaptionblue'>" + captiontext + "</div>");
	});
	jQuery("img.captionmegrey").each(function(){
		var captiontext = jQuery(this).attr('title');
		jQuery(this).wrap("<div class='imgpost'></div>");
		jQuery(this).parent().append("<div class='thecaptiongrey'>" + captiontext + "</div>");
	});
	jQuery("img.captionmered").each(function(){
		var captiontext = jQuery(this).attr('title');
		jQuery(this).wrap("<div class='imgpost'></div>");
		jQuery(this).parent().append("<div class='thecaptionred'>" + captiontext + "</div>");
	});

	/* Hover image opacity */
	jQuery("a[rel^='fadeimg'] img").hover(function() {
		jQuery(this).stop().fadeTo("fast", 0.5); 
	},function(){
		jQuery(this).stop().fadeTo("fast", 1.0); 
	});
	
	/* Pretty Photo */
	addPrettyPhoto();
	
	/* Load Facebook and Twitter Widgets */
	addFacebook();
	addTwitter();
	
	/* Youtube Video */
	addYoutubeVideo();
	
	/* Blog Mini Sidebar Slider */
	addSidebarSlider();
	
	/* Activate smooth tabs */
	if(jQuery('.smoothTabs').length != 0){
		jQuery('.smoothTabs').smoothTabs(200);
	}
	
	/* Portfolio Quicksand */
	addPortfolio('4');
	addPortfolio('3');
	addPortfolio('2');
	addPortfolio('1');
	
	/* Contact Form */
	if(jQuery('#contactform').length != 0){
		addForm('#contactform');
	}
	
	/* Newsletter Subscription */
	if(jQuery('#newsletterform').length != 0){
		addForm('#newsletterform');
	}
	
	/* Blog Comments */
	if(jQuery('#replyform').length != 0){
		addForm('#replyform');
	}
});

jQuery(window).load(function() {
	
	/* Nivo Slider */
	if(jQuery('#slider').length != 0){
		jQuery('#slider').nivoSlider({
			effect: 'random',
			slices: 10,
			boxCols: 5,
			boxRows: 4,
			animSpeed: 500,
			pauseTime: 5000,
			startSlide: 0,
			directionNav: false,
			directionNavHide: true,
			controlNav: true,
			controlNavThumbs: true,
			controlNavThumbsFromRel: false,
			controlNavThumbsSearch: '.jpg',
			controlNavThumbsReplace: '_thumb.jpg',
			keyboardNav: true,
			pauseOnHover: true,
			manualAdvance: false,
			captionOpacity: 0.9,
			prevText: 'Prev',
			nextText: 'Next',
			beforeChange: function(){},
			afterChange: function(){},
			slideshowEnd: function(){},
			lastSlide: function(){},
			afterLoad: function(){},
			customChange: function(){
				Cufon.replace('.slidercaption', { fontFamily: 'Bebas Neue', hover: true });
			}
		});
	}
	
	/* Piecemaker Slider */
	addPiecemaker();
	
	/* Google Maps */
	loadGoogleMaps();
	
});

function addPrettyPhoto() {
	/* PrettyPhoto init */
	jQuery("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'facebook',
		overlay_gallery: true,
		show_title: false,
		hideflash: true
	});
	
	/* PrettyPhoto hover image opacity */
	jQuery("a[rel^='prettyPhoto'] img").hover(function() {
		jQuery(this).stop().fadeTo("fast", 0.5); 
	},function(){
		jQuery(this).stop().fadeTo("fast", 1.0); 
	});	
}

function addFacebook() {
	/* Facebook */
	if(jQuery('#facebooklike').length != 0){
		window.fbAsyncInit = function() { 
			jQuery("#facebooklike").append('<fb:like href="" layout="button_count" show_faces="false" width="90"></fb:like>'); 
			jQuery('#facebooklike').delay(500).fadeIn(1000);
		};
		var fbscript = document.createElement('script'); 
		fbscript.type = 'text/javascript'; 
		fbscript.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1'; 
		document.getElementsByTagName('head')[0].appendChild(fbscript);
	}
}

function addTwitter() {
	/* Twitter */
	if(jQuery('#twittertweet').length != 0){
		jQuery("#twittertweet").append('<a href="http://twitter.com/share" class="twitter-share-button" data-url="#" data-text="Notorious Creative Portfolio Template" data-count="horizontal">Tweet</a>'); 
		jQuery('#twittertweet').delay(500).fadeIn(1000);
		var twscript = document.createElement('script'); 
		twscript.type = 'text/javascript'; 
		twscript.src = 'http://platform.twitter.com/widgets.js'; 
		document.getElementsByTagName('head')[0].appendChild(twscript);
	}
}

function addYoutubeVideo() {
	/* Youtube Video */
	if(jQuery('#youtubevideo').length != 0){
		jQuery("#youtubevideo").append('<object type="application/x-shockwave-flash" style="width:670px; height:404px;" data="http://www.youtube.com/v/5Ajm7WdWeNw?hl=en&amp;fs=1&amp;hd=1">'+
			'<param name="movie" value="http://www.youtube.com/v/5Ajm7WdWeNw?hl=en&amp;fs=1&amp;hd=1" />'+
			'<param name="allowFullScreen" value="true" />'+
			'</object>'); 
	}
}

function addPiecemaker(){
	/* 3D Slider */
	if(jQuery('#piecemaker').length != 0){
		jQuery("head").append('<script>swfobject.embedSWF("images/piecemaker2/piecemaker.swf" , "piecemaker", "920", "445", "10", null, flashvars, params, null)</script>');
	}
}

function addSidebarSlider(){
	/* Blog Sidebar Slider */
	if(jQuery('#sidebarslider').length != 0){
		jQuery('#sidebarslider').tinycarousel({ 
			controls: false, 
			pager: true, 
			interval: true, 
			intervaltime: 3000
		});	
	}
}

function initGoogleMaps() {
	/* Google Maps Init */
	var myLatlng = new google.maps.LatLng(50.935816, 6.961212);
	var myOptions = {
		zoom: 14,
		center: myLatlng,
		popup: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("googlemap"), myOptions);
}
  
function loadGoogleMaps() {
	/* Google Maps Load */
	if(jQuery('#googlemap').length != 0){
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initGoogleMaps";
		document.body.appendChild(script);
	}
}

function addPortfolio(portfolioid) {
	/* Portfolio Quicksand */
	var $list = jQuery('#portfoliolist'+portfolioid+'column');
	
	if(jQuery($list).length != 0){
		
		var $data = $list.clone();
	
		jQuery('.portfoliofilter li').click(function(e) {
	
			jQuery(".portfoliofilter li a").addClass("portfoliobutton_noselect");
			jQuery(".portfoliofilter li a").removeClass("portfoliobutton");	
			jQuery(this).children('a').removeClass("portfoliobutton_noselect");
			jQuery(this).children('a').addClass("portfoliobutton");
	
			var filterClass = jQuery(this).attr('class');
	
			if (filterClass == 'all') {
				var $filteredData = $data.find('.portfolioentry'+portfolioid+'column');
			} else {
				var $filteredData = $data.find('.portfolioentry'+portfolioid+'column[data-type=' + filterClass + ']');
			}
			
			jQuery($list).quicksand($filteredData, {
				duration: 500,
				easing: 'easeInOutQuad',
				adjustHeight: 'dynamic',
				enhancement: function() {
					if(typeof Cufon !== 'undefined'){
					   Cufon.refresh();
					}
				}
			}, function(){
				addPrettyPhoto();
			});
			
			return false;
		});
	}
}

/* Contact Form */
function addForm(formtype) {

	var formid = jQuery(formtype);
	var emailsend = false;
	
	formid.find("button[name=send]").click(sendemail);
	
	function validator() {
		
		var emailcheck = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		var othercheck = /.{4}/;
		var noerror = true;
		
		formid.find(".requiredfield").each(function () {
													 
			var fieldname = jQuery(this).attr('name');
			var value = jQuery(this).val();

			if(fieldname == "email"){
				if (!emailcheck.test(value)) {
					jQuery(this).addClass("formerror");
					noerror = false;
				} else {
					jQuery(this).removeClass("formerror");
				}	
			}else{
				if (!othercheck.test(value)) {
					jQuery(this).addClass("formerror");
					noerror = false;
				} else {
					jQuery(this).removeClass("formerror");
				}	
			}
		})
		
		if(!noerror){
			formid.find(".errormessage").fadeIn();
		}
		
		return noerror;
	}
	
	function resetform() {
		formid.find("input").each(function () {
			jQuery(this).val("");	
		})
		formid.find("textarea").val("");
		emailsend = false;
	}
	
	function sendemail() {
		formid.find(".successmessage").hide();
		var phpfile = "";
		if(formtype=="#contactform"){
			phpfile = "php/contact.php";
		}else if(formtype=="#newsletterform"){
			phpfile = "php/signup.php";
		}else{
			phpfile = "";
		}
		if (validator()) {
			if(!emailsend){
				emailsend = true;
				formid.find(".errormessage").hide();
				formid.find(".sendingmessage").show();
				jQuery.post(phpfile, formid.serialize(), function() {
					formid.find(".sendingmessage").hide();
					formid.find(".successmessage").fadeIn();
					resetform();
				});
			}
		} 
		return false
	}
}

/* jQuery Smooth Tabs version 1.1.0 */
(function($) {  
	$.fn.smoothTabs = function(fadeSpeed) {
		// Visible index
		var currentIndex = 0;
		// Clicked tab class
		var smoothTabsLiCurrent = 'smoothTabsLiCurrent';
		// Hidden div class
		var smoothTabsDivHidden = 'smoothTabsDivHidden';
		// Visible div class
		var smoothTabsDivVisible = 'smoothTabsDivVisible';
		// Current hash if any.
		var hash = document.location.hash.substr(1, document.location.hash.length);

		if (hash && $('#'+hash, this).size()) {
			currentIndex = $('#'+hash, this).index();
		}

		// Makes first tab current, hides all divs and fades in the first one
		this.each(function() {
			$('ul li:eq('+currentIndex+')', this).addClass(smoothTabsLiCurrent);
			$(this).children("div").addClass(smoothTabsDivHidden);
			$('div:eq('+currentIndex+')', this).fadeIn(fadeSpeed)
								.addClass(smoothTabsDivVisible)
								.removeClass(smoothTabsDivHidden);
		});

		// Tab click function
		$('ul li', this).click(function(){
			var $parentUl = $(this).parent();
			var $parentDiv = $($parentUl).parent();
			$('li', $parentUl).removeClass(smoothTabsLiCurrent);
			$(this).addClass(smoothTabsLiCurrent);
			var $clickedIndex = $('li', $parentUl).index(this);
			var $currentDiv = $('div', $parentDiv).get($clickedIndex);
			
			// If current tab is clicked - we're done
			if ($($currentDiv).attr('class') == smoothTabsDivVisible) {
				return false;
			}
			
			// Current div is replaced by the selected one
			$('.'+smoothTabsDivVisible, $parentDiv).fadeOut(fadeSpeed, function(){
				$($currentDiv).fadeIn(fadeSpeed).addClass(smoothTabsDivVisible).removeClass(smoothTabsDivHidden);
			});
			$('.'+smoothTabsDivVisible, $parentDiv).removeClass(smoothTabsDivVisible).addClass(smoothTabsDivHidden);
		});
	};
})(jQuery);
  
