var mbe = new Object();
mbe = {
	templateURL: '',
	fp_project_tops: [],
	currentProject: 0,
	default_vars: {
		options_for_qtip: {
			content: {
		      text: true,
		      attr: 'title',
		      title: {
		         text: false,
		         button: false
		      }
		   },
		   position: {
		      my: 'middle left',
		      at: 'middle right',
		      target: false,
		      container: false,
		      viewport: false,
		      adjust: {
		         x: 0, y: 0,
		         mouse: true,
		         method: 'flip',
		         resize: true
		      },
		      effect: true
		   },
		   show: {
		      target: false,
		      event: false,
		      effect: true,
		      delay: 90,
		      solo: false,
		      ready: true,
		      modal: {
		         on: false,
		         effect: true,
		         blur: true,
		         keyboard: true
		      }
		   },
		   hide: {
		      target: false,
		      event: 'blur',
		      effect: true,
		      delay: 0,
		      fixed: false,
		      inactive: false,
		      leave: 'window',
		      distance: false
		   },
		   style: {
		      classes: 'ui-tooltip-red',
		      widget: false,
		      tip: {
		         corner: true,
		         mimic: false,
		         method: true,
		         width: 9,
		         height: 9,
		         border: 0,
		         offset: 0
		      }
		   },
		   events: {
		      render: null,
		      move: null,
		      show: null,
		      hide: null,
		      toggle: null,
		      focus: null,
		      blur: null
		   }
		}
	},
	ready: function() {
//		var loader= new Loader("#mbe-web-content", {
//			userCallback: mbe.showContent,
//			showProgress:true,
//			showProgressText:true,
//			textSize:15
//		});
//		loader.Start();

		mbe.addButtonActions();
		if ($('#projects-container').size()>0) {
			$('#projects-container').scrollTo(0);
		}

		mbe.qtip();
		//mbe.startAnimation();

		mbe.ourWork.gallery.allowSlide = true;
	},
	resize: function() {
		//for resizing
		mbe.ourWork.gallery.align();
	},
	scroll: function() {
		//for scrolling event
		mbe.ourWork.gallery.align();
	},
	qtip: function() {

		$('.site-author a img').each(function() {
			$(this).qtip({
				content: {
					text: 'Loading...',
					ajax: {
						url: MyAjax.ajaxurl,
						type: 'post',
						data: {
							action: $('#qtip-ajax-author-url').val(),
							name: $(this).attr('rel'),
							img_url: $('#bloginfo_value').val()
						}
					}
				},
				position: {
					my: 'top left',
					at: 'bottom center',
					target: 'event',
					adjust: {
						x: -5,
						y: 0
					}
				},
				style: {
					tip: {
						corner: 'top left',
						mimic: 'top center',
						width: 10,
						height: 12,
						border: false,
						offset: 20
					},
					classes: 'ui-tooltip-shadow ui-tooltip-light'
				}
			});
		});
	},
	showContent: function() {
	},
	startAnimation: function() {
		//$('.website').fadeIn('slow');
		//$('.top-animation').fadeOut();
	},
	addButtonActions: function() {
		$('a.button, a.download, a.google').mouseenter(function() {
			$(this).addClass('hover');
		});
		$('a.button, a.download, a.google').mouseleave(function() {
			$(this).removeClass('hover');
			$(this).removeClass('active');
		});
		$('a.button, a.download, a.google').mousedown(function() {
			$(this).addClass('active');
		});
		$('a.button, a.download, a.google').mouseup(function() {
			$(this).removeClass('active');
		});

		$('.projects .pictures .sidebar ul.items li').click(function() {
			i=parseInt($(this).attr('id').substr(6));
			mbe.goToProject(i);
		});
	},
	goToProject: function(i) {
		mbe.currentProject = i;
		$('.projects .pictures .sidebar .scrollbar .bullet img').each(function(){
			if ($(this).is('#project-img-'+i)) {
				$(this).attr('src',mbe.templateURL+'/img/projects-bullet-selected.png');
			} else {
				$(this).attr('src',mbe.templateURL+'/img/projects-bullet.png');
			}
		});
		$('.project-info').each(function(){
			if ($(this).is('#project-'+i+'-info')) {
				$(this).slideDown('fast');
			} else {
				$(this).slideUp('fast');
			}
		});
		$('#projects-container').scrollTo(i*275,'fast',{
			easing: 'easeInOutCirc',
			onAfter: function() {
				$('.projects .pictures .subtitle').animate({
					top: mbe.fp_project_tops[i]
				});
				/*
				if (i==4) {
					$('.projects .pictures .subtitle').animate({
						top: '20px'
					});
				} else if (i==0) {
					$('.projects .pictures .subtitle').animate({
						top: '105px'
					});
				} else {
					$('.projects .pictures .subtitle').animate({
						top: '60px'
					});
				}
				*/
			}
		});
		return false;
	},
	goToProjectUp: function() {
		projects = $('#projects-container img').size();
		if (mbe.currentProject<=0) {
			mbe.goToProject(projects-1);
		} else {
			mbe.goToProject(mbe.currentProject-1);
		}
		return false;
	},
	goToProjectDown: function() {
		projects = $('#projects-container img').size();
		if (mbe.currentProject>=projects-1) {
			mbe.goToProject(0);
		} else {
			mbe.goToProject(mbe.currentProject+1);
		}
		return false;
	},
	sideGoToProject: function(i) {
		mbe.currentProject = i;
		$('.columns .column-right .item .side-scrollbar .bullet img').each(function(){
			if ($(this).is('#project-img-'+i)) {
				$(this).attr('src',mbe.templateURL+'/img/projects-bullet-selected.png');
			} else {
				$(this).attr('src',mbe.templateURL+'/img/projects-bullet.png');
			}
		});
		$('.project-info').each(function(){
			if ($(this).is('#project-'+i+'-info')) {
				$(this).slideRight('fast');
			} else {
				$(this).slideLeft('fast');
			}
		});
		$('#projects-container').scrollTo(i*200,'fast',{
			easing: 'easeInOutCirc',
			axis: 'x'
		});
		return false;
	},
	sideGoToProjectLeft: function() {
		projects = $('#projects-container img').size();
		if (mbe.currentProject<=0) {
			mbe.sideGoToProject(projects-1);
		} else {
			mbe.sideGoToProject(mbe.currentProject-1);
		}
		return false;
	},
	sideGoToProjectRight: function() {
		projects = $('#projects-container img').size();
		if (mbe.currentProject>=projects-1) {
			mbe.sideGoToProject(0);
		} else {
			mbe.sideGoToProject(mbe.currentProject+1);
		}
		return false;
	},
	showWhoWeAre: function() {
		$('#what-we-do').slideUp('fast');
		$('#who').parent().addClass('selected');
		$('#who-we-are').slideDown('fast');
		$('#what').parent().removeClass('selected');
		return false;
	},
	showWhatWeDo: function() {
		$('#what-we-do').slideDown('fast');
		$('#what').parent().addClass('selected');
		$('#who-we-are').slideUp('fast');
		$('#who').parent().removeClass('selected');
		return false;
	},
	ourWork: {
		ready: function() {
			mbe.ourWork.addHovers();
		},
		addHovers: function() {
			$('.projects.work .pictures .pad').mouseenter(function(){
				$(this).children('.float-left').children('img.gotosite').fadeIn('fast');
			});
			$('.projects.work .pictures .pad').mouseleave(function(){
				$(this).children('.float-left').children('img.gotosite').fadeOut('fast');
			});
		},
		toggleDetails: function(_this) {
			$(_this).toggleClass('active');
			$(_this).parent().children('.details').slideToggle('slow','easeInOutBack');
			return false;
		},
		gallery: {
			show: function(_id) {
				var gallery = $('#gallery-'+_id);
				if (gallery.length) {
					gallery.height($(document).height());
					gallery.find('.projects').css('top',$(document).scrollTop());
					gallery.fadeIn('fast');

					var scroll = parseInt($(document).scrollTop());
					var projects_h = parseInt($('#gallery-'+_id+' .projects').height());
					var window_h = parseInt($(window).height());

					if (window_h > projects_h) {
						var p_top = parseInt((window_h-projects_h)/2)+scroll;
					} else {
						var p_top = 0;
					}

					gallery.find('.projects').css('top',p_top+'px');
				} else {
					$.post(MyAjax.ajaxurl, {
						action: 'gallery_show',
						post_id: _id
						}, function(result) {
							$('body').append(result);
							mbe.addButtonActions();

							$('.gallery-nav .helper-arrow').hover(function(e) {
								e.preventDefault();
								mbe.ourWork.gallery.arrow.show(this);
							}, function (e) {
								e.preventDefault();
								mbe.ourWork.gallery.arrow.hide(this);
							});

							var gallery = $('#gallery-'+_id);
							gallery.height($(document).height());

							gallery.fadeIn('fast');

							var scroll = parseInt($(document).scrollTop());
							var projects_h = parseInt($('#gallery-'+_id+' .projects').height());
							var window_h = parseInt($(window).height());

							if (window_h > projects_h) {
								var p_top = parseInt((window_h-projects_h)/2)+scroll;
							} else {
								var p_top = 0;
							}

							gallery.find('.projects').css('top',p_top+'px');

							gallery.find('.projects').click(function(e) {
								e.stopImmediatePropagation();
							});
							$(document).keyup(function(e) {
							    if (e.which == 27) mbe.ourWork.gallery.hide(_id);
							});
					});
				}
			},
			hide: function(_id) {
				var gallery = $('#gallery-'+_id);
				if (gallery) {
					gallery.fadeOut('fast');
				}
			},
			align: function(e) {
				if ($('.gallery').length > 0) {
					var scroll = parseInt($(document).scrollTop());
					var projects_h = parseInt($('.gallery .projects:visible').height());
					var window_h = parseInt($(window).height());

					if (window_h > projects_h) {
						var p_top = parseInt((window_h-projects_h)/2)+scroll;
					} else {
						var p_top = 0;
					}

					//$('.gallery .projects:visible').css('top',p_top+'px');
					$('.gallery .projects:visible').animate({
							top: p_top
						}, {
							duration: 50,
							specialEasing: {
								top: 'linear'
							},
							complete: function() {
								// Animation complete.
							}
					});
				}
			},
			arrow: {
				fadingIn: false,
				fadingOut: false,
				show: 	function(_this) {
					var element = $(_this).parent().children('.nav-arrow');
					if (mbe.ourWork.gallery.arrow.fadingOut) {
						element.stop(true,true);
						mbe.ourWork.gallery.arrow.fadingOut = false;
					}
					if (!mbe.ourWork.gallery.arrow.fadingIn) {
						mbe.ourWork.gallery.arrow.fadingIn = true;
						element.fadeIn(200, function() {
							mbe.ourWork.gallery.arrow.fadingIn = false;
						});
					}
				},
				hide: function(_this) {
					var element = $(_this).parent().children('.nav-arrow');
					if (mbe.ourWork.gallery.arrow.fadingIn) {
						element.stop(true,true);
						mbe.ourWork.gallery.arrow.fadingIn = false;
					}
					if (!mbe.ourWork.gallery.arrow.fadingOut) {
						mbe.ourWork.gallery.arrow.fadingOut = true;
						element.fadeOut(200, function() {
							mbe.ourWork.gallery.arrow.fadingOut = false;
						});
					}
				}
			},
			slide: function(_id, direction) {
				if (mbe.ourWork.gallery.allowSlide == true) {
					mbe.ourWork.gallery.allowSlide = false;
					if (direction == 'left') {
						if (parseInt($('#gallery-'+_id+' .gallery-img').css('left')) <= -900) {
							$('#gallery-'+_id+' .details').slideToggle('fast', function() {
								var info_left = parseInt($('#gallery-'+_id+' .project-infos').css('left'));
								$('#gallery-'+_id+' .project-infos').css('left', info_left+900);
								$('#gallery-'+_id+' .gallery-img').animate({
										left: '+=900'
									}, 600, function() {
										// Animation complete.
										mbe.ourWork.gallery.allowSlide = true;

										$('#gallery-'+_id+' .project-info').hide();
										$('#gallery-'+_id+' .project-infos').width(900);
										$('#gallery-'+_id+' .project-infos').css('left', '0px');
										var a_id = parseInt($('#gallery-'+_id+' .gallery-img').css('left')) / (-900);
										$('#project-info-'+_id+'-'+a_id).show();

										$('#gallery-'+_id+' .details').slideToggle('fast');

										$('#gallery-nav-right-'+_id).css('visibility', 'visible');
										if (parseInt($('#gallery-'+_id+' .gallery-img').css('left')) > -900) {
											$('#gallery-nav-left-'+_id).children('.nav-arrow').fadeOut(200, function() {
												$('#gallery-nav-left-'+_id).css('visibility', 'hidden');
											});
										} else {
											$('#gallery-nav-left-'+_id).css('visibility', 'visible');
										}
								});
							});
						} else {
							mbe.ourWork.gallery.allowSlide = true;
						}
					} else if (direction == 'right') {
						if (parseInt($('#gallery-'+_id+' .gallery-img').css('left')) >= 1800-parseInt($('#gallery-'+_id+' .gallery-img').width())) {
							$('#gallery-'+_id+' .details').slideToggle('fast', function() {
								var info_left = parseInt($('#gallery-'+_id+' .project-infos').css('left'));
								$('#gallery-'+_id+' .project-infos').css('left', info_left-900);
								$('#gallery-'+_id+' .gallery-img').animate({
										left: '-=900'
									}, 600, function() {
										// Animation complete.
										mbe.ourWork.gallery.allowSlide = true;

										$('#gallery-'+_id+' .project-info').hide();
										$('#gallery-'+_id+' .project-infos').width(900);
										$('#gallery-'+_id+' .project-infos').css('left', '0px');
										var a_id = parseInt($('#gallery-'+_id+' .gallery-img').css('left')) / (-900);
										$('#project-info-'+_id+'-'+a_id).show();

										$('#gallery-'+_id+' .details').slideToggle('fast');

										$('#gallery-nav-left-'+_id).css('visibility', 'visible');
										if (parseInt($('#gallery-'+_id+' .gallery-img').css('left')) < 1800-parseInt($('#gallery-'+_id+' .gallery-img').width())) {
											$('#gallery-nav-right-'+_id).children('.nav-arrow').fadeOut(200, function() {
												$('#gallery-nav-right-'+_id).css('visibility', 'hidden');
											});
										} else {
											$('#gallery-nav-right-'+_id).css('visibility', 'visible');
										}
								});
							});
						} else {
							mbe.ourWork.gallery.allowSlide = true;
						}
					}
				}
			}
		}
	},
	contact: {
		submitForm: function() {
			$('#contact-loader').show();
			$('#contact-submit').hide();

			var form = $('#contact-form');
			var message = $('#form-messages');

			$.post(form.attr('action'), form.serialize(), function(result) {

				$('#contact-name').qtip('hide');
				$('#contact-email').qtip('hide');
				$('#contact-message').qtip('hide');
				$('#contact-captcha').qtip('hide');

				//Recaptcha.reload();

				var options = mbe.default_vars.options_for_qtip;
				//options.style.border.color = (result.type=='info'?'#CDE6AC':'#F79992');
				options.style.classes = (result.type=='info'?'ui-tooltip-green':'ui-tooltip-red');
				//console.log(options);

				if (result.type == 'error') {
					/*
					message.removeClass('info');
					message.addClass('error');
					message.html('');
					*/
					for (error in result.content) {
						options.content = result.content[error];
						$('#contact-'+error).qtip(options);

						//message.append('<li>'+result.content[error]+'</li>');
					}
					message.slideDown('fast');


					$('#contact-loader').hide();
					$('#contact-submit').show();

				} else if (result.type == 'info') {
					/*
					message.removeClass('error');
					message.addClass('info');
					message.html('');
					message.append('<li>'+result.content+'</li>');
					message.slideDown('fast');
					*/


					$('#contact-loader').hide();
					$('#contact-submit').show();

					options.content = result.content;
					options.position.adjust.x = -130;
					options.position.adjust.y = 10;

					//console.log(options);
					$('#contact-submit').parent().qtip(options);

					form.find('input.text').val('');
					form.find('textarea.text').val('');

					if (result.redirect_url) {
						window.location = result.redirect_url;
					}
				}
			});
		}
	}
}
$(document).ready(mbe.ready);
$(window).resize(mbe.resize);
$(window).scroll(mbe.scroll);

