if (typeof initDesc != 'undefined') {
	jQuery("#portfolio_desc").html(initDesc);
}

portcat = 1;
portfolioLoaded = false;

//preload
$.preloadImages = function()
{
	$.each (arguments,function (e)
	{
		$ ('<img src="' + this + '" />');
	});
}

$.preloadImages(themePath + "/images/portfolio_cat_biz.png");

//end of preload

jQuery(function($) {
	
	var thumbCount = 0;
	
	$('ul.gallery').galleria({
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : false, // helper for making the image clickable
		insert    : '#portfolio_main_content', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
			
			// fade in the image & caption
			if(! ($.browser.msie) ) { // FF/Win fades large images terribly slow;
				image.css('display','none').animate({opacity:'toggle' },350);
			}
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.65);
			
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
			
			// add a title for the clickable image
			//image.attr('title','Next >');

			//add the lightbox anchor
			if (toLightbox != 'no') {
				
				//check if array -- backwards compatibility
				if(toLightbox instanceof Array) {
					if ($.browser.msie) 
						$('.galleria_wrapper').html('<a href="' + toLightbox[0] + '" class="lightbox">' + $('.galleria_wrapper').html() + "</a>");
					else {
						$('.replaced').wrap('<a href="' + toLightbox[0] + '" class="lightbox">');
					}
					
					//write other lightbox links
					var hidden_html = "";
					
					for(var i=1;i<toLightbox.length;i++) {
						hidden_html += '<a href="' + toLightbox[i] + '" class="lightbox">a</a>';
					}
					$('#hidden_links').html(hidden_html);	
				}
				else {
					if ($.browser.msie) 
						$('.galleria_wrapper').html('<a href="' + toLightbox + '" class="lightbox">' + $('.galleria_wrapper').html() + "</a>");
					else {
						$('.replaced').wrap('<a href="' + toLightbox + '" class="lightbox">');
					}
					
					$('#hidden_links').html("");
				}
				
				//set up lightbox
				$('.lightbox').lightBox({
					imageBtnClose : themePath + "/images/lightbox_close.jpg",
					imageBtnNext : themePath + "/images/lightbox_next.jpg",
					imageBtnPrev : themePath + "/images/lightbox_prev.jpg",
					overlayOpacity : 0.85,
					imageLoading: themePath + "/images/loading.gif"
				}); // Select all links that contains lightbox in the attribute rel
			}
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.65';
			
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(700);
			
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.65); } // don't fade out if the parent is active
			);
			
			//when last thumb, remove loader
			thumbCount++;
			if( thumbCount == totalThumbCount )
			{
				$("#portfolio_cat_loading").html("").hide();
				$("#portfolio_cat").animate({opacity:1},350);
				portfolioLoaded = true;
			}
		}
	});
	
	//portfolio next/prev hover
	$("#portfolio_next").hover(function(){
		
		if (portcat == 1) {
			moves = webmoves;
		}
		else if (portcat == 2) {
			moves = printmoves;
		}
		else if (portcat == 3) {
			moves = bizmoves;
		}

		if( loc < moves )
		{
			$("#portfolio_next").fadeTo(100,1.0);
		}	
	},
	function(){
		if (portcat == 1) {
			moves = webmoves;
		}
		else if (portcat == 2) {
			moves = printmoves;
		}
		else if (portcat == 3) {
			moves = bizmoves;
		}
		
		if( loc < moves )
		{
			$("#portfolio_next").fadeTo(100,0.75);
		}	
	});
	
	$("#portfolio_prev").hover(function(){
		if( loc > 1)
		{
			$("#portfolio_prev").fadeTo(100,1.0);
		}
	},
	function(){
		if( loc > 1 )
		{
			$("#portfolio_prev").fadeTo(100,0.75);
		}
	});
	
	$("#portfolio_cat_print").click(function(){
		
		if (portfolioLoaded) {
		
			if (portcat == 1) {
				if (loc > 1) {
					$('#gallery_thumbs').css("left", "-47px")
				}
				
				$("#portfolio_cat_web").css("color", "#808080");
				$("#portfolio_cat_web_baloon").fadeTo(250, 0);
				$("#portfolio_cat_print_baloon").css("display", "block").fadeTo(250, 1)
				$("#portfolio_cat_print").css("color", "#FFFFFF");
				
				//swap galleries
				//$('#web_gallery').hide();
				$('#web_gallery').hide();
				//$('#print_gallery').fadeIn(350);
				$('#print_gallery').animate({
					opacity: 'toggle'
				}, 350);
				
				if(printmoves == 1)
					$('#portfolio_next').css("opacity", "0.3").css("cursor", "default");
				else
						$('#portfolio_next').css("opacity", "0.75").css("cursor", "default");
				$('#portfolio_prev').css("opacity", "0.3").css("cursor", "default");
				
				portcat = 2;
				loc = 1;
			}
			else 
				if (portcat == 3) {
					if (loc > 1) {
						$('#gallery_thumbs').css("left", "-47px")
					}
					
					$("#portfolio_cat_biz").css("color", "#808080");
					$("#portfolio_cat_biz_baloon").fadeTo(250, 0);
					$("#portfolio_cat_print_baloon").css("display", "block").fadeTo(250, 1)
					$("#portfolio_cat_print").css("color", "#FFFFFF");
					
					//swap galleries
					$('#biz_gallery').hide();
					//$('#print_gallery').fadeIn(350);
					$('#print_gallery').animate({
						opacity: 'toggle'
					}, 350);
					
					if(printmoves == 1)
						$('#portfolio_next').css("opacity", "0.3").css("cursor", "default");
					else
						$('#portfolio_next').css("opacity", "0.75").css("cursor", "default");
					$('#portfolio_prev').css("opacity", "0.3").css("cursor", "default");
					
					portcat = 2;
					loc = 1;
				}
			
		}
	});
	
	$("#portfolio_cat_web").click(function(){
		if (portfolioLoaded) {
			if (portcat == 2) {
				if (loc > 1) {
					$('#gallery_thumbs').css("left", "-47px")
				}
				
				$("#portfolio_cat_print").css("color", "#808080");
				$("#portfolio_cat_print_baloon").fadeTo(250, 0);
				$("#portfolio_cat_web_baloon").css("display", "block").fadeTo(250, 1);
				$("#portfolio_cat_web").css("color", "#FFFFFF");
				
				//swap galleries
				$('#print_gallery').hide();
				//$('#print_gallery').fadeIn(350);
				//$('#web_gallery').children().css("margin-left","7.5px").css("margin-top","7.5px"); //fix
				$('#web_gallery').animate({
					opacity: 'toggle'
				}, 350);
				
				if(webmoves == 1)
					$('#portfolio_next').css("opacity", "0.3").css("cursor", "default");
				else
					$('#portfolio_next').css("opacity", "0.75").css("cursor", "default");
				$('#portfolio_prev').css("opacity", "0.3").css("cursor", "default");
				
				portcat = 1;
				loc = 1;
			}
			
			else 
				if (portcat == 3) {
					if (loc > 1) {
						$('#gallery_thumbs').css("left", "-47px")
					}
					
					$("#portfolio_cat_biz").css("color", "#808080");
					$("#portfolio_cat_biz_baloon").fadeTo(250, 0);
					$("#portfolio_cat_web_baloon").css("display", "block").fadeTo(250, 1);
					$("#portfolio_cat_web").css("color", "#FFFFFF");
					
					//swap galleries
					$('#biz_gallery').hide();
					//$('#print_gallery').fadeIn(350);
					$('#web_gallery').animate({
						opacity: 'toggle'
					}, 350);
					
					if(webmoves == 1)
						$('#portfolio_next').css("opacity", "0.3").css("cursor", "default");
					else
						$('#portfolio_next').css("opacity", "0.75").css("cursor", "default");
					$('#portfolio_prev').css("opacity", "0.3").css("cursor", "default");
					
					portcat = 1;
					loc = 1;
				}
		}
	});
	
	$("#portfolio_cat_biz").click(function(){
		if (portfolioLoaded) {
			if (portcat == 1) {
				if (loc > 1) {
					$('#gallery_thumbs').css("left", "-47px")
				}
				
				$("#portfolio_cat_web").css("color", "#808080");
				$("#portfolio_cat_web_baloon").fadeTo(250, 0);
				$("#portfolio_cat_biz_baloon").css("display", "block").fadeTo(250, 1)
				$("#portfolio_cat_biz").css("color", "#FFFFFF");
				
				//swap galleries
				$('#web_gallery').hide();
				//$('#print_gallery').fadeIn(350);
				$('#biz_gallery').animate({
					opacity: 'toggle'
				}, 350);
				
				if(bizmoves == 1)
					$('#portfolio_next').css("opacity", "0.3").css("cursor", "default");
				else
					$('#portfolio_next').css("opacity", "0.75").css("cursor", "default");
				$('#portfolio_prev').css("opacity", "0.3").css("cursor", "default");
				
				portcat = 3;
				loc = 1;
			}
			
			else 
				if (portcat == 2) {
					if (loc > 1) {
						$('#gallery_thumbs').css("left", "-47px")
					}
					
					$("#portfolio_cat_print").css("color", "#808080");
					$("#portfolio_cat_print_baloon").fadeTo(250, 0);
					$("#portfolio_cat_biz_baloon").css("display", "block").fadeTo(250, 1);
					$("#portfolio_cat_biz").css("color", "#FFFFFF");
					
					//swap galleries
					$('#print_gallery').hide();
					//$('#print_gallery').fadeIn(350);
					$('#biz_gallery').animate({
						opacity: 'toggle'
					}, 350);
					
					if(bizmoves == 1)
						$('#portfolio_next').css("opacity", "0.3").css("cursor", "default");
					else
						$('#portfolio_next').css("opacity", "0.75").css("cursor", "default");
					$('#portfolio_prev').css("opacity", "0.3").css("cursor", "default");
					
					portcat = 3;
					loc = 1;
				}
		}
	});
	
	$('.portfolio_cats').hover(function(){
		if( portfolioLoaded )
			$(this).addClass('portfolio_cats_hover');
	}, function(){
		if( portfolioLoaded )
			$(this).removeClass('portfolio_cats_hover');
	});
	
	var contactme_hidden = true;
	
	$("#contactme_blog").click(function(){
		if (contactme_hidden) {
			$("#contactme_hide").slideDown( 300 );
			$("#around_blog").css("marginTop", "30px");
		}
		else {
			$("#contactme_hide").slideUp( 300 );
			$("#around_blog").css("marginTop", "15px");
		}
		
		contactme_hidden = !contactme_hidden;
	});
	
	$("#contactme_blog").hover(function(){
		$("#contactme_arrow").css("opacity",1);
		$("#contactme_blog").addClass("contactme_blog_hover");
	},
	function() {
		$("#contactme_arrow").css("opacity",0.5);
		$("#contactme_blog").removeClass("contactme_blog_hover");
	});

	//submission form
	$('#contactme_send_button').click(function(){
		$(this).attr("disabled", true); 
		$('.wpcf7-form').submit();
	})
	
	//contactme_modal close
	contactme_close = function() {
		$('#contactme_modal').fadeOut(250);
		$('#contactme_send_button').removeAttr("disabled"); 
	};
	
	//search button
	$('#search_button').click(function(){
		$('#searchform').submit();
	});
	
	//done now, get twitter -- possibly slow so do it when everythings loaded
	$.get( themePath + "/twitter_status.php", {},
	  function(data){
	    $('.twitter_text').html(data);
	  });


});

