var counter = 0;
var counter_last = 0; 
	
$(document).ready(function(){
	
	if($('#portfolio_demo').length != 0)
		 portfolio_widget_gallery()
	
	if($('#portfolio_block_content').length != 0)
		portfolio_gallery();
		
	$('.gallery_element').fadeTo('fast', 0.5, function() {
		$('.gallery_element').each(function(index) {
   			if(index == 0)
				 this.style.opacity = "1";
			else
				counter_last = index; 
  		});
    });
	
	$('.min').bind('click', function(event) {
		if ($(this).closest('.blog_entry').children('.blog_content').is(":hidden")) {
    	  $(this).closest('.blog_entry').children('.blog_content').show("slow");
		  $(this).attr("src","/assets/min.png");
    	} else {
    	  $(this).closest('.blog_entry').children('.blog_content').slideUp();
		  $(this).attr("src","/assets/max.png");
   		}
	}); 
	
	$('.gallery_element').bind('click', function(event) {
	
		var test = '/widget/portfolio_full/' + $(this).attr("alt") + '/';
		var obj = this; 
		$.get(test, function(data) {
			$('#portfolio_block_content').hide();  
    		$('#portfolio_block_content').html(data);
			$('#portfolio_block_content').fadeIn("slow"); 
			$('.gallery_element').each(function(index) {
   				this.style.opacity = "0.5"; 
  			});
			obj.style.opacity = "1"; 
	 	});
	});
	
	$('.proj_filter').change(function() { 
		 $.get('/ajax/projects.php', { category: $('#category').val(), language: $('#language').val(), platform: $('#platform').val()}, function(data) {
			$('#posts').html(data); 
		});
	}); 
	
	$('.tutorial_filter').change(function() {
		 $.get('/ajax/tutorials.php', { category: $('#category').val(), language: $('#language').val(), platform: $('#platform').val()}, function(data) {
			$('#posts').html(data); 
		});		
	}); 
	
	 $('#submit').click(function () {  
		 var name = $('input[name=name]');  
         var email = $('input[name=email]');  
         var website = $('input[name=website]');  
         var comment = $('textarea[name=message]');
		
         if (name.val()=='' || (name.val().indexOf("0") != -1) || (name.val().indexOf("1") != -1) || (name.val().indexOf("2") != -1) || (name.val().indexOf("3") != -1)
	   || (name.val().indexOf("4") != -1) || (name.val().indexOf("5") != -1)  || (name.val().indexOf("6") != -1) || (name.val().indexOf("7") != -1) || (name.val().indexOf("8") != -1)
		|| (name.val().indexOf("9") != -1) ) {  
            name.addClass('hightlight');  
             return false;  
        } else name.removeClass('hightlight');  
           
         if (email.val()=='' || (email.val().indexOf("@") == -1) || (email.val().indexOf(".") == -1) || (email.val().indexOf("@") > email.val().indexOf(".") )) {  
             email.addClass('hightlight');  
            return false;  
        } else email.removeClass('hightlight');  
         
         if (comment.val()=='') {  
            comment.addClass('hightlight');  
            return false;  
        } else comment.removeClass('hightlight'); 
		
	
       //organize the data properly  
         var data = 'name=' + name.val() + '&email=' + email.val() + '&website='  
         + website.val() + '&message='  + encodeURIComponent(comment.val());
           
         //disabled all the text fields  
         $('.field').attr('disabled','true');  
           
         //show the loading sign  
         $('.loading').show();  
           
        //start the ajax  
         $.ajax({  
            //this is the php file that processes the data and send mail  
             url: "/ajax/contact.php",   
               
            //GET method is used  
            type: "GET",  
  
            //pass the data           
            data: data,       
              
            //Do not cache the page  
             cache: false,  
              
            //success  
            success: function (html) {                
                //if process.php returned 1/true (send mail success)  
                if (html==1) {                    
                     //hide the form  
                    $('#form').fadeOut('slow');                   
                     
                   //show the success message  
                     $('.done').fadeIn('slow');  
                      
                //if process.php returned 0/false (send mail failed)  
			  } else alert('Sorry, unexpected error. Please try again later.');                 
            }         
         });  
          
         //cancel the submit button default behaviours  
         return false;  
     });     
});

function portfolio_widget_gallery()
{
	Timer = setInterval('portfolio_next()', 8000);
	$('.portfolio_widget').hover(   
        function () {   
            clearInterval(Timer);      
        },     
        function () {   
			// when the mouse leaves the gallery , restart 
            Timer = setInterval('portfolio_next()', 8000);            
        }); 
}

function portfolio_gallery()
{
	Timer = setInterval('portfolio_full_next()', 11000);
	$('#portfolio_block_content').hover(   
        function () {   
            clearInterval(Timer);      
        },     
        function () {   
			// when the mouse leaves the gallery , restart 
            Timer = setInterval('portfolio_full_next()', 11000);            
        }); 
}

function portfolio_images()
{
	$('.gallery_element').fadeTo('fast', 0.5, function() {
		$('.gallery_element').each(function(index) {
   			if(index == counter)
				 this.style.opacity = "1";
  		});
    });
}

function portfolio_next()
{
	$.get('/widget/portfolio_widget/next/', function(data) {
			$('#portfolio_demo').hide();  
    		$('#portfolio_demo').html(data);
			$('#portfolio_demo').fadeIn("slow");
			
	 });
}

function portfolio_full_next()
{
	$.get('/widget/portfolio_full/next/', function(data) {
			$('#portfolio_block_content').hide();  
    		$('#portfolio_block_content').html(data);
			$('#portfolio_block_content').fadeIn("slow"); 
			if(counter == counter_last)
				counter = 0; 
			else
				counter++; 
			portfolio_images();
	 });
}

function portfolio_prev()
{
	$.get('/widget/portfolio_widget/prev/', function(data) {
			$('#portfolio_demo').hide();  
    		$('#portfolio_demo').html(data);
			$('#portfolio_demo').fadeIn("slow"); 
  			
	});	
}

function portfolio_full_prev()
{
	$.get('/widget/portfolio_full/prev/', function(data) {
			$('#portfolio_block_content').hide();  
    		$('#portfolio_block_content').html(data);
			$('#portfolio_block_content').fadeIn("slow"); 
			if(counter == 0)
				counter = counter_last;
			else
				counter--;
			portfolio_images();
	 });
}
