$('#gallery img').ready(function() {  
	$('#image-load').delay(1000).css('display','none');
	$('#slider').css('display','block');
});

$(document).ready(function() {
	$("a#1").click(function() { show(0); return false; });
	$("a#2").click(function() { show(1); return false; });
	$("a#3").click(function() { show(2); return false; });
	$("a#4").click(function() { show(3); return false; });
	$("a#5").click(function() { show(4); return false; });
	$("a#6").click(function() { show(5); return false; });
	var images = $("#gallery img");
	var thumbs = $("#thumbs img");
	var thumblinks = $("#thumbs a");
	var index = thumbs.length-1;
	var thumbsHeight = 100;
	thumblinks.slice(thumblinks.length-3,thumblinks.length).clone().attr('id', function() { var id = $(this).id; return 'b' + this.id}).prependTo("#thumbs");
	$("a#b4").click(function() { show(3); return false; });
	$("a#b5").click(function() { show(4); return false; });
	$("a#b6").click(function() { show(5); return false; });
	for (i=0; i<thumbs.length; i++) {
		$(thumbs[i]).addClass("thumb-"+i);
		$(images[i]).addClass("image-"+i);
	}
	show(index);
	setInterval(sift, 8000);
	function sift() {
		if ( index < thumbs.length && index > 0) {index -= 1 ; }
		else { index = thumbs.length-1 }
		show ( index );
	}
	function show(num) {
		images.fadeOut(400);
		$(".image-"+num).fadeIn(400);
		var scrollPos = ( (num-1) * thumbsHeight ) + 100;
		$("#thumbs").stop().animate({scrollTop: scrollPos}, 400);
	}
	
	$('#twitter ul').ezslide();
	
	$('#imagesloc').hover(function() {
		$('#textcoffe').animate({ opacity: 0, top:'1px' }, 500);
		$('#textloc').animate({ opacity: 1, top:'30px' }, 500);
	}, function(){   
		$('#textloc').animate({ opacity: 0, top:'50px' }, 500);
		$('#textcoffe').animate({ opacity: 1, top:'30px' }, 500);
	}   
	);
	 
	/* email */
	$(".email_this").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false,
		'onClosed'		: function() {
		    $("#mform").hide();
			$('#theform').attr('style','');
		}
	});
	
	
	$('#emailfrom').keyup(function() {
		if($('#emailfrom').hasClass('error')) {
			if(isValidEmailAddress($('#emailfrom').val()))
				$('#emailfrom').removeClass('error');
		}
	});
	
	$('#subject').keyup(function() {
		if($('#subject').hasClass('error')) {
			if($('#subject').val() != '')
				$('#subject').removeClass('error');
		}
	});
	
	$('#message').keyup(function() {
		if($('#message').hasClass('error')) {
			if($('#message').val() != '')
				$('#message').removeClass('error');
		}
	});
	
	$("#share").click(function() {
		if($('#emailfrom').val() != '') {
			if(isValidEmailAddress($('#emailfrom').val())) {
				$('#emailfrom').removeClass('error');
			}
			else
				$('#emailfrom').addClass('error');
		}
		else
			$('#emailfrom').addClass('error');
		
		if($('#subject').val()!='') {
			$('#subject').removeClass('error');
		}
		else
			$('#subject').addClass('error');
  	
		if($('#message').val()!='') {
			$('#message').removeClass('error');
		}
		else
			$('#message').addClass('error');   
						
		if(!$('#subject').hasClass('error') && !$('#emailfrom').hasClass('error') && !$('#message').hasClass('error')) {
			$('#share').val('Sending...');
			$('#share').attr('disabled','disabled');
			$.post('send_email.php',{subject : $('#subject').val(), from: $('#emailfrom').val(), msg: $('#message').val()}, function(data) {
				if(data=='ok')  {
					$('#mform').attr('style','');
					$('#theform').attr('style','display:none');
					$('#mform').html('<b>Your message has been send, thanks!</b>');
				}
				else {
					$('#mform').attr('style','');
					$('#theform').attr('style','display:none');
					$('#mform').html('<b>Error</b>');
				}
				
				$.fancybox.resize();
			})
		}
	});
	
	function isValidEmailAddress(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}
	
	/** end email **/
	
	$('#sendnews').click(function() {
		var e=$('#emailnews').val();
		if(e!='') {
			if(isValidEmailAddress(e)) {
				$.post('add_subs.php',{email : e}, function(data) {
					if(data=='ok') {
						$('#newsmsg').text('Thank you for subscribing with us!');
					}
				})				
			}
			else
			$('#newsmsg').text('Please input your valid email address');  
		}
        else
			$('#newsmsg').text('Please input your email address'); 
	});
	
	var $pg = $('#portfolio .portfolio-grid');
	var speed=100;
	var speed5 = speed * 5;
	$pg.masonry({
		singleMode: true,
		// only apply masonry layout to visible elements
		itemSelector: '.col2:not(.invis)',
		animate: true,
		animationOptions: {
		duration: speed5,
		queue: false
		}
	});

	$('#portfolio .filter a').click(function() {
		var colorClass = '.' + $(this).attr('class');

		$('#portfolio .filter a.selected').removeClass('selected');
		$(this).addClass('selected');
		colorClass=colorClass.replace(/selected/i,'');

		if($(this).hasClass('all')) {
		// show all hidden boxes
		$pg.children('.invis')
		.toggleClass('invis').fadeIn(speed5);
		} else {  
		// hide visible boxes 
		$pg.children().not(colorClass).not('.invis')
		.toggleClass('invis').fadeOut(speed5);
		// show hidden boxes
		$pg.children(colorClass+'.invis')
		.toggleClass('invis').fadeIn(speed5);
		}
		$pg.masonry();	
		return false;
	});
	
	$('#portfolio .portfolio-grid li').hover(function() {
		$(this).children('.back').animate({ opacity: 0.25 }, speed);
		$(this).children('.detail').animate({ opacity: 1 }, speed);
	}, function() {
		$(this).children('.back').animate({ opacity: 1 }, speed);
		$(this).children('.detail').animate({ opacity: 0 }, speed);
	});
	
	$('#contactform').validate();
}); 

$.fn.ezslide = function () {
    var $this = this,
        cur = 0,
        fadeIt = function( which ) {
            var li = $this.find('li');
            
            cur = which = (which >= li.length) ? 0 : which;
                
            li.fadeOut(100);
            li.eq( which ).delay(200).fadeIn(500, function(){
                setTimeout(function() {
                    cur++;
                    fadeIt( cur );
                }, 5000);
            });
            
        };
    
    fadeIt( cur );
};