function showCaption(text, newLightbox)
{	
	$('#portfolio_desc').css("opacity",0).html(text).fadeTo(500,1);
	toLightbox = newLightbox;
}

function next()
{
	if (portcat == 1) {
		moves = webmoves;
		movepx = webmovepx;
	}
	else if (portcat == 2) {
		moves = printmoves;
		movepx = printmovepx;
	}
	else if (portcat == 3) {
		moves = bizmoves;
		movepx = bizmovepx;
	}
	
	if (loc < moves) {
		$('#gallery_thumbs').animate({
			"left": "-=" + movepx + "px"
		}, "fast", "swing");
		loc++;
		
		if(loc == moves)
			$('#portfolio_next').css("opacity", "0.3").css("cursor", "default");
		$('#portfolio_prev').css("opacity", "0.75").css("cursor", "auto");
	}
}

function prev()
{
	if (portcat == 1) {
		moves = webmoves;
		movepx = webmovepx;
	}
	else if (portcat == 2) {
		moves = printmoves;
		movepx = printmovepx;
	}
	else if (portcat == 3) {
		moves = bizmoves;
		movepx = bizmovepx;
	}
	
	if( loc > 1 )
	{
		$('#gallery_thumbs').animate({"left":"+="+movepx+"px"}, "fast", "swing");
		loc--;
		$('#portfolio_next').css("opacity","0.75").css("cursor", "auto");
		
		if(loc == 1)
			$('#portfolio_prev').css("opacity","0.3").css("cursor", "default");
	}
}

