﻿var search = document.location.search.replace(/^\?/, "");
var pairs = search.split("&");
var params = [];
for (var i = 0; i < pairs.length; i++) {
	var pair = pairs[i].split("=");
	params[unescape(pair[0]).toLowerCase()] = (pair.length > 1) ? unescape(pair[1]) : "";
}

$(document).ready(function(){
	
	// start accordian menu 
	
	$("#mainNav li ul").css({display:"none"}) // set all submenu display to none
	
	var phot = window.location.pathname.indexOf('/photos/')!==-1,
		retouch = window.location.pathname.indexOf('/retoucher/')!==-1 || params.t === 'retoucher',
		video = window.location.pathname.indexOf('/video_production/')!==-1 || params.t === 'videographer',
		cat = window.location.pathname.match(/\//g).length > 2;
		
	if( !video && (params['pid'] || params['p'] || (phot && !cat)) && !params['cat'])
		$("#photography .photography_list ul").css({display:"block"}); // menus intially to open
	$("#styling .styling_list ul").css({display:"block"}); // menus intially to open
	if(retouch){
		$(".retoucher_list ul").css({display:"block"}); // menus intially to open
	}
	if(video){
		$(".videographer_list ul").css({display:"block"}); // menus intially to open
	}
	if(params['cat'] || (phot && cat)) {
		$("#category_ul").css({display:"block"}); // menus intially to open
		$("#category_ul .selected ul").css({display:"block"}); // menus intially to open
	}
	//$("#category_ul li ul").css({display:"none"}) // set all submenu display to none
		
	$("#mainNav li a").each(function(){
		
		var parentli = $(this).parent("li");
		var myList = parentli.children("ul");
		
		// if no sub ul iterate
		if(myList.length == 0) 
			return;
		
		// attach event to the link
		$(this).click(function(e){
			var parentList = $(this).parent('li');
			var ulMenu = parentList.children('ul');
			// show submenu
			ulMenu.animate({
					opacity:"toggle",
					height:"toggle"
			}, "slow");
			
			// close any currently opened menus 
			var myNeighbors = parentList.siblings("li").children("ul");
			
			
			for(var i=0; i<myNeighbors.length; i++) {
				if(myNeighbors[i].style.display!="none") {
					$(myNeighbors[i]).animate({
						opacity:"toggle",
						height:"toggle"
					}, "fast");
				}
			}
		
			return false;
		
		});
	});
	
	//end accordian menu
	
	// start scrolling content box

	
		// add and remove classes and wrap in a window div to allow for scrollable content
		$(".scrolling")
		.removeClass("scrolling")
		.addClass("jq_scroller")
		.wrap('<div class="jq_window"></div>');
	
		if($(".jq_scroller").height() > 389) { // if area not taller than containing box don't add scroll capability
		
			// add control buttons
			$(".jq_window").append('<ul id="controls">\
				<li id="up_scroller"><a href="#"><img src="core/scrollUp.gif" alt="scroll up button"></a></li>\
				<li id="down_scroller"><a href="#"><img src="core/scrollDown.gif" alt="scroll down button"></a></li>\
				</ul>');

			// attach events and handle the scrolling
			$(".jq_scroller").css("top", "0px"); // set Top to defeat IE bug
			var scrollHeight = $(".jq_scroller").height()+101,
				windowHeight = $(".jq_window").height(),
				endMargin = -(scrollHeight - windowHeight),
				moveAmt = 100,
				moving = false,
				innerTop,
				toMove;
			$("#up_scroller").click(function(){
				innerTop = parseInt($(".jq_scroller").css("top"));
				toMove = 0;
				$(".jq_scroller").animate({top:toMove},'slow')
			});
			
			$("#down_scroller").click(function(){
				if(moving) return;
				moving = true;
				var toMove;

				innerTop = parseInt($(".jq_scroller").css("top"));
				if(innerTop - moveAmt > endMargin) {
					toMove = -moveAmt + innerTop;
				} else {
					toMove = endMargin - innerTop;
				}
				if (innerTop < endMargin) {
					moving = false;
					return false;
				}
				$(".jq_scroller").animate({top:toMove},'slow', function(){
					moving = false;
				});
				return false;
			});
	
	}
	// end scrolling content box

	// NOTES 
	// include function to leave menu open if on main page or sub page of photography?
	// set cookie to show open menus ?
	
	function hideLogo() {
       // hide element with id=logo
        $('#logo').hide();
    }
   
    function showLogo(){
       // Fade In element with id=logo
        $('#logo').fadeIn('slow');
    }
});


function isFilled(field) {
	if (field.value.length < 1 || field.value == field.defaultValue) {
		return false;
	} else {
		return true;
	}
}

function isEmail(field) {
	if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
		return false;
	} else {
		return true;
	}
}

function validateForm(whichform) {
	for (var i=0; i<whichform.elements.length; i++) {
	var element = whichform.elements[i];
	if (element.className.indexOf("required") != -1) {
		if (!isFilled(element)) {
			alert("Please fill in the "+element.name+" field.");
			return false;
		}
	}
	if (element.className.indexOf("email") != -1) {
		if (!isEmail(element)) {
			alert ("The "+element.name+" field must be a valid email address.");
			return false;
		}
	}
}
return true;
}

function prepareForms() {
	for (var i=0; i<document.forms.length; i++) {
		var thisForm = document.forms[i];
		thisform.onsubmit = function() {
			return validateForm(this);
		}
	}
}

