$(document).ready(function(e) {
			
	// idevice detection - if detected, bail from all hover events
	var $isIDevice = (
		(navigator.platform.indexOf("iPhone") != -1) ||
		(navigator.platform.indexOf("iPod") != -1) ||
		(navigator.platform.indexOf("iPad") != -1)
	);

	// hide cursors
	$('body').css('cursor', 'none');
	$('a').css('cursor', 'none');
	
	// apply appropriate bg image and do custom cursor movements
	if(screen.width>=1900) {
		var $bgf = 'bg-1920.jpg';
	} else {
		if(screen.width>=1600) {
			var $bgf = 'bg-1600.jpg';
		} else {
			var $bgf = 'bg-1080.jpg';
		}
	}
	if($isIDevice) {
		$('body').css('background-image', 'url(/assets/images/bg-iDevice.jpg)');
		$('#cursor_a').hide();
		$('#cursor_b').hide();
		$('#cursor_c').hide();
	} else {
		$('body').css('background-image', 'url(/assets/images/'+$bgf+')').mousemove(
			function(e) {
				$('#cursor_a').show('fade', 200).css('top', e.pageY + 4);
				$('#cursor_b').show('fade', 200).css('left', e.pageX + 4);
				$('#cursor_c').show('fade', 200).css('top', e.pageY + 2).css('left', e.pageX + 2);
			}
		);
	}
	
	// logo hover and click
	var $bookout = false;
	function bookToggle() {
		if($bookout == true) {
			$('#the-book').hide('fold', 400, function() { 			   
				$('#preview-window').show('slide', { direction: "left" }, 400); 
				if($('#sub-menu-wrapper')) { $('#sub-menu-wrapper').show('slide', { direction: "left" }, 400); };
				if($('#sub-menu-wrapper-2-lines')) { $('#sub-menu-wrapper-2-lines').show('slide', { direction: "left" }, 400); };	
				if($('#sub-menu-wrapper-contact')) { $('#sub-menu-wrapper-contact').show('slide', { direction: "left" }, 400); };
			});
			$bookout = false;
		} else {
			if($('#sub-menu-wrapper')) { $('#sub-menu-wrapper').hide('slide', { direction: "left" }, 400); };
			if($('#sub-menu-wrapper-2-lines')) { $('#sub-menu-wrapper-2-lines').hide('slide', { direction: "left" }, 400); };
			if($('#sub-menu-wrapper-contact')) { $('#sub-menu-wrapper-contact').hide('slide', { direction: "left" }, 400); };
			$('#preview-window').hide('slide', { direction: "left" }, function() { 
				$('#the-book').show('fold', 400); 
			});
			$bookout = true;
		}
	}
	$('#logo').hover(
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			$(this).css('background-position', '-133px 0px');
		},
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			$(this).css('background-position', '0px 0px');
		}			 
	).click(
		function() {
			bookToggle();
		}
	);
	$('#the-book').click(
		function() {
			bookToggle();
		}
	);
	
	// main menu clicks
	$('#main-menu-list a').click(
		function(e) {
			if($(this).hasClass('active') || $(this).hasClass('propen')) {
				if($(this).hasClass('projects')) {
					$('#main-menu-sub').slideUp('fast', function() {});
					$(this).removeClass('propen');
				}
			} else {
				if($(this).hasClass('projects')) {
					$('#main-menu-sub').slideDown('fast', function() {});
					$('#main-menu-list a').each(
						function() {
							if($(this).hasClass('active') && $(this).data('sublabel') != 'yes') {
								$(this).stop(true, true).removeClass('active');
							}
						} 
					);
					$(this).addClass('propen');
				} else {
					$('#main-menu-list a').each(
						function() {
							if($(this).hasClass('active')) {
								$(this).stop(true, true).removeClass('active');
							}
						} 
					);
					$(this).stop(true, true).addClass('active').css('background-position', '-270px 0px');
				}
			}
			if(!$(this).hasClass('projects')) {
				$('#page-loader').show();
				$('#preview-window').fadeOut(200 , function() {});
				$('#sub-menu-wrapper, #sub-menu-wrapper-2-lines, #sub-menu-wrapper-contact').fadeOut(200 , function() {});
			}
		}							  
	);   
	
	// main menu hovers
	$('#main-menu-list a').hover(
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			if(!$(this).hasClass('active')) {
				$(this).animate( { backgroundPosition: "-135px 0px" }, 'fast' );
			}
		},
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			if(!$(this).hasClass('active')) {
				$(this).stop(true, true).css('background-position', '0px 0px');
			}
		}
	);
	
	// sub menu hovers
	$('#sub-menu-wrapper a, #sub-menu-wrapper-2-lines a').hover(
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			$(this).animate( { backgroundPosition: "0px 0px" }, 'fast' );
			var $projectKey = $(this).data('projectkey');
			$('#image-'+$projectKey).stop(true, true).fadeIn(300);
		},
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			$(this).stop(true, true).css('background-position', '-300px 0px');
			var $projectKey = $(this).data('projectkey');
			$('#image-'+$projectKey).stop(true, true).fadeOut(300);
		}					 
	).click( function() { 
		$('#page-loader').show();
		$('#sub-menu-wrapper, #sub-menu-wrapper-2-lines, #sub-menu-wrapper-contact').hide("slide", { direction: "left" }, 200);
		$('#preview-window').fadeOut(200 , function() {}); 
	});
	
	// 'open' content/preview windows
	$('#preview-window').slideDown(400 , function() { $('#content-window').fadeIn(200 , function() {}); });

	// 'open' sub menu
	$('#sub-menu-wrapper, #sub-menu-wrapper-2-lines, #sub-menu-wrapper-contact').show("slide", { direction: "left" }, 500, function() { $('#sub-menu-wrapper ul, #sub-menu-wrapper-2-lines ul').show(); } );
	
	// project slideshow
	if($('#project-slideshow')) {
		var $slideshow = {
			count : 0, current : 1,
			next : function() {
				if(this.current < this.count) {
					$('#slide-'+this.current).hide('slide', { direction: "left" }, 300);
					this.current++;
					$('#slide-'+this.current).show('slide', { direction: "right" }, 300);
				}
				if(this.current == this.count) {
					$('#title-button-next').css('opacity', '0.0').css('background-position', '0px 0px');
				}
				$('#title-button-prev').css('opacity', '1.0');
			},
			prev : function() {
				if(this.current > 1) {
					$('#slide-'+this.current).hide('slide', { direction: "right" }, 300);
					this.current--;
					$('#slide-'+this.current).show('slide', { direction: "left" }, 300);
				}
				if(this.current == 1) {
					$('#title-button-prev').css('opacity', '0.0').css('background-position', '0px 0px');
				}
				$('#title-button-next').css('opacity', '1.0');
			}
		};
		$slideshow.count = $('#project-slideshow li').length;
		if($slideshow.count == 1) {
			$('#title-button-prev').css('opacity', '0.0').css('background-position', '0px 0px');
			$('#title-button-next').css('opacity', '0.0').css('background-position', '0px 0px');
		}
		$('#title-button-prev').click(
			function() {
				if($slideshow.current > 1) {
					$slideshow.prev();
				}
			}
		).hover(
			function() {
				if($isIDevice) { return; } // no hovers for idevices
				if($slideshow.current == 1) { return false; }
				$(this).css('background-position', '-'+$(this).width()+'px 0px');
			},
			function() {
				if($isIDevice) { return; } // no hovers for idevices
				if($slideshow.current == 1) { return false; }
				$(this).css('background-position', '0px 0px');
			}
		).mousedown(
			function() {
				if($slideshow.current == 1) { return false; }
				$(this).css('background-position', '-'+($(this).width()*2)+'px 0px');
			}
		);
		$('#title-button-next').click(
			function() {
				if($slideshow.current < $slideshow.count) {
					$slideshow.next();
				}
			}
		).hover(
			function() {
				if($isIDevice) { return; } // no hovers for idevices
				if($slideshow.current == $slideshow.count) { return false; }
				$(this).css('background-position', '-'+$(this).width()+'px 0px');
			},
			function() {
				if($isIDevice) { return; } // no hovers for idevices
				if($slideshow.current == $slideshow.count) { return false; }
				$(this).css('background-position', '0px 0px');
			}
		).mousedown(
			function() {
				if($slideshow.current == $slideshow.count) { return false; }
				$(this).css('background-position', '-'+($(this).width()*2)+'px 0px');
			}
		);
	}
	
	// top buttons
	var $awards_out = false;
	var $text_out = false;
	$('#title-button-awards').click(
		function() {
			if($awards_out) {
				$(this).css('background-position', '-'+$(this).width()+'px 0px');
				$('#awards-content').slideUp(200 , function() {});
				$awards_out = false;
			} else {
				$('#title-button-text').css('background-position', '0px 0px'); $('#text-content').hide(); $text_out = false;
				$('#awards-content').slideDown(300 , function() {});
				$awards_out = true;
			}
		}
	).hover(
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			if(!$awards_out) {
				$(this).css('background-position', '-'+$(this).width()+'px 0px');
			}
		},
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			if(!$awards_out) {
				$(this).css('background-position', '0px 0px');
			}
		}
	).mousedown(
		function() {
			$(this).css('background-position', '-'+($(this).width()*2)+'px 0px');
		}
	);
	$('#title-button-text').click(
		function() {
			if($text_out) {
				$(this).css('background-position', '-'+$(this).width()+'px 0px');
				$('#text-content').slideUp(200 , function() {});
				$text_out = false;
			} else {
				$('#title-button-awards').css('background-position', '0px 0px'); $('#awards-content').hide(); $awards_out = false;
				$('#text-content').slideDown(300 , function() {});
				$text_out = true;
			}
		}
	).hover(
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			if(!$text_out) {
				$(this).css('background-position', '-'+$(this).width()+'px 0px');
			}
		},
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			if(!$text_out) {
				$(this).css('background-position', '0px 0px');
			}
		}
	).mousedown(
		function() {
			$(this).css('background-position', '-'+($(this).width()*2)+'px 0px');
		}
	);
	$('#title-button-pdf').hover(
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			if(!$awards_out) {
				$(this).css('background-position', '-'+$(this).width()+'px 0px');
			}
		},
		function() {
			if($isIDevice) { return; } // no hovers for idevices
			if(!$awards_out) {
				$(this).css('background-position', '0px 0px');
			}
		}
	).mousedown(
		function() {
			$(this).css('background-position', '-'+($(this).width()*2)+'px 0px');
		}
	);
	$('div.common-drop-content-frame img').click(
		function() {
			$(this).parent().slideUp(200 , function() {});
			$('#title-button-awards').css('background-position', '0px 0px'); $awards_out = false;
			$('#title-button-text').css('background-position', '0px 0px'); $text_out = false;
		}
	);

});
