$(function() {
	var testimonial_page = 1;
	$.address.change(function(event) {
		current = event.value.split('/');
		$('html, body').animate({
			scrollTop: 0
		}, 'slow');
		$('#contact_form, #commentform').validationEngine('hide');
		
		switch (current[1]) {
		case 'mostrecent':
			if (typeof(current[2]) == "undefined") {
				current[2] = $('#mostrecent_thumbs .items li:first-child a').attr('href');
			}
			
			$('#' + current[1] + '_nav').clearItems(current[1]);
			$('#' + current[1] + '_nav').updateNav();
			$('#mostrecent_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				if (typeof(current[2]) != "undefined") {
					$('a[href*="' + current[2] + '"]').updatePortfolioItem();
				}
			});
			break;
			
		case 'office':
			if (typeof(current[2]) == "undefined") {
				$.address.value($('#contact_thumbs .items li:first-child a').attr('href'));
			}
			$('#' + current[1] + '_nav').updateNav();
			$('#' + current[1] + '_nav').clearItems(current[1]);
			$('#contact_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				if (typeof(current[2]) != "undefined") {
					$('#contact_thumbs .items li a[href*="' + current[2] + '"]').updatePageItem();
				} else {
					$.address.value($('#contact_thumbs .items li:first-child a').attr('href'));
				}
			});
			break;
			
		case 'uk':
			$('#office_nav').updateNav();
			$('#office_nav_nav').clearItems(current[1]);
			$('#contact_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				$('#contact_thumbs .items li a[href*="lightmaker-uk"]').updatePageItem();
			});
			break;
			
		case 'usa':
			$('#office_nav').updateNav();
			$('#office_nav_nav').clearItems(current[1]);
			$('#contact_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				$('#contact_thumbs .items li a[href*="lightmaker-usa"]').updatePageItem();
			});
			break;
			
		case 'nl':
			$('#office_nav').updateNav();
			$('#office_nav_nav').clearItems(current[1]);
			$('#contact_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				$('#contact_thumbs .items li a[href*="lightmaker-netherlands"]').updatePageItem();
			});
			break;
			
		case 'can':
			$('#office_nav').updateNav();
			$('#office_nav_nav').clearItems(current[1]);
			$('#contact_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				$('#contact_thumbs .items li a[href*="lightmaker-canada"]').updatePageItem();
			});
			break;
			
		case 'contact-us':
			if (typeof(current[2]) == "undefined") {
				$.address.value($('#contact_thumbs .items li:first-child a').attr('href'));
			}
			$('#' + current[1] + '_nav').updateNav();
			$('#' + current[1] + '_nav').clearItems(current[1]);
			$('#contact_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				if (typeof(current[2]) != "undefined") {
					$('#contact_thumbs .items li a[href*="' + current[2] + '"]').updatePageItem();
				} else {
					$.address.value($('#contact_thumbs .items li:first-child a').attr('href'));
				}
			});
			break;
		
		case 'contacts':
            if (typeof(current[2]) == "undefined") {
                    $.address.value($('#contact_thumbs .items li:first-child a').attr('href'));
            }
            $('#' + current[1] + '_nav').updateNav();
            $('#' + current[1] + '_nav').clearItems(current[1]);
            $('#contact_thumbs').animate({
                    left: 20,
                    opacity: 1
            }, 500, 'easeOutCubic', function() {
                    if (typeof(current[2]) != "undefined") {
                            $('#contact_thumbs .items li a[href*="' + current[2] + '"]').updatePageItem();
                    } else {
                            $.address.value($('#contact_thumbs .items li:first-child a').attr('href'));
                    }
            });
            break;

	
		case 'portfolio':
			category = false;
			switch (current[2]) {
				case 'sports':
					category = true;
				case 'entertainment':
					category = true;
				case 'foodanddrink':
					category = true;
				case 'gaming':
					category = true;
				case 'government':
					category = true;
				case 'luxury':
					category = true;
				case 'travel':
					category = true;
				case 'finance':
					category = true;
				case 'mobile-tablet':
					category = true;
			}
			$('#see_all_portfolio').show();
			if ($('a[href*="/mostrecent"]').hasClass('current') || $('#search_container').css('left') == '210px') {
				$('a[href*="/mostrecent"]').removeClass('current');
				$('#all_portfolio').addClass('current');
				if (!category) {
					$.ajax({
						url: "/portfolio?ajax=1",
						success: function(data) {
						
							$('#pane .items').html(data);
							
							iScrollInit({
								pane: $('#pane')
							});
							//$('a[href*="' + current[2] + '"]').parent().parent().find('.current').removeClass();
							$('.scrollable .current').removeClass();
							$('a[href*="' + current[2] + '"]').addClass('current');
						}
					});
				}
			}
			if (category) {
				//$('a[href*="' + current[2] + '"]').parent().parent().parent().parent().find('.current').removeClass();
				$('.scrollable .current').removeClass('current');
				$('#main_nav .current').removeClass('current');
				$('a[href*="' + current[2] + '"]').addClass('current');
			} else {
				if (typeof(current[2]) == "undefined") {
					//$('#' + current[1] + '_nav').parent().find('.current').removeClass('current');
					$('#main_nav .current').removeClass('current');
					$('#all_portfolio').addClass('current');
				}
			}
			if (category || typeof(current[2]) == "undefined" || current[2] == "") {
				if (typeof(current[2]) != "undefined" && current[2] != "") {
					page = current[2];
				} else {
					page = 'portfolio';
				}
				$.ajax({
					url: "/" + page + '?ajax=1',
					success: function(data) {
						
							$('#pane .items').html(data);
							
							iScrollInit({
								pane: $('#pane')
							});
						if (typeof(current[2]) == "undefined" && !category) {
							if (!$('#see_all_portfolio').hasClass('collapse')) {
								$('#see_all_portfolio').click();
								$('#see_all_portfolio').hide();
								displayImages({
									pane: $('pane')
								});
								$('#recent_social').load("/social-sidebar/", function() {
									$('#pane2').css('height', $('#thumbs').innerHeight());
									iScrollInit({
										pane: $('#pane2')
									});
									/* LOAD RECENT SOCIAL SECTION */
									$('#recent_social').animate({
										right: 0,
										opacity: 1
									}, 500, 'easeOutCubic');
								});
							}
						} else {
							$.address.value($('#thumbs .items li:first-child a').attr('rel').split(':')[1]);
						}
					}
				});
			}
			//$('#' + current[1] + '_nav').updateNav();
			$('#' + current[1] + '_nav').clearItems(current[1]);
			$('#portfolio_menu').slideDown();
			$('#social_menu').slideUp();
			$('#job_menu').slideUp();
			/* THIS IS WHERE THE ANIMATION SEQUENCE STARTS	*/
			/* THIS NEEDS TO BE DONE IN THE CORRECT ORDER	*/
			/* WITH THE NEXT ANIMATION NOT STARTING UNTIL	*/
			/* THE PREVIOUS ONE ENDS 						*/
			/* SHOW THUMBNAILS */
			$('#thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				/* CHECK IF THERE IS AN ARTICLE SELECTED 		*/
				/* IF NO ARTICLE CHANGE URL TO MOST RECENT		*/
				/* IF ARTICLE IS SELECTED START AJAX FUNCTION 	*/
				if (typeof(current[2]) != "undefined" && category != true) {
					$('a[href="/portfolio/' + current[2] + '"]').updatePortfolioItem();
				}
			});
			break;
			
		case 'news':
			if (typeof(current[2]) == "undefined") {
				$.address.value($('#news_thumbs .items li:first-child a').attr('href'));
			}
			$('#' + current[1] + '_nav').updateNav();
			$('#' + current[1] + '_nav').clearItems(current[1]);
			$('#news_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				if (typeof(current[2]) != "undefined") {
					$('a[href*="' + current[2] + '"]').updatePortfolioItem();
				}
			});
			break;
			
		case 'social':
			if (typeof(current[2]) != "undefined") {
				$.address.value('/social');
			} else {
				$('#social_menu').slideDown();
				$('#portfolio_menu').slideUp();
				$('#job_menu').slideUp();
				$('#social_nav').updateNav();
				if ($('.dynamic_column_container').position().left == -1000) {
					$('#' + current[1] + '_nav').clearItems(current[1]);
				}
				$.address.value($(this).attr('href'));
				$('#recent_social').animate({
					right: 1500,
					opacity: 0
				}, 500, 'easeInCubic');
				$('.thumbs_nav').animate({
					left: -800,
					opacity: 0
				}, 500, 'easeInCubic', function() {
					var filter = "";
					$('#social_nav .subnav em').each(function() {
						if ($(this).hasClass('icon_selected')) {
							filter = filter + $(this).parent().parent().find('a').attr('href') + "+";
						}
					});
					$('#social').load("/social/?ajax=1&filter=" + filter, function() {
					
						/* resize elements on load start */
						if ($(window).width() < 1338) {
							smallSize();
						} else {
							midSize();
						}
						$('#social_container').masonry({
							// options
							itemSelector: '.cool_containers',
							columnWidth: 249,
							gutterWidth: 10,
							isFitWidth: true
						});
						$('#social_container').masonry('reload');
						if ($('#social_container').height() + 80 > 900) {
							$('#still_container').height($('#social_container').height() + 80);
						} else {
							$('#still_container').height(900);
						}
						var initContentHeight = $('#social').innerHeight();
						$('#recent_social').animate({
							right: 1500,
							opacity: 0
						}, 500, 'easeInCubic');
						$('#social').animate({
							left: 210,
							opacity: 1
						}, 500, 'easeOutCubic', function() {})
					});
				});
			}
			break;
			
		case 'testimonials':
			$('#' + current[1] + '_nav').updateNav();
			$('#' + current[1] + '_nav').clearItems(current[1]);
			$('#recent_social').animate({
				right: 1500,
				opacity: 0
			}, 500, 'easeInCubic', function() {
				$('.thumbs_nav').animate({
					left: -800,
					opacity: 0
				}, 500, 'easeInCubic', function() {
					$('#testimonials').animate({
						left: 210,
						opacity: 1
					}, 500, 'easeOutCubic', function() {
						$('#container_box').load('/category/testimonials/?ajax=1&page=' + testimonial_page, function() {
							if ($(window).width() < 1338) {
								smallSize();
							} else {
								midSize();
							}
							$('#container_box').masonry({
								// options
								itemSelector: '.cool_containers',
								columnWidth: 249,
								gutterWidth: 10,
								isFitWidth: true
							});
							$('#container_box').masonry('reload');
							$('#still_container').height($('#container_box').height() + 133);
							testimonial_page++;
							$('#testimonial_loadmore').unbind();
							$('#testimonial_loadmore').click(function() {
								$.ajax({
									url: '/category/testimonials/?ajax=1&page=' + testimonial_page,
									context: document.body,
									success: function(data) {
										$boxes = $(data);
										$('#container_box').append($boxes);
										$('#container_box').masonry('reload');
										$('#still_container').height($('#container_box').height() + 133);
										testimonial_page++;
									}
								});
							});
							$('#recent_social').animate({
								right: 1500,
								opacity: 0
							}, 500, 'easeInCubic');
						})
					})
				})
			});
			break;
			
		case 'awards':
			$('#' + current[1] + '_nav').updateNav();
			$('#' + current[1] + '_nav').clearItems(current[1]);
			if (typeof(current[2]) == "undefined") {
				$('.dynamic_column_container').animate({
					left: -1000,
					opacity: 0
				}, 500, 'easeInCubic', function() {
					$("#social .cool_containers").hide();
					$("#testimonials .cool_containers").hide();
				});
				$('.main_content').animate({
					left: -1000,
					opacity: 0
				}, 500, 'easeInCubic', function() {
					$('.thumbs_nav').animate({
						left: -800,
						opacity: 0
					}, 500, 'easeOutCubic', function() {
						if ($(window).width() < 1338) {
							smallSize();
						} else {
							midSize();
						}
						$('#search_container').load("/category/awards?ajax=1", function(response, status, xhr) {
							$('#search_container a[rel^="address"]').click(function(e) {
								e.preventDefault();
							});
							var initContentHeight = $('#search_container').height();
							$('#still_container').css('height', initContentHeight);
							$('#pane').css('height', initContentHeight - 122);
							$('#pane2').css('height', initContentHeight - 122);
							$('#recent_social_follow').css('height', initContentHeight - 122);
							$('#search_container').animate({
								left: 210,
								opacity: 1
							}, 500, 'easeOutCubic');
						});
						$('#recent_social').animate({
							right: 0,
							opacity: 1
						}, 500, 'easeOutCubic');
					})
				});
			} else {
				$('#awards_thumbs').animate({
					left: 20,
					opacity: 1
				}, 500, 'easeOutCubic', function() {
					if (typeof(current[2]) != "undefined") {
						$('a[href*="' + current[2] + '"]').updatePortfolioItem();
					}
				});
			}
			break;
				
			case 'incubator':
				$('#' + current[1] + '_nav').updateNav();
				$('#' + current[1] + '_nav').clearItems(current[1]);
				if (typeof(current[2]) == "undefined") {
					$('.dynamic_column_container').animate({
						left: -1000,
						opacity: 0
					}, 500, 'easeInCubic', function() {
						$("#social .cool_containers").hide();
						$("#testimonials .cool_containers").hide();
					});
					$('.main_content').animate({
						left: -1000,
						opacity: 0
					}, 500, 'easeInCubic', function() {
						$('.thumbs_nav').animate({
							left: -800,
							opacity: 0
						}, 500, 'easeOutCubic', function() {
							if ($(window).width() < 1338) {
								smallSize();
							} else {
								midSize();
							}
							$('#search_container').load("/category/incubator?ajax=1", function(response, status, xhr) {
								$('#search_container a[rel^="address"]').click(function(e) {
									e.preventDefault();
								});
								var initContentHeight = $('#search_container').height();
								$('#still_container').css('height', initContentHeight);
								$('#pane').css('height', initContentHeight - 122);
								$('#pane2').css('height', initContentHeight - 122);
								$('#recent_social_follow').css('height', initContentHeight - 122);
								$('#search_container').animate({
									left: 210,
									opacity: 1
								}, 500, 'easeOutCubic');
							});
							$('#recent_social').animate({
								right: 0,
								opacity: 1
							}, 500, 'easeOutCubic');
						})
					});
				} else {
					$('#incubator_thumbs').animate({
						left: 20,
						opacity: 1
					}, 500, 'easeOutCubic', function() {
						if (typeof(current[2]) != "undefined") {
							$('a[href*="' + current[2] + '"]').updatePortfolioItem();
						}
					});
				}
				break;
			
		case 'what-we-do':
			if (typeof(current[2]) == "undefined") {
				$.address.value($('#service_thumbs .items li:first-child a').attr('href'));
			}
			$('#service_nav').updateNav();
			$('#service_nav').clearItems(current[1]);
			
			$('#service_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				if (typeof(current[2]) != "undefined") {
					$('a[href*="' + current[2] + '"]').updatePortfolioItem();
				}
			});
			break;
			
		case 'job':
			category = false;
			switch (current[2]) {
			case 'uk':
				category = true;
			case 'usa-offices':
				category = true;
			case 'LightmakerAMS':
				category = true;
			}
			$('#job_menu').slideDown();
			$('#portfolio_menu').slideUp();
			$('#social_menu').slideUp();
			if (typeof(current[2]) == "undefined" || category) {
				if (typeof(current[2]) == "undefined") {
					$('#job_thumbs .items li').fadeIn();
				}
				$.address.value($('#job_thumbs .items li:visible a').attr('href'));
			}
			$('#' + current[1] + '_nav').updateNav();
			$('#' + current[1] + '_nav').clearItems(current[1]);
			$('#job_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				if (typeof(current[2]) != "undefined") {
					$('a[href="/job/' + current[2] + '/"]').updatePageItem();
				}
			});
			break;
			
		case 'search':
			$('.dynamic_column_container').animate({
				left: -1000,
				opacity: 0
			}, 500, 'easeInCubic', function() {
				$("#social .cool_containers").hide();
				$("#testimonials .cool_containers").hide();
			});
			$('.main_content').animate({
				left: -1000,
				opacity: 0
			}, 500, 'easeInCubic', function() {
				$('.thumbs_nav').animate({
					left: -800,
					opacity: 0
				}, 500, 'easeOutCubic', function() {
					if ($(window).width() < 1338) {
						smallSize();
					} else {
						midSize();
					}
					$('#search_container').load("/search/" + current[2].replace(/ /g, "+") + '?ajax=1', function(response, status, xhr) {
						$('#header_icon_search_results').click(function() {
							if ($('#search-lm_results').val() != "") {
								$.address.value('/search/' + $('#search-lm_results').val());
							}
							return false;
						});
						$('#search_form_results').keydown(function(e) {
							if (e.keyCode == 13 && $('#search-lm_results').val() != "") {
								$.address.value('/search/' + $('#search-lm_results').val());
								return false;
							}
						});
						$('#search_container a[rel^="address"]').click(function(e) {
							e.preventDefault();
						});
						var initContentHeight = $('#search_container').height();
						$('#still_container').css('height', initContentHeight);
						$('#pane').css('height', initContentHeight - 122);
						$('#pane2').css('height', initContentHeight - 122);
						$('#recent_social_follow').css('height', initContentHeight - 122);
						$('#search_container').animate({
							left: 210,
							opacity: 1
						}, 500, 'easeOutCubic');
						$("#search_form_results label").inFieldLabels();
					});
					$('#recent_social').animate({
						right: 0,
						opacity: 1
					}, 500, 'easeOutCubic');
				})
			});
			break;
			
		case 'accessibility':
			_gaq.push(['_trackEvent', 'page', 'accessibility']);
			$('#mostrecent_nav').clearItems(current[1]);
			$('#mostrecent_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				$('a[href*="/accessibility"]').updatePortfolioItem();
			});
			break;
			
		case 'terms-and-conditions':
			_gaq.push(['_trackEvent', 'page', 'terms-and-conditions']);
			$('#mostrecent_nav').clearItems(current[1]);
			$('#mostrecent_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				$('a[href*="/terms-and-conditions"]').updatePortfolioItem();
			});
			break;
			
		case 'terms':
			$.address.value('/terms-and-conditions');
			break;
			
		case 'tag':
			$('.dynamic_column_container').animate({
				left: -1000,
				opacity: 0
			}, 500, 'easeInCubic', function() {
				$("#social .cool_containers").hide();
				$("#testimonials .cool_containers").hide();
			});
			$('.main_content').animate({
				left: -1000,
				opacity: 0
			}, 500, 'easeInCubic', function() {
				$('.thumbs_nav').animate({
					left: -800,
					opacity: 0
				}, 500, 'easeOutCubic', function() {
					if ($(window).width() < 1338) {
						smallSize();
					} else {
						midSize();
					}
					$('#search_container').load("/search/" + current[2].replace(" ", "+") + '?ajax=1', function(response, status, xhr) {
						$('#header_icon_search_results').click(function() {
							if ($('#search-lm_results').val() != "") {
								$.address.value('/search/' + $('#search-lm_results').val());
							}
							return false;
						});
						$('#search_form_results').keydown(function(e) {
							if (e.keyCode == 13 && $('#search-lm_results').val() != "") {
								$.address.value('/search/' + $('#search-lm_results').val());
								return false;
							}
						});
						$('#search_container a[rel^="address"]').click(function(e) {
							e.preventDefault();
						});
						var initContentHeight = $('#search_container').height();
						$('#still_container').css('height', initContentHeight);
						$('#pane').css('height', initContentHeight - 122);
						$('#pane2').css('height', initContentHeight - 122);
						$('#recent_social_follow').css('height', initContentHeight - 122);
						$('#search_container').animate({
							left: 210,
							opacity: 1
						}, 500, 'easeOutCubic');
						$("#search_form_results label").inFieldLabels();
					});
					$('#recent_social').animate({
						right: 0,
						opacity: 1
					}, 500, 'easeOutCubic');
				})
			});
			break;
			
		default:
			if (typeof(current[2]) == "undefined") {
				current[2] = $('#mostrecent_thumbs .items li:first-child a').attr('href');
			}
			$('#' + current[1] + '_nav').clearItems(current[1]);
			$('#news_nav').updateNav();
			$('#mostrecent_thumbs').animate({
				left: 20,
				opacity: 1
			}, 500, 'easeOutCubic', function() {
				if (typeof(current[2]) != "undefined") {
					$('a[href*="' + current[2] + '"]').updatePortfolioItem();
				}
			});
		}
	});
	
	/* upadte nav */
	$.fn.updateNav = function() {
		$(this).parent().find('.current').removeClass('current');
		$(this).children().addClass('current');
		return false;
	}
	
	/* clear common items */
	$.fn.clearItems = function(page) {
	
		/* HIDE SOCIAL AND TESTIMONIAL PAGES AND REMOVE ITEMS */
		$('.dynamic_column_container').animate({
			left: -1000,
			opacity: 0
		}, 500, 'easeInCubic', function() {
			$("#social .cool_containers").remove();
			$("#testimonials .cool_containers").remove();
		});
		
		/* HIDE SEARCH CONTAINER */
		$('#search_container').animate({
			left: -1000,
			opacity: 0
		}, 500, 'easeInCubic');
		
		/* HIDE MAIN CONTENT CONTAINER AND EMPTY CONTENTS */
		$('#main_content').animate({
			left: -1000,
			opacity: 0
		}, 500, 'easeInCubic', function() {
			$('#main_content').empty();
		});
		
		/* HIDE ALL THUMBNAILS */
		if (page != "what-we-do") {
			$('#service_nav .current').removeClass('current');
			$('#service_thumbs').animate({
				left: -800,
				opacity: 0
			}, 500, 'easeOutCubic');
		}
		if (page != "office") {
			$('#office_nav .current').removeClass('current');
			$('#contact_thumbs').animate({
				left: -800,
				opacity: 0
			}, 500, 'easeOutCubic');
		}
		if (page != "job") {
			$('#job_nav .current').removeClass('current');
			$('#job_thumbs').animate({
				left: -800,
				opacity: 0
			}, 500, 'easeOutCubic');
		}
		if (page != "news") {
			$('#news_nav .current').removeClass('current');
			$('#news_thumbs').animate({
				left: -800,
				opacity: 0
			}, 500, 'easeOutCubic');
		}
		if (page != "mostrecent") {
			$('#mostrecent_thumbs').animate({
				left: -800,
				opacity: 0
			}, 500, 'easeOutCubic');
		}
		if (page != "awards") {
			$('#awards_nav .current').removeClass('current');
			$('#awards_thumbs').animate({
				left: -800,
				opacity: 0
			}, 500, 'easeOutCubic');
		}
		if (page != "incubator") {
			$('#incubator_nav .current').removeClass('current');
			$('#incubator_thumbs').animate({
				left: -800,
				opacity: 0
			}, 500, 'easeOutCubic');
		}
		if (page != "portfolio") {
			$('#thumbs').animate({
				left: -800,
				opacity: 0
			}, 500, 'easeOutCubic');
		}
		
		/* SHOW MAIN THUMB NAV - ONLY DOES ANYTHING WHEN COMING FORM TESTIMONIAL OR SOCIAL PAGE */
		$('.thumbs_nav').animate({
			left: 0,
			opacity: 1
		}, 500, 'easeOutCubic');
		testimonial_page = 1;
		return false;
	}
});

