(function($) {
	var portfolio_mouse_wheel = parseInt($("meta[name=portfolio_mouse_wheel]").attr('content'));

	$.fn.extend({

		CloudPortfolio: function() {
			el = this;
			el.children('div').find('a').hover(

			function() {
				el = $(this);
				if (el.hasClass('done')) {
					el.find(".portfolio-overlay").stop().fadeTo(300, 0.8);
				} else {
					el.find("img")
					height = el.find("img").height();
					cont = el.find(".portfolio-content");
					content_height = cont.height();
					content_margin_top = ((height - content_height) / 2);

					cont.css({
						marginTop: content_margin_top
					});
					el.find(".portfolio-overlay").css({
						visibility: 'visible',
						height: height
					}).stop().fadeTo(300, 0.8);
					el.addClass('done');

				}
			}, function() {
				el.find(".portfolio-overlay").stop().fadeTo(300, 0);
			});

		},
		CloudCarousel: function() {
			carousel = this;
			items = carousel.children('div');
			var columns = parseInt(carousel.attr('data-columns'));
			var total_width = 0;
			var length = items.length;

			items.each(function(i) {

				el = $(this);
				total_width += el.outerWidth(true);

				if (i + 1 > columns) {
					el.addClass('no-shadow');
				} else {
					//el.addClass('visibles');	
				}

				el.attr('data-car-id', i + 1);

			});



			carousel.attr('data-max', columns).attr('data-min', 0).wrap('<div class="full-width" style="padding:0 40px; margin:0 -40px; width:860px; overflow:hidden; position:relative;"></div>').css({
				width: total_width
			});
			$('#wide-content').trigger('animated_content');
			in_animate = false;



			$('.carousel-next[data-rel="' + carousel.attr('id') + '"]').click(function() {

				if (in_animate == true) return false;

				carousel = $('#' + $(this).attr('data-rel'));

				carousel_next(carousel, columns, length);

				return false;


			});



			//////////////////////////////////////////////////////////////////
			$('.carousel-previous[data-rel="' + carousel.attr('id') + '"]').click(function() {

				if (in_animate) return false;

				carousel = $('#' + $(this).attr('data-rel'));

				carousel_prev(carousel, columns, length);

				return false;


			});


			//////////////////////////////////////////////////////////////////
			if (portfolio_mouse_wheel == 1) {

				carousel.bind('mousewheel', function(event, delta) {
					if (in_animate) return false;

					if (delta > 0) return carousel_prev($(this), columns, length);
					else return carousel_next($(this), columns, length);

				});

			} // mouse_wheel
		},

		CloudTabs: function() {
			var tab_inprogress;
			var el = this;
			var titles = el.children('.tab_titles').children('li');
			var items = el.children('.tab_contents').children('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];

				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');

				$('#wide-content').trigger('animated_content');
				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("data-group");

				el.addClass('not');

				if (group && el.hasClass('toggle-closed')) {
					group_el = $('[data-group="' + 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;
						$('#wide-content').trigger('animated_content');
					} else {
						el.find('.toggle-content').slideUp(function() {
							toggle_inprogress = false;
							$('#wide-content').trigger('animated_content');
						})
					};
					el.removeClass('toggle-opened').removeClass('not').addClass('toggle-closed');
				} else {
					if (isie) {
						el.find('.toggle-content').removeClass('not').show();
						toggle_inprogress = false;
						$('#wide-content').trigger('animated_content');
					} else {
						el.find('.toggle-content').slideDown(function() {
							toggle_inprogress = false;
							$('#wide-content').trigger('animated_content');
						})
					};
					el.removeClass('toggle-closed').removeClass('not').addClass('toggle-opened');
				}

				return false;

			});


		},

		CloudLoadImages: function(options) {

			cli_defaults = {
				after_loading: function() {}
			}

			options = $.extend({}, cli_defaults, options);

			$.each(this, function() {
				$(this).parent('a').hide().after('<span class="load-wrap"></span>');


			});

			$(this).one("load", function() {
				$(this).parent().parent().find('.load-wrap').remove();
				$(this).parent('a').hide().fadeIn();
			}).each(function() {
				if (this.complete) $(this).trigger("load");
				options.after_loading.call();
			});


		},


		CloudSendMessage: function() {
			var el__ = this;
			var contact_inprogress;
			var submitElement = el__.find('.contactsubmit');
			submitElement.prop('disabled', '');


			el__.bind("submit", function(e) {
				e.preventDefault();
				$('<div></div>').addClass('modal-loading').css({
					opacity: .8
				}).appendTo(el__);

				//alert('sending');
				if (contact_inprogress == true) return false;
				contact_inprogress = true;

				if (!ajaxurl) {
					alert('An Error Has Occurred. Please Try Again.');
				}


				submitElement.attr('data-temp-val', submitElement.val()).val('Sending...').prop('disabled', 'true');

				var contactForm_vars = {
					action: 'cloudfw_contact'
				};


				$.ajax({
					type: "POST",
					url: ajaxurl,
					data: $.param(contactForm_vars, true) + "&" + el__.serialize(),
					dataType: 'json',
					success: function(data) {
						try {

							el__.find('.modal-loading').remove();
							el__.find('.errorMessages').slideUp(function() {
								$(this).remove()
							});
							el__.find('.error').removeClass('error');
/*alert(data);
						  	return false;*/

							// Error
							if (data.error) {

								$('<ul></ul>').addClass('errorMessages').css({
									display: 'none'
								}).prependTo(el__);

								// Loop Error Messages
								if ($.isArray(data.message)) {
									$(data.message).each(function(i) {
										$('<li></li>').html(data.message[i]).prependTo($('.errorMessages', el__));
									});
								} else {
									$('<li></li>').html(data.message).prependTo($('.errorMessages', el__));
								}

								el__.find('.errorMessages').slideDown();


								// Add Error Classes For Elemets
								if ($.isArray(data.eClasses)) {
									$(data.eClasses).each(function(i) {
										jQuery('#' + data.eClasses[i], el__).parent().addClass('error');
									});
								}
							}
							// Success
							else {

								el__.html($('<div />').html(data.message).text());

							}

							//alert(data.message);
							contact_inprogress = false;
							submitElement.val(submitElement.attr('data-temp-val')).prop('disabled', '');

						} catch (e) {
							alert('An JS Error Has Occurred.');
						} // try & catch
					},
					error: function(jqXHR, textStatus, errorThrown) {
						alert('ERROR:' + errorThrown);
					}


				});

				return false;

			});

		},


		runPrettyPhoto: function() {

			this.prettyPhoto({
				theme: 'light_square'
			}).after('<span class="plus-ribbon"></span>');


		}



	});




})(jQuery);