(function($){ 
    $.fn.extend({ 
				
		CloudPortfolioItemHover: function() {
			el = this;
			el.children('.portfolio-item').children('li:not(.not)').find('a').hover(
																			  
				function(){
						el = $(this);
						height = el.find("img").height();
						el.find(".portfolio-desc").css({height: height}).stop().fadeTo(300, 0.8);
						el.find(".portfolio-icon").animate({backgroundPosition: "50% 50%"},300);
						
						
						},
				function(){
						el.find(".portfolio-desc").stop().fadeTo(300, 0);
						el.find(".portfolio-icon").css({backgroundPosition: "200% 50%"});
					});
			
			},
		
		
		CloudPortfolio: function() {

			var el = this;
			var column = el.attr("id"); 
				column = column.split('__');
				column = column[1];
				
			var def_height = el.attr("lang"); 
				def_height = def_height.split('__');
				def_height = def_height[1];
				
			
			var in_proccess;
			var items = el.children('.portfolio-item').children('li:not(.not,.hidden)');
			var hidden_items = el.children('.portfolio-item').children('li.hidden');
			
			var st_width = items.width()-1;
			var st_height = 180;
			var arrow_st_width = (st_width/2);
			
			if (!column) column = 3;
			
			
			el.find(".inside").css("width",(st_width));
			
			
			var i = 1;
			
				items.each(function() {
				
				if(i === 1) { $(this).addClass('current').addClass('p_'+i);
				
				
				
				}
				else { $(this).addClass('p_'+i);if(i%column == 0) { $(this).addClass('last').after('<div class="clear"></div>'); }; }
				
				i++;
				
				});
				
			var	j = 1;
				
				hidden_items.each(function() {
				 
				 $(this).addClass('p_'+j); 
				
				j++;
				
				});
			

			
				el.CloudPortfolioItemHover();
				
				
			
			
			if (hidden_items.length > 0) {
				
			el.children('.portfolio-item').append('<li class="not grid oneof'+column+' last" style="float:right; height:'+def_height+'px;"><a href="#" class="previous" style="width:'+arrow_st_width+'px; height:'+def_height+';"><span style="height:'+def_height+'px;">Previous</span></a> <a href="#" class="next" style="width:'+arrow_st_width+'px; height:'+def_height+'px;"><span style="height:'+def_height+'px;">Next</span></a></li>');

			el.children('.portfolio-item').children('.not').bind('mouseover',function(){
					
					var cont_height = items.first().children('.inside').find('img').height();
					if (!cont_height > 0) return false;
					this_el = jQuery(this);
					this_el.css({height: cont_height});
					this_el.find('a,span').animate({height: cont_height},200);
					
				
				});
			
			
			el.find('.next').click(function() {
											
				if (in_proccess) return false;
				in_proccess = true;

				var items = el.children('.portfolio-item').children('li:not(.not,.hidden)');
				var hidden_items = el.children('.portfolio-item').children('li.hidden');

				var firstitem = items.first();
				firstitem.animate({width:0},200,function(){
						
						firstitem.addClass('hidden').hide();				   
						el.find('.not').before(firstitem.clone());
						firstitem.remove();   
					
					
						hidden_items.first().css({width: st_width}).fadeIn(500).removeClass('hidden').show();
						
						el.find(".last").removeClass('last');
						el.children('.portfolio-item').find('.clear').remove();
						
						var i = 1;
						el.children('.portfolio-item').children('li:not(.hidden)').each(function() {
						if(i%column == 0) { $(this).addClass('last').after('<div class="clear"></div>'); };
						i++;
						});
						
						$("a[rel^='prettyPhoto']").prettyPhoto();
						el.CloudPortfolioItemHover();
						
						in_proccess = false;
						
				});
				
				
				
				

				
				return false;
				
			});

			el.find('.previous').click(function() {
												
				if (in_proccess) return false;
				in_proccess = true;

				var items = el.children('.portfolio-item').children('li:not(.not,.hidden)');
				var hidden_items = el.children('.portfolio-item').children('li.hidden');
				
				items.last().fadeOut(200,function(){
												  
												  
				items.last().addClass('hidden').hide();
																				 
				var lastitem = hidden_items.last();
				
				el.children('.portfolio-item').prepend(lastitem.clone().removeClass('hidden').css({width: 0}).animate({width: st_width},200));
				el.find(".last").removeClass('last');
				lastitem.remove();
				
				
				
						el.children('.portfolio-item').find('.clear').remove();
						
						var i = 1;
						el.children('.portfolio-item').children('li:not(.hidden)').each(function() {
						if(i%column == 0) { $(this).addClass('last').after('<div class="clear"></div>'); };
						i++;
						});
																 
																						 
						$("a[rel^='prettyPhoto']").prettyPhoto();
						el.CloudPortfolioItemHover();
						
						in_proccess = false;						
				});

				
				
				
				return false;
				
			});
			

			
			/*var first_item = items.find("img").first();
			
				var image = new Image();
				image.src = first_item.attr('src');
			
				if (image.complete) {       
				  
				hight_final = first_item.height();
				el.find('.not, .next, .previous, .next span, .previous span').animate({"height": hight_final});
				//alert(hight_final);
				  
				  }   
				else {
				
				  
				}*/
											 
											 
				
			
			//$('.next').css({"height": hight_final, "border": "solid 1px"});
			
			}
			
			
		},
		
		CloudTabs: function() {
			var tab_inprogress;
			var el = this;
			var titles = el.children('.tab_titles').find('li');
			var items = el.children('.tab_contents').find('li');
			
				var i = 1;
				
			// Add Numeric Class Tab Contents
			items.each(function(){ $(this).addClass('tab_'+i); i++;});
				
			items.first().show().removeClass('hidden').addClass('currentTab');
			//alert('Showed');
			
			titles.children('a').click(function(){
					
					thisel = $(this);
					isie = $.browser.msie;
					
					if (tab_inprogress == true || thisel.parent('li').hasClass('current')) return false;
					tab_inprogress = true;
					 
					var id = thisel.attr("href");
					var id = id.split('#');
					var id = id[1];
					
					if (!isie){					
						el.children('.tab_contents').find('.currentTab').slideUp(function(){$(this).removeClass('currentTab').addClass('hidden');});
						el.children('.tab_contents').find('.'+id).css('display','none').removeClass('hidden').slideDown(function(){tab_inprogress = false;}).addClass('currentTab');}
					else
					{
						el.children('.tab_contents').find('.currentTab').hide().removeClass('currentTab').addClass('hidden').fadeOut().fadeIn();
						el.children('.tab_contents').find('.'+id).show().removeClass('hidden').addClass('currentTab');
						tab_inprogress = false;
						
					}
					
					el.find('.current').removeClass('current');
					thisel.parent('li').addClass('current');
					
					
					return false;
				
				});
			
			
			
			},
			
		CloudToggle: function() {
			var toggle_inprogress;
			var el = this;

				el.find('.toggle-link').click(function(){
					
					if (toggle_inprogress == true) return false;
					toggle_inprogress = true;

					thisel = $(this);
					isie = $.browser.msie;
					group = el.attr("rel");
					
					el.addClass('not');
					
					if (group && el.hasClass('toggle-closed')) {
						group_el = $('[rel="'+group+'"]').not('.not');
						if (isie){
							group_el.find('.toggle-content').hide(); 
							group_el.removeClass('toggle-opened').addClass('toggle-closed');
						}else{
							group_el.find('.toggle-content').slideUp(function(){
									
									group_el.removeClass('toggle-opened').addClass('toggle-closed');
									
							});
						}
					}
					
					if (el.hasClass('toggle-opened')) {
						if (isie) {el.find('.toggle-content').removeClass('not').hide();  toggle_inprogress = false;} else { el.find('.toggle-content').slideUp(function(){ toggle_inprogress = false; }) };
						el.removeClass('toggle-opened').removeClass('not').addClass('toggle-closed');
						}else
						{
						if (isie) {el.find('.toggle-content').removeClass('not').show(); toggle_inprogress = false;} else { el.find('.toggle-content').slideDown(function(){ toggle_inprogress = false; }) };
						el.removeClass('toggle-closed').removeClass('not').addClass('toggle-opened');
					}
					
					return false;
				
				});
			
			
			},
		
		CloudSendMessage: function() {
			el__ = this;
			var contact_inprogress;
			
			
			el__.live("submit",function(){
					
					if (contact_inprogress == true) return false;
					contact_inprogress = true;
					
					
					var tmpurl = $("meta[name=tmpurl]").attr('content');
					if (!tmpurl) {
						
						alert('An Error Has Occurred. Please Try Again.');
						return false;				
						}	
						
						
						var name = el__.find('#name').val();
						var email = el__.find('#email').val();
						var message = el__.find('#message').val();
						var nonce = el__.find('#wp_contact_nonce').val();
						
						el__.find('#contactsubmit').val('Sending...').attr('disabled','true');
						
						
						$.ajax({
						  type: "POST",
						  url: tmpurl + '/contact_submit.php',
						  data: ({name : name, email : email, message : message, nonce : nonce}),
						  success: function(data) {
							el__.html(data);
							contact_inprogress = false;
						  }
						});
						
						return false;
									  
									  });
			
			}
		
		
});
	
})(jQuery);
