/*******************************************************************
 * Navigation object
 *******************************************************************/

/*  returns x coordinate of any element */
function findPosX(obj){
    var curleft = 0;
    if (obj.offsetParent){
        while (obj.offsetParent){
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

/* returns y coordinate of any element*/
function findPosY(obj){
    var curtop = 0;
    if (obj.offsetParent){
        while (obj.offsetParent){
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

var navigation = {
	col:[],
	init: function(){
		var self = navigation;
		var s;
		self.preload();
		initQuote();
		self.col = document.getElementById('navigation').getElementsByTagName('a');
		duff(self.col, function(e){
			addEvent(e,'mouseover',function(){
				obj = this.firstChild;
				obj.src = obj.src.replace("-up.gif","-over.gif");
			});
			addEvent(e,'mouseout',function(){
				obj = this.firstChild;
				obj.src = obj.src.replace("-over.gif","-up.gif");
			});
		});
		
		// sub-nav init
		var navAbout = document.getElementById('nav-about').firstChild;
		var navAboutSub = document.getElementById('nav-about-sub');
                function positionSubNav2(){
                        navAboutSub.style.top = findPosY(navAbout) + 22 + 'px';
                        navAboutSub.style.left = findPosX(navAbout) - 1 + 'px';
                }
                positionSubNav2();
		addEvent(navAbout,'mouseover',function(){
			navAboutSub.style.display = "block";
		});
		addEvent(navAboutSub,'mouseover',function(){
			navAboutSub.style.display = "block";
			navAbout.src = navAbout.src.replace("-up.gif","-over.gif");
		});
		addEvent(navAboutSub,'mouseout',function(){
			navAboutSub.style.display = "none";
			navAbout.src = navAbout.src.replace("-over.gif","-up.gif");
		});
		addEvent(navAbout,'mouseout',function(){
			navAboutSub.style.display = "none";
		});

		if (document.getElementById("Nate")) {
		  	var Tammy = new toggleLayer(document.getElementById("Tammy"));
			addEvent(document.getElementById("Tammylink"),"click",Tammy.exec);
			var Eric = new toggleLayer(document.getElementById("Eric"));
			addEvent(document.getElementById("Ericlink"),"click",Eric.exec);
				var Dianna = new toggleLayer(document.getElementById("Dianna"));
			addEvent(document.getElementById("Diannalink"),"click",Dianna.exec);
				var Andrea = new toggleLayer(document.getElementById("Andrea"));
			addEvent(document.getElementById("Andrealink"),"click",Andrea.exec);
		}

		if (document.getElementById("wedding")) {
			var wedding = new toggleLayer(document.getElementById("wedding"));
			addEvent(document.getElementById("weddinglink"),"click",wedding.exec);
			var engagement = new toggleLayer(document.getElementById("engagement"));
			addEvent(document.getElementById("engagementlink"),"click",engagement.exec);
			var boudoir = new toggleLayer(document.getElementById("boudoir"));
			addEvent(document.getElementById("boudoirlink"),"click",boudoir.exec);
			var kids = new toggleLayer(document.getElementById("kids"));
			addEvent(document.getElementById("kidslink"),"click",kids.exec);
		}
		resizeIfOdd();

	},
	preload: function(){
		if (document.images){
			pic0= new Image(104,21);
			pic0.src="images/nav-galleries-over.gif";
			pic1= new Image(81,21);
			pic1.src="images/nav-about-over.gif";
			pic2= new Image(72,21);
			pic2.src="images/nav-blog-over.gif";
			pic5= new Image(97,21);
			pic5.src="images/nav-contact-over.gif";
		}	
	}
}
function positionSubNav(){
	var navAbout = document.getElementById('nav-about').firstChild;
	var navAboutSub = document.getElementById('nav-about-sub');
	navAboutSub.style.top = findPosY(navAbout) + 22 + 'px';
	navAboutSub.style.left = findPosX(navAbout) - 1 + 'px';
}

function toggleLayer(obj){
	var o = obj;
	this.exec = function(){
		this.c = (this.c > 0) ? this.c : 0;
		this.c++;
		o.style.display = (this.c % 2 == 1) ? "block" : "none";	
	}
}

addEvent(window,"load",navigation.init)
addEvent(window,"mousemove",positionSubNav)
addEvent(window,"resize",forceSafariEven)


//addEvent(document,"readystatechange",navigation.init)

   
function initQuote(){
	var kudosQuoteField = document.getElementById('js-quote');
	var kudosAuthorField = document.getElementById('js-author');
	var kudosImageField = document.getElementById('js-img');
	
	var kudosImage_arr = [
		'<img src="images/kudo-nicdan.jpg" alt="Nicolle & Danny" />',
		'<img src="images/kudo-rebale.jpg" alt="Rebecca & Alex" />',
		'<img src="images/kudo-nivik.jpg" alt="Niyati & Vikram" />',
		'<img src="images/kudo-marbro.jpg" alt="Marissa & Brook" />',
		'<img src="images/kudo-jenphil.jpg" alt="Jenna & Phil" />',
		'<img src="images/kudo-alymic.jpg" alt="Alyson & Michael" />',
		'<img src="images/kudo-ambsha.jpg" alt="Amber & Shane" />',
		'<img src="images/kudo-jonvin.jpg" alt="Jonalyn & Vince" />',
		'<img src="images/kudo-meljef.jpg" alt="Melissa & Jeffrey" />',
	];
    
	var kudosQuote_arr = [
        'Working with B&G has been a wonderful experience. From our first meeting Jen and Nate have been relaxed, open and available. The day of our wedding Jen and Nate were calm and unobtrusive. They caught the candid moments we wanted and captured the emotions of the day. The photos show the day&#039;s sweetness, goofiness, and surprise.',
        'Our experience with B&G was absolutely wonderful! We&#039;ve told several people that it was like having our friends photograph our wedding. Several people commented on how unobtrusive you were during the ceremony. I didn&#039;t even notice you until I was walking out of the church at the end.  It was so much fun working with Nate and Jen. It was wonderful to have one thing I knew would go perfectly on the wedding day! Thank you Nate and Jen!',
        'Feeling the comfort in your photographers is key and we know for a fact that Jen and Nate truly made us feel comfortable. They calmed us down and always brought a smile to our faces and that is what I see in their photography. We really could just relax and kick back with them while they were snapping away. And, to top that, their photographic images, are just flawless, fun and amazing. Also, the rest of the guests fall in love with them too. They become like family and get to know everyone.', 
		'We loved working with Nate & Jen.  As an engaged couple, it was wonderful to work with a young newly married couple.  Their energy is teriffic, totally upbeat and enthusiastic but also laid back and very flexible.  It&#039;s wonderful to work with photographers who really listen!!  You guys are great!  Thanks for everything!!',
		'We looked at a number of portfolios online and Nate and Jen&#039;s pictures were by far the best we saw.  Jen was always very prompt with getting back to me regarding questions and seemed genuinely excited about our wedding during the months prior. During the wedding, they were incredibly professional. We didn&#039;t even know they were there. For two people who hate getting their picture taken, they made it an extremely easy and comfortable process. I was surprised at how quickly we were able to view all the photos and at how quickly our proofs arrived. We were so happy! Outstanding! A+++++++',
        'We LOVED Jen and Nate! If you have a special occasion that you need captured, they&#039;re a must. They&#039;re flexible, easy to work with and cool. Not to mention that their photography came out BEAUTIFULLY.',
		'You were the first photographers I met with and I knew I wanted you. Enthusiasm, style, your personalities. Everything was right. As a designer, I felt your pricing was definitely fair, and photography for me was probably the biggest love for my wedding day.  Both of you have great personalities, you work well together, you have enthusiasm, you try different things. You completely blend in at the wedding and seem to be everywhere and invisible at the same time. You were completely flexible and were so great with your customer service. Our wedding was small and personal to us and it felt like you were one of our invited guests, not someone we hired.  We loved working with you guys and want to stay in touch, because you are both just so fun to be around.',
		'We cannot get over the number of compliments we continue to receive about how amazing our wedding photos turned out.  The advantage of working with Jen and Nate is that you have two photographers on-site who are extremely professional, creative and helpful, and who can produce results that are similar, if not better, than some of the most expensive photographers in the Los Angeles area.\nThe initial pictures posted on B&G&#039;s blog the next day were a big hit with friends and family, and we couldn&#039;t believe that within two weeks, all of the pictures were posted online.  We were able to spend a rainy day on our honeymoon reliving the great moments from our wedding.',
		'Jen and Nate are AMAZING! We could not have asked for two better people to shoot our wedding and are incredibly happy with their work product. We have already hired them to shoot other parties for us and would recommend them to anyone, anytime, anywhere.'
	];
    var kudosAuthor_arr = [
		'<strong>Nicolle & Danny<br />Los Angeles</strong>',
		'<strong>Rebecca & Alex<br />Redondo Beach</strong>',
		'<strong>Niyati & Vikram<br />Irvine</strong>',
		'<strong>Marissa & Brook<br />Phoenix</strong>',
		'<strong>Jenna & Phil<br />Los Angeles</strong>',
		'<strong>Alyson & Michael<br />Santa Monica</strong>',
		'<strong>Amber & Shane<br />Westminster</strong>',
		'<strong>Jonalyn & Vince<br />Los Angeles</strong>',
		'<strong>Melissa & Jeffrey<br />Los Angeles</strong>',
	];
	
	var pressImageField = document.getElementById('press-img');
	var pressTitleField = document.getElementById('press-title');
	
	var pressImage_arr = [
		'<img src="images/sidebar-press-bridalworld-fall01.jpg" alt="Bridal World" />',
	];
	
	var pressTitle_arr = [
		'<strong>Bride World</strong><br />Fall 2001',
	];

	//var kudosLength = 4;
	var kudosRandom = Math.floor( ( kudosImage_arr.length ) * Math.random() );
	if(kudosQuoteField && kudosAuthorField && kudosImageField){
        kudosImageField.innerHTML = kudosImage_arr[kudosRandom];
        kudosQuoteField.innerHTML = kudosQuote_arr[kudosRandom];
        kudosAuthorField.innerHTML = kudosAuthor_arr[kudosRandom];
	};     
	
	var pressRandom = Math.floor( ( pressImage_arr.length ) * Math.random() );
	if(pressImageField && pressTitleField){
		pressImageField.innerHTML = pressImage_arr[pressRandom];
		pressTitleField.innerHTML = pressTitle_arr[pressRandom];
	};
};

function resizeIfOdd(){
	if(document.documentElement.clientWidth % 2 == 0)
	   window.resizeBy(-1,0); 
};

function forceSafariEven(){ 
	window.setTimeout('resizeIfOdd()',5);
};               

/*******************************************************************
 * Utility functions to speed iterations and general event
 * handling functions.  Nothing beneath this point should be touched.
 * 
 * New methods:
 * addEvent(obj,event,func);
 * removeEvent(obj,event,func);
 *
 * example: addEvent(document,"readystatechange",function);
 * example: removeEvent(document,"readystatechange",function);
 *******************************************************************/

function isPresent(param) {
	return typeof param != "undefined" && param != null;
}

function duff(objectArray, method) {
	var arrayLength = objectArray.length;
	var loopLength = arrayLength % 8;
	var index = 0;
	while (loopLength--) {
		method(objectArray[index++]);
	}
	loopLength = parseInt(arrayLength / 8);
	while (loopLength--) {
		method(objectArray[index++]);
		method(objectArray[index++]);
		method(objectArray[index++]);
		method(objectArray[index++]);
		method(objectArray[index++]);
		method(objectArray[index++]);
		method(objectArray[index++]);
		method(objectArray[index++]);
	}
}


function addEvent(element, type, handler) {
	if (isPresent(element.type) && element.type.indexOf("select-") == 0 ) {
		element["on" + type] = handler;
		return;
	}
	if (!handler.$$guid) handler.$$guid = addEvent.guid++;
	if ( !isPresent(element.events) ) element.events = new Object();
	var handlers = element.events[type];
	if (!handlers) {
		handlers = element.events[type] = new Object();
		handlers.order = new Array();
		if (element["on" + type] && (element != document || type != "readystatechange") ) {
			handlers[0] = element["on" + type];
			handlers.order[0] = 0;
		}
	}
	handlers[handler.$$guid] = handler;
	handlers.order[handlers.order.length] = handler.$$guid;
	if (element != document || type != "readystatechange") {
		element["on" + type] = handleEvent;
	}
}
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.events && element.events[type]) {
		var handlers = element.events[type];
		var id = handler.$$guid;
		if (delete handlers[handler.$$guid]) {
			for (var i = 0; i < handlers.order.length; i++) {
				if (handlers.order[i] == handler.$$guid) {
					var newOrder = handlers.order.slice(0, i);
					if (i + 1 != handlers.order.length) {
						newOrder = newOrder.concat( handlers.order.slice(i + 1) );
					}
					handlers.order = newOrder;
				}
			}
		}
	}
}

function handleEvent(event) {
	var returnValue = true;
	event = isPresent(event) ? event : isPresent(document.getElementById) ? fixEvent(window.event) : window.event;
	var handlers = this.events[event.type];
	var order = handlers.order;
	for (var i = 0; i < order.length; i++) {
		this.$$handleEvent = handlers[ order[i] ];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
}

function endOfPageScriptExists() {
	var scripts = document.getElementsByTagName("SCRIPT");
	if (scripts.length == 0) {
		return false;
	}
	var lastScript = scripts.item(scripts.length - 1);
	if (lastScript.childNodes.length > 0) {
		for (var i = 0; i < lastScript.childNodes.length; i++) {
			var currentNode = lastScript.childNodes.item(i);
			if (currentNode.nodeType == 3 && currentNode.nodeValue.indexOf("handleReadyStateChange()") != -1) {
				return true;
			}
		}
	}
	else {
		return lastScript.innerHTML.indexOf("handleReadyStateChange()") != -1;
	}
	return false;
}

window.intervalId = 0;

function handleReadyStateChange() {
	if (window.intervalId != 0) {
		if ( endOfPageScriptExists() ) {
			clearInterval(window.intervalId);
			window.intervalId = 0;
		}
		else {
			return;
		}
	}
	var event = new Object();
	event.readyState = "complete";
	event.srcElement = document;
	event.target = document;
	event.currentTarget = document;
	event.type = "readystatechange";
	if ( !isPresent(document.events) ) {
		document.Events = new Object();
		var dummyEvents = new Object();
		dummyEvents.readystatechange = dummyHandlers;
		document.events = dummyEvents;
	}
	if ( !isPresent(document.events.readystatechange) ) {
		var dummyHandlers = new Object();
		dummyHandlers.order = new Array();
		document.events.readystatechange = dummyHandlers;
	}
	document.$$handleEvent = handleEvent;
	document.$$handleEvent(event);
}

function fixEvent(event) {
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
}

fixEvent.preventDefault = function() {
	this.returnValue = false;
}

fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
}