//override validation
function wpcf7ProcessJson(data) {
	var wpcf7ResponseOutput = jQuery(data.into).find('div.wpcf7-response-output');
	wpcf7ClearResponseOutput();
	if (data.invalids) {
		/*jQuery.each(data.invalids, function(i, n) {
			wpcf7NotValidTip(jQuery(data.into).find(n.into), n.message);
		});*/
		//wpcf7ResponseOutput.addClass('wpcf7-validation-errors');
		
		//do my own thing
		$('#contactme_modal_text').html('Oops! There was something wrong with your contact form. Either you did not fill all the fields or you have entered an invalid E-mail Address.<br/><b><a href="javascript:void(0);" id="contactme_again" class="contactme_close_but">Please try again.</a></b><div id="contactme_close"><a href="javascript:void(0);" class="contactme_close_but"><img src="'+themePath+'/images/contactme_close.png" alt=""/></a></div>');
		$('.contactme_close_but').click(contactme_close);
		$('#contactme_modal').fadeIn(250);
		
	}
	if (data.captcha) {
		jQuery.each(data.captcha, function(i, n) {
			jQuery(data.into).find(':input[name="' + i + '"]').clearFields();
			jQuery(data.into).find('img.wpcf7-captcha-' + i).attr('src', n);
			var match = /([0-9]+)\.(png|gif|jpeg)$/.exec(n);
			jQuery(data.into).find('input:hidden[name="_wpcf7_captcha_challenge_' + i + '"]').attr('value', match[1]);
		});
	}
    if (data.quiz) {
        jQuery.each(data.quiz, function(i, n) {
            jQuery(data.into).find(':input[name="' + i + '"]').clearFields();
            jQuery(data.into).find(':input[name="' + i + '"]').siblings('span.wpcf7-quiz-label').text(n[0]);
            jQuery(data.into).find('input:hidden[name="_wpcf7_quiz_answer_' + i + '"]').attr('value', n[1]);
        });
    }
	if (1 == data.spam) {
		wpcf7ResponseOutput.addClass('wpcf7-spam-blocked');
	}
	if (1 == data.mailSent) {
		jQuery(data.into).find('form').resetForm().clearForm();
		wpcf7ResponseOutput.addClass('wpcf7-mail-sent-ok');
		
		//do my own thing
		$('#contactme_modal_text').html('Thank you for your message! I\'ll get back to you as soon as possible.<br/><div id="contactme_close"><a href="javascript:void(0);" class="contactme_close_but"><img src="'+themePath+'/images/contactme_close.png" alt="Close Me"/></a></div>');
		$('.contactme_close_but').click(contactme_close);
		$('#contactme_modal').fadeIn(250);

        if (data.onSentOk) {
			jQuery.each(data.onSentOk, function(i, n){
				eval(n)
			});
		}
	} else {
		wpcf7ResponseOutput.addClass('wpcf7-mail-sent-ng');
	}
	
	//wpcf7ResponseOutput.append(data.message).fadeIn('fast');
}