// JavaScript Document

$(document).ready(function(){
	
	var thePath = $(location).attr('href').toLowerCase();
	var urlPart = thePath.indexOf("morsekode.com");
	var local = '';
	var root = '';
	if (urlPart != -1) {
		root = "";
		local = false;
	} else {
		var urlPart2 = thePath.indexOf("localhost");
		if (urlPart2 != -1) {
			root = "http://localhost/mk-new-site";
			local = true;
		} else {
			root = "http://10.168.1.75/mk-new-site";
			local = true;
		}
	}
	
	var ua = navigator.userAgent.toLowerCase();
	var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
	
	// THIS CODE IS RUN ON INITIAL PAGE LOAD TO SET UP WHAT SHOULD SHOW
	
	var newHash = window.location.hash.substr(1);
	if (newHash == '' || newHash == 'undefined') {
		$("#headlines-section").find(".hidethis").delay("1000").slideToggle("400");
		$("#headlines-section").addClass("current");
	} else {
		var slashCharactersReload = newHash.indexOf("/", 3);
		if (slashCharactersReload != -1) { 
			// if hash does include a slash (/) mark
			var theSection = newHash.substr(2,slashCharactersReload - 2);
			var theLink = newHash.substr(slashCharactersReload + 1);
			if (theSection == "work") {
				$("#work-section").find(".hidethis").show();
				$("#work-section").addClass("current");
				showworkclick(theLink);
			} else if (theSection == "headlines") {
				showheadlinesclick(theLink);
			} else if (theSection == "kode") {
				$("#kode-section").find(".hidethis").show();
				$("#kode-section").addClass("current");
				showkodeclick(theLink);
			} else if (theSection == "team") {
				$("#team-section").find(".hidethis").show();
				$("#team-section").addClass("current");
				showteamclick(theLink);
			} else if (theSection == "work-archives") {
			$("#work-section").find(".hidethis").delay("1000").slideToggle("400");
				$("#work-section").addClass("current");
				$("#featuredwork").hide();
				showworkarchivesbycat(theLink);
			} else if (theSection == "headline-archives") {
				$("#headlines-section").find(".hidethis").delay("1000").slideToggle("400");
				$("#headlines-section").addClass("current");
				$("#featuredheadlines").hide();
				showheadlinearchivesbycat(theLink);
			}
		} else {
			if (newHash == "work-archives") {
				$("#work-section").find(".hidethis").delay("1000").slideToggle("400");
				$("#work-section").addClass("current");
				$("#featuredwork").hide();
				showworkarchiveslist();
			} else if (newHash == "headline-archives") {
				$("#headlines-section").find(".hidethis").delay("1000").slideToggle("400");
				$("#headlines-section").addClass("current");
				$("#featuredheadlines").hide();
				showheadlinearchiveslist();
			} else {
				var theSection = newHash.substr(2);
				$("#" + theSection + "-section").find(".hidethis").delay("1000").slideToggle("400");
				$("#" + theSection + "-section").addClass("current");
			}
		}
	}
	
	/*
	function is_touch_device() {  
	  try {  
		document.createEvent("TouchEvent");  
		return true;  
	  } catch (e) {  
		return false;  
	  }  
	}
	*/
	
	// THIS CODE MOSTLY CONTROLS WHEN PEOPLE CLICK ON THINGS
	
	$("h2").click(function() {
		var tempSection = $(this).parents(".section").attr('id');
		var tempSectionDash = tempSection.indexOf("-");
		var whichSection = tempSection.substr(0,tempSectionDash);
		window.location.hash = "!/" + whichSection;
		return false;
	});
	
	$(".boxmenu a, .teamboxmenu a").click(function() {
		var tempSection = $(this).parents(".section").attr('id');
		var tempSectionDash = tempSection.indexOf("-");
		var whichSection = tempSection.substr(0,tempSectionDash);
		window.location.hash = "!/" + whichSection + "/" + $(this).attr('id');
		return false;
	});
	
	$("#work-archives, #headline-archives").click(function() {
		window.location.hash = "!/" + $(this).attr('id');
		return false;
	});
	
	$("h2").hover(function () {
		$(this).addClass("hover");
	}, function () {
		$(this).removeClass("hover");
	});
	
	$('#yourname, #youremail, #message').clearingInput();
	
	// THIS CODE IS RUN EVERY TIME SOMEONE CLICKS ON SOMETHING AND IT CHANGES THE URL AT THE TOP OF THE PAGE
	
	// Not all browsers support hashchange. For older browser support: http://benalman.com/projects/jquery-hashchange-plugin/
	$(window).bind('hashchange', function(e) {
		newHash = window.location.hash.substr(1);
		$("#showheadlines").trigger('close');
		$("#showwork").trigger('close');
		$("#showkode").trigger('close');
		$("#showteam").trigger('close');
		var slashCharacters = newHash.indexOf("/", 3);
		if (slashCharacters != -1) { // if hash does include a slash (/) mark
			var theLink = newHash.substr(slashCharacters + 1);
			theSection = newHash.substr(2,slashCharacters - 2);
			if (local == false) {
				_gaq.push(['_trackPageview', '#!/' + theSection + '/' + theLink]);
			}
			if (theSection == "work") {
				showworkclick(theLink);
			} else if (theSection == "headlines") {
				showheadlinesclick(theLink);
			} else if (theSection == "kode") {
				showkodeclick(theLink);
			} else if (theSection == "team") {
				showteamclick(theLink);
			} else if (theSection == "work-archives") {
				showworkarchivesbycat(theLink);
			} else if (theSection == "headline-archives") {
				showheadlinearchivesbycat(theLink);
			}
		} else {
			theSection = newHash.substr(2);
			if (local == false) {
				_gaq.push(['_trackPageview', '#!/' + theSection]);
			}
			if (newHash == "!/work-archives") {
				showworkarchiveslist();
			} else if (newHash == "!/headline-archives") {
				showheadlinearchiveslist();
			} else {
				var tempSection = $(".current").attr('id');
				var tempSectionDash = tempSection.indexOf("-");
				var currentSection = tempSection.substr(0,tempSectionDash);
				var theSection = newHash.substr(2);
				if (currentSection != theSection) {
					$(".current .hidethis").slideUp('200', function() {
						$(".section").removeClass("current");
						$("#" + theSection + "-section").find(".hidethis").slideToggle("400");
						$("#" + theSection + "-section").addClass("current");
					});
				} else {
					e.preventDefault();
				}
			}
		}
		return false;
		if (newHash == '' || newHash == 'undefined') {
			$("#headlines-section").find(".hidethis").delay("1000").slideToggle("400");
			$("#headlines-section").addClass("current");
		}
	});
	
	
	
	// THIS CODE IS RUN WHEN CALLING A LIGHTBOX FOR A PIECE OF WORK
	
	function showworkclick(theLink) {
		$('#contact-section').after('<div id="showwork" class="popcontent clearfix"></div>');
		$('#showwork').load(root + "/work/view/" + theLink + '/ #grabthis > *', function() {
			$(this).append('<div class="close"></div>');
			$("#showwork").lightbox_me({
				centered: true,
				destroyOnClose: true,
				overlayCSS: {background: 'transparent url(workspace/images/background-trans.png) center top'},
				onLoad: function(e) { 
					if ($.flash.available == false || isAndroid) {
						$(".workgallery").css("visibility","visible");
						$(".nextpreviousbuttons").css("display","block");
					} else {
						$('#flashreplace').flash({
							swf:root + '/workspace/swf/mkPlayer.swf',
							width: 800,
							height: 450,
							allowFullScreen: true,
							flashvars: {
								xmlSource: root + '/work/view/xml/' + theLink + '/'
							}
						});
					}
					var currentlyPlayedVideo;
					$('.workgallery').cycle();
					$(".videoplaybutton").click(function() {
						$('.workgallery').cycle('pause');
						$(this).parent('.pieceofwork').find('.overvideo').hide();
						$(this).hide();
						$(this).parent('.pieceofwork').find('.workvideo').show();
						$(this).parent('.pieceofwork').find('.workvideo').get(0).play();
						currentlyPlayedVideo = $(this).parent('.pieceofwork');
						
					});
					$(".previousbutton").click(function() {
						$('.workgallery').cycle('prev');
						$('.workgallery').cycle('pause');
						if (currentlyPlayedVideo != null) {
							currentlyPlayedVideo.find('.workvideo').get(0).pause();
							currentlyPlayedVideo.find('.workvideo').hide();
							currentlyPlayedVideo.find('.overvideo').show();
							currentlyPlayedVideo.find('.videoplaybutton').show();
						}
					});
					$(".nextbutton").click(function() {
						$('.workgallery').cycle('next');
						$('.workgallery').cycle('pause');
						if (currentlyPlayedVideo != null) {
							currentlyPlayedVideo.find('.workvideo').get(0).pause();
							currentlyPlayedVideo.find('.workvideo').hide();
							currentlyPlayedVideo.find('.overvideo').show();
							currentlyPlayedVideo.find('.videoplaybutton').show();
						}
					});
				},
				onClose: function(e) { 
					window.location.hash = "!/work";
					return false;
				}
			});
		});
		return false;
	}
	
	function showworkarchiveslist() {
		$("#featuredwork").slideUp('200', function() {
			if ($(this).is(":hidden")) {
				$("#archivedwork").load(root +"/work/ #grabarchives > *", function() {
					$("#archivedwork").slideDown("400", function() {
						$(".archivelist a").click(function() {
							window.location.hash = "!/work-archives/" + $(this).attr('id');
							return false;
						});
						$("#backtowork").click(function() {
							$("#archivedwork").slideUp('200', function() {
								$("#featuredwork").slideDown('400');
							});
							return false;
						});
						$('#archivedwork .boxmenu a').click(function(e) {
							var whichSection = $(this).parents(".section").attr('id');
							window.location.hash = "!/" + whichSection + "/" + $(this).attr('id');
							return false;
						});
					});
				});
			}
		});
	}
	
	function showworkarchivesbycat(theLink) {
		$('#archivedwork').slideUp("200", function() {
			var theKind = "";
			if (theLink == "music" || theLink == "retail" || theLink == "technology" || theLink == "healthcare" || theLink == "non-profit" || theLink == "self-promotion") {
				theKind = "industry";
			} else if (theLink == "advertising" || theLink == "collateral" || theLink == "interactive" || theLink == "branding" || theLink == "video" || theLink == "publishing") {
				theKind = "discipline";
			}
			$('#archivedwork').load(root + "/work/?" + theKind + "=" + theLink + ' #grabarchives > *', function() {
				$('#archivedwork').slideDown("400", function() {
					$(".archivelist a").click(function() {
						window.location.hash = "!/work-archives/" + $(this).attr('id');
						return false;
					});
					$("#backtowork").click(function() {
						$("#archivedwork").slideUp('200', function() {
							$("#featuredwork").slideDown('400');
						});
						return false;
					});
					$('.boxmenu a').click(function(e) {
						var tempSection = $(this).parents(".section").attr('id');
						var tempSectionDash = tempSection.indexOf("-");
						var whichSection = tempSection.substr(0,tempSectionDash);
						window.location.hash = "!/" + whichSection + "/" + $(this).attr('id');
						return false;
					});
				});
			});
		});
	}
	
	function showheadlinesclick(theLink) {
		$('#contact-section').after('<div id="showheadlines" class="popcontent clearfix"></div>');
		$('#showheadlines').load(root + "/headlines/view/" + theLink + '/ #grabthis > *', function() {
			$(this).append('<div class="close"></div>');
			$("#showheadlines").lightbox_me({
				centered: true,
				destroyOnClose: true,
				overlayCSS: {background: 'transparent url(workspace/images/background-trans.png) center top'},
				onLoad: function() {
					if ($.flash.available == false || isAndroid) {
						$(".headlinesgallery").css("visibility","visible");
					} else {
						$('#flashreplace').flash({
							swf: root + '/workspace/swf/mkPlayer.swf',
							width: 800,
							height: 450,
							allowFullScreen: true,
							flashvars: {
								xmlSource: root + '/headlines/view/xml/' + theLink + '/'
							}
						});
					}
					$(".videoplaybutton").click(function() {
						$(this).parent('.headlinesgallery').find('.overvideo').hide();
						$(this).hide();
						$(this).parent('.headlinesgallery').find('.headlinesvideo').show();
						$(this).parent('.headlinesgallery').find('.headlinesvideo').get(0).play();
					});
				},
				onClose: function() { 
					window.location.hash = "!/headlines";
					return false;
				}
			});
		});
		return false;
	}
	
	function showheadlinearchiveslist() {
		$("#featuredheadlines").slideUp('200', function() {
			$("#archivedheadlines").load(root + "/headlines/ #grabheadlinearchives > *", function() {
				$("#archivedheadlines").slideDown("400", function() {
					$(".boxmenu a").click(function() {
						var tempSection = $(this).parents(".section").attr('id');
						var tempSectionDash = tempSection.indexOf("-");
						var whichSection = tempSection.substr(0,tempSectionDash);
						window.location.hash = "!/" + whichSection + "/" + $(this).attr('id');
						return false;
					});
					$("#years a").click(function() {
						window.location.hash = "!/headline-archives/" + $(this).attr('id');
						return false;
					});
					$("#backtoheadlines").click(function() {
						$("#archivedheadlines").slideUp('200', function() {
							$("#featuredheadlines").slideDown('400');
						});
						return false;
					});
				});
			});
		});
	}
	
	function showheadlinearchivesbycat(theLink) {
		$("#archivedheadlines").slideUp("200", function() {
			$("#archivedheadlines").load(root + "/headlines/" + theLink + "/ #grabheadlinearchives > *", function() {
				$("#archivedheadlines").slideDown("400", function() {
					//$('#flashreplace').flash({
					//	swf: root + '/workspace/swf/mkPlayer.swf',
					//	width: 800,
					//	height: 450,
					//	allowFullScreen: true,
					//	flashvars: {
					//		xmlSource: root + '/headlines/view/xml/' + theLink + '/'
					//	}
					//});
					$(".boxmenu a").click(function() {
						var tempSection = $(this).parents(".section").attr('id');
						var tempSectionDash = tempSection.indexOf("-");
						var whichSection = tempSection.substr(0,tempSectionDash);
						window.location.hash = "!/" + whichSection + "/" + $(this).attr('id');
						return false;
					});
					$("#years a").click(function() {
						window.location.hash = "!/headline-archives/" + $(this).attr('id');
						return false;
					});
					$("#backtoheadlines").click(function() {
						$("#archivedheadlines").slideUp('200', function() {
							$("#featuredheadlines").slideDown('400');
						});
						return false;
					});
				});
			});
		});
	}
	
	function showkodeclick(theLink) {
		$('#contact-section').after('<div id="showkode" class="popcontent clearfix"></div>');
		$('#showkode').load(root + "/kode/" + theLink + '/ #grabthis > *', function() {
			$(this).append('<div class="close"></div>');
			$("#showkode").lightbox_me({
				centered: true,
				destroyOnClose: true,
				overlayCSS: {background: 'transparent url(workspace/images/background-trans.png) center top'},
				onLoad: function() { 
					if ($.flash.available == false || isAndroid) {
						$(".kodegallery").css("visibility","visible");
					} else {
						$('#flashreplace').flash({
							swf:root + '/workspace/swf/mkPlayer.swf',
							width: 800,
							height: 450,
							allowFullScreen: true,
							flashvars: {
								xmlSource: root + '/kode/reel/xml/'
							}
						});
					}
					$(".videoplaybutton").click(function() {
						$(this).parent('.kodegallery').find('.overvideo').hide();
						$(this).hide();
						$(this).parent('.kodegallery').find('.kodevideo').show();
						$(this).parent('.kodegallery').find('.kodevideo').get(0).play();
					});
				},
				onClose: function() { 
					window.location.hash = "!/kode";
					return false;
				}
			});
		});
		return false;
	}
	
	function showteamclick(theLink) {
		$('#contact-section').after('<div id="showteam" class="popcontent clearfix"></div>');
		$('#showteam').load(root + "/team/" + theLink + '/ #grabthis > *', function() {
			$(this).append('<div class="close"></div>');
			$("#showteam").lightbox_me({
				centered: true,
				destroyOnClose: true,
				overlayCSS: {background: 'transparent url(workspace/images/background-trans.png) center top'},
				onLoad: function() {
					if ($.flash.available == false || isAndroid) {
						$(".teamgallery").css("visibility","visible");
					} else {
						var teamVideoHeight = $(".teamgallery").height();
						$('#flashreplace').flash({
							swf: root + '/workspace/swf/mkPlayer.swf',
							width: 800,
							height: teamVideoHeight,
							allowFullScreen: true,
							flashvars: {
								xmlSource: root + '/team/xml/' + theLink + '/'
							}
						});
					}
					$(".videoplaybutton").click(function() {
						$(this).parent('.teamgallery').find('.overvideo').hide();
						$(this).hide();
						$(this).parent('.teamgallery').find('.teamvideo').show();
						$(this).parent('.teamgallery').find('.teamvideo').get(0).play();
					});
				},
				onClose: function() { 
					window.location.hash = "!/team";
					return false;
				}
			});
		});
		return false;
	}
					
	
	
	
	$("#contact-section a.textbutton").click(function() {
		var somethingvalue = $("#thesomething").val();
		if (somethingvalue == '') {
			var postUrl = $(this).parents("form").attr("action");
			var dataString = $(this).parents("form").serialize();
			var successful = '';
			$.ajax({
				type: "POST",
				url: postUrl,
				data: dataString,
				success: function(html) {
					$('#contact-section').after(html);
					$("#contactresult").lightbox_me({
						centered: true,
						destroyOnClose: true,
						overlayCSS: {background: 'transparent url(workspace/images/background-trans.png) center top'},
						onLoad: function() { 
							$("#contactresult").append('<div class="close"></div>');
							successful = $('#contactresult').hasClass('success');
						},
						onClose: function() {
							if (successful == true) {
								$("#contact-section").find("#yourname").val('Your Name');
								$("#contact-section").find("#youremail").val('Your Email');
								$("#contact-section").find("textarea").text('What would you like to say?');
							}
						}
					});
				}
			});
		}
		return false;
	});
	
	
	if ($(".section").hasClass("current")) {
		//do nothing
	} else {
		$("#headlines-section").find(".hidethis").delay("1000").slideToggle("400");
		$("#headlines-section").addClass("current");
	}
		
});


