var firstClick = true;
var lastPosXLess = 250;
var clickedLink;
var menuItem0PosX;
var menuItem1PosX;
var menuItem2PosX;
var menuItem3PosX;
var menuItem4PosX;
var menuItem5PosX;
var MenuItemPosXArray;

var AjaxContent = function() {
    var container_div = '';
	var content_div = '';
	return {
							
		getContent : function(url,index) {
			clickedLink = ($("ul.menu li:nth-child("+(index+1)+") a"));
			
			if (index != undefined) {
			
				$(container_div).animate({opacity:1},
				
					function() { // the callback, loads the content with ajax
					
						//$('#content').animate({opacity:1});
						
						if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
							$(container_div).load(url+" "+content_div, //only loads the selected portion
							function() {
								myScroll = new iScroll('wrapper');
								var curMenuItemIndex = index;								
								switch (curMenuItemIndex) {
									case 0:
										var curPosXLess = 250; //products 250
										break;
									case 1:
										var curPosXLess = 400; //aboutus 400
										break;
									case 2:
										var curPosXLess = 250; //clients 250
										break;
									case 3:
										var curPosXLess = 450; //contact 450
										break;
									case 4:
										var curPosXLess = 450; //news 450
										break;
								}
								
								menuItem0PosX = $('ul.menu li a.products').position().left;
								menuItem1PosX = $('ul.menu li a.aboutus').position().left;
								menuItem2PosX = $('ul.menu li a.clients').position().left;
								menuItem3PosX = $('ul.menu li a.contact').position().left;
								menuItem4PosX = $('ul.menu li a.news').position().left;
								menuItem5PosX = $('ul.menu li a.clientaccess').position().left;
								MenuItemPosXArray = [menuItem0PosX,menuItem1PosX,menuItem2PosX,menuItem3PosX,menuItem4PosX,menuItem5PosX];
																
								$.each($('ul.menu li'), function(index) {
									if (index == curMenuItemIndex) {
										var childSelector = $(this).children().attr('class');
										
										if (childSelector == 'products') {
											$(function() {
												$.fn.superstatic.options = {  
													slides : [
														{image : './wordpress/images/products/dkny1.jpg'}
													]
												};
												$('#superstatic').superstatic('#superstatic');
																								
												$('#product-content div.content div').each(function() {
													if ($(this).index() == 0) {
														$(this).css({ display: 'block' });
														$(this).delay(1600).animate({ opacity: 1 }, 500);
														$('#product-content').css({ display: 'block', position: 'relative', top: 500 });
														$('#product-menu').css({ display: 'block' });
														$('ol.products').css({ display: 'block', position: 'relative', top: 550 + $(this).outerHeight() });
														$('ol.products li a').css({ 'font-size': '21px', 'line-height': '38px' });
												        $('h2.titleh2').animate({ opacity: 1 }, 500);
													}
												});
										    });
										} else if (childSelector == 'aboutus') {
											$(function() {
												$.fn.superstatic.options = {  
													slides : [
														{image : './wordpress/images/aboutus.jpg'}
													]
												};
										        $('#superstatic').superstatic('#superstatic');
										    });
										} else if (childSelector == 'clients') {
											$(function() {
												$.fn.superstatic.options = {  
													slides : [
														{image : './wordpress/images/clients.jpg'}
													]
												};
										        $('#superstatic').superstatic('#superstatic');
										    });
										} else if (childSelector == 'contact') {
											$(function() {
												$.fn.superstatic.options = {  
													slides : [
														{image : './wordpress/images/contact.jpg'}
													]
												};
										        $('#superstatic').superstatic('#superstatic');
										    });
										} else if (childSelector == 'news') {
											$(function() {
												$.fn.superstatic.options = {  
													slides : [
														{image : './wordpress/images/news.jpg'}
													]
												};
										        $('#superstatic').superstatic('#superstatic');
										    });
										}
										
										// position the content near the left of menu items
										//if (navigator.userAgent.match(/iPad/i)) {											
											/*if ($('ul.menu').position().left + MenuItemPosXArray[index] - 30 >= $(window).width() - $('#contentWrapper').width()) {
												$('#contentWrapper').css({ width: curPosXLess, left: $(window).width() - $('#contentWrapper').width() });
											} else {*/
												//$('#contentWrapper').css({ width: curPosXLess, left: $('ul.menu').position().left + MenuItemPosXArray[index] - 30 });
												$('#contentWrapper').css({ width: curPosXLess, left: 0 });
											//}
										//}
										
										$('#interiorWrapper').css({ width: curPosXLess });
									}									
								});
								
								$('#interiorWrapper').animate({ opacity: 1 }, 2000);
								$('#content').css({ opacity: 1 });
							});
							
							
						} else {
						
							$(container_div).load(url+" "+content_div, //only loads the selected portion
							function() {
								
								if (firstClick == true) {
									menuItem0PosX = $('ul.menu li a.products').position().left;
									menuItem1PosX = $('ul.menu li a.aboutus').position().left;
									menuItem2PosX = $('ul.menu li a.clients').position().left;
									menuItem3PosX = $('ul.menu li a.contact').position().left;
									menuItem4PosX = $('ul.menu li a.news').position().left;
									menuItem5PosX = $('ul.menu li a.clientaccess').position().left;
									MenuItemPosXArray = [menuItem0PosX,menuItem1PosX,menuItem2PosX,menuItem3PosX,menuItem4PosX,menuItem5PosX];
								}
								firstClick = false;
								
								var curMenuItemIndex = index;
								var prevMenuItemIndex = (curMenuItemIndex - 1);
								if (prevMenuItemIndex < 0) {
									prevMenuItemIndex = 0;
								}
								switch (curMenuItemIndex) {
									case 0:
										var curPosXLess = 250; //products
										break;
									case 1:
										var curPosXLess = 440; //aboutus
										var curLPos = '-45px'; //products
										break;
									case 2:
										var curPosXLess = 250; //clients
										var curLPos = '166px'; //products
										break;
									case 3:
										var curPosXLess = 360; //contact
										var curLPos = '241px'; //products
										break;
									case 4:
										var curPosXLess = 360; //news
										var curLPos = '336px'; //products
										break;
								}

								//Stop slideshow
								clearInterval(slideshow_interval);
								$.paused = true;
								
								$('ul.menu li').each(function(index) {
									
									// Less Than & Equal To
									if(index <= curMenuItemIndex) {
										
										if (index != 0) {
											var xPosDiff = (MenuItemPosXArray[index] - MenuItemPosXArray[(index-1)]) - curPosXLess;
										} else {
											var xPosDiff = MenuItemPosXArray[index+1] - curPosXLess - 20;
										}
										
										if (curMenuItemIndex != 0) {
											if (index == 0 && $(this).hasClass('static')) {
												$(this).css({ position: 'relative', left: -350, bottom: 0 });
												$(this).stop().animate({ left: xPosDiff }, 500);
											} else {
												$(this).css({ position: 'relative', left: 0 });
												$(this).stop().animate({ left: xPosDiff }, 500);
											}
										} else {
											$(this).css({ position: 'fixed', left: $('ul.menu').position().left, bottom: $('#menu').scrollTop() + 154 });
											$(this).stop().animate({ left: 280 }, 500);
										}
										
										$(this).addClass('static');
										$(this).removeClass('selected');
										
										// Is Equal To
										if (index == curMenuItemIndex) {
											$(this).addClass('selected');
											var childSelector = $(this).children().attr('class');
											var backgroundUrl = "./wordpress/images/" + childSelector + ".jpg";
											$('#activeslide-static-blur-container-' + childSelector).animate({opacity:0}, 0);
											
											// $('#contentWrapper').css({ position: 'relative', left: -$(window).width() });
											
											if (curMenuItemIndex != 0) {
												// $('#interiorWrapper').css({ opacity: 1, position: 'relative', left: lastPosXLess, width: 1 });
												$('#interiorWrapper').css({ opacity: 1, position: 'absolute', width: 1 });
											} else {
												$('#interiorWrapper').css({ opacity: 1, position: 'fixed', width: 1, height: '2000px' });
											}
											$('#content').css({ width: curPosXLess });
										
											if (curMenuItemIndex != 0) {
												lastPosXLess = $('ul.menu').position().left + MenuItemPosXArray[index] + xPosDiff - 20;
											} else {
												lastPosXLess = 280;
											}		
											var posPosXLess = lastPosXLess;
											//$('#interiorWrapper').animate({ left: posPosXLess }, 500, function() { 
											// curLPos();
											// $('#interiorWrapper').animate({ left: clickedLink.offset().left }, 500, function() { });
											// Page Transition Effect
											$('#content').animate('.contentTransition', 500, function() {
												$('#interiorWrapper').animate({left: (clickedLink.offset().left - 22)}, 250, function(){
													$('#interiorWrapper').delay(100).animate({ width: curPosXLess }, 500, function() {
														// $('#interiorWrapper').css({ overflow: 'visible' });
														$('h2.titleh2').animate({ opacity: 1 }, 500);
													});
												});
											});
											
											if (childSelector == 'products') {
												$(function() {
													$.fn.superstatic.options = {  
														slides : [
															{image : './wordpress/images/products/dkny1.jpg'}
														]
													};
											        $('#superstatic').superstatic('#superstatic');
											        
											        $('#product-content div.content div').each(function() {
														if ($(this).index() == 0) {
															$(this).css({ display: 'block' });
															$(this).delay(1600).animate({ opacity: 1 }, 500);
													        $('#product-content').css({ position: 'fixed', left: 280, bottom: 210 + $(this).outerHeight() });
														}
													});
											        
											        $('ul.menu li a.products').mouseover(function() {
											        	if(productMenuActive == false && $(this).parent().hasClass('selected') == true) {
												        	showProductMenu();
														}
													});
											    });
											} else if (childSelector == 'aboutus') {
												$(function() {
													$.fn.superstatic.options = {  
														slides : [
															{image : './wordpress/images/aboutus.jpg'}
														]
													};
											        $('#superstatic').superstatic('#superstatic');
											    });
											} else if (childSelector == 'clients') {
												$(function() {
													$.fn.superstatic.options = {  
														slides : [
															{image : './wordpress/images/clients.jpg'}
														]
													};
											        $('#superstatic').superstatic('#superstatic');
											    });
											} else if (childSelector == 'contact') {
												$(function() {
													$.fn.superstatic.options = {  
														slides : [
															{image : './wordpress/images/contact.jpg'}
														]
													};
											        $('#superstatic').superstatic('#superstatic');
											    });
											} else if (childSelector == 'news') {
												$(function() {
													$.fn.superstatic.options = {  
														slides : [
															{image : './wordpress/images/news.jpg'}
														]
													};
											        $('#superstatic').superstatic('#superstatic');
											    });
											}
										}
									}
									
									
									//ipad targeting
									if(myScroll != undefined){
										myScroll.destroy();
										myScroll = null;
										// myScroll = new TouchScroll(document.getElementById("content")); 
										setTimeout('myScroll = new iScroll("content"); ', 1500);
									}
									
									// Great Than
									if(index > curMenuItemIndex) {
										if ($(this).hasClass('static')) {
											$(this).removeClass('static');
											$(this).removeClass('selected');
											var posPosXLess =  (MenuItemPosXArray[index] - MenuItemPosXArray[(index-1)]) + 350;
											$(this).stop().animate({ left: 0 }, function() {
												//
											});
										}
									}
									
								});
								posPosX = MenuItemPosXArray[curMenuItemIndex] - 250;
								
								
							}); //end function
							
						} //end if (iphone)
					} //end function
					
				); //end animate
					
			} //end if
				
		},
		
		ajaxify_links: function(elements) {
			
			// set opacity of menu items
			$(elements).css('opacity','0.7');
			
			// on mouse over
			/*
			$(elements).hover(function() {
				var _element = $(this);
				if(_element.attr('disabled') != 'disabled'){
					_element.stop().animate({
						opacity: 1
					}, 200);
				}
			},	
			// on mouse out
			function() {
				if(_element.attr('disabled') != 'disabled'){
					_element.stop().animate({
						opacity: 0.7
					}, 200);
				}
			});
			
			$(elements).click(function() {
				var _element = $(this);
				if(_element.attr('disabled') != 'disabled'){
										
					//re-enable all elements					
					$('ul.menu li').each(function() {
						var _ele = $(this);
						var _eleA = $(this).find('a');
						if(_eleA.attr('disabled') == 'disabled'){
							_eleA.stop().css({ backgroundPosition: '0px 0px' });
							_eleA.stop().animate({ opacity: 0.7 }, {duration:1});
							_eleA.removeAttr('disabled');
							_eleA.css({ 'pointer-events': 'auto', 'cursor': 'pointer' });
						}
					});
					
					//disable selected element
					_element.stop().animate({
						opacity: 1
					}, 'fast');
					_element.stop().css({ backgroundPosition: '0px -20px' });
					
					_element.attr('disabled', 'disabled');
					var childSelector = _element.attr('class');
					if (childSelector != 'products') {
						_element.css({ 'pointer-events': 'none', 'cursor': 'default' });
					}
					
					var curItemIndex = $(this).parent().index();
					if (_element.hasClass('external')) {
						AjaxContent.getContent(this.href);
					} else {
						AjaxContent.getContent(this.href,curItemIndex);
						return false; //prevents the link from being followed
					}
				}
			});
			*/
			
			$(elements).hover(function() {
				if($(this).attr('disabled') != 'disabled'){
					$(this).stop().animate({
						opacity: 1
					}, 200);
				}
			},	
			// on mouse out
			function() {
				if($(this).attr('disabled') != 'disabled'){
					$(this).stop().animate({
						opacity: 0.7
					}, 200);
				}
			});
			
			$(elements).click(function() {
				if($(this).attr('disabled') != 'disabled'){
										
					//re-enable all elements
					$('ul.menu li').each(function() {
						if($(this).find('a').attr('disabled') == 'disabled'){
							$(this).find('a').stop().css( 'background-position-y', '0px' );
							$(this).find('a').stop().animate({ opacity: 0.7 }, {duration:1});
							$(this).find('a').removeAttr('disabled');
							$(this).find('a').css({ 'pointer-events': 'auto', 'cursor': 'pointer' });
						}
					});
					
					//disable selected element
					$(this).stop().animate({
						opacity: 1
					}, 'fast');
					$(this).stop().css( 'background-position-y', '-20px' );
					
					$(this).attr('disabled', 'disabled');
					var childSelector = $(this).attr('class');
					
					if (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i)) {
						$(this).css({ 'pointer-events': 'none', 'cursor': 'default' });
					} else {
						if (childSelector != 'products') {
							$(this).css({ 'pointer-events': 'none', 'cursor': 'default' });
						}
					}
					
					var curItemIndex = $(this).parent().index();
					if ($(this).hasClass('external')) {
						AjaxContent.getContent(this.href);
					} else {
						AjaxContent.getContent(this.href,curItemIndex);
						return false; //prevents the link from being followed
					}
				}
			});
		},
	
		init: function(params) { //sets the initial parameters
			container_div = params.containerDiv; 
			content_div = params.contentDiv;
			return this; //returns the object in order to make it chainable
		}
	}
}();
