/* FW100R4 */
/* (c)2010 SugarHill Works LLC - http://www.sugarhillworks.com */
/**
 * A simple querystring parser.
 * Example usage: var q = $.parseQuery(); q.fooreturns  "bar" if query contains "?foo=bar"; multiple values are added to an array. 
 * Values are unescaped by default and plus signs replaced with spaces, or an alternate processing function can be passed in the params object .
 * http://actingthemaggot.com/jquery
 *
 * Copyright (c) 2008 Michael Manning (http://actingthemaggot.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 **/
					var linkchkcount = 0;

$.parseQuery = function(qs,options) {
	var q = (typeof qs === 'string'?qs:window.location.search), o = {'f':function(v){return unescape(v).replace(/\+/g,' ');}}, options = (typeof qs === 'object' && typeof options === 'undefined')?qs:options, o = $.extend({}, o, options), params = {};
	$.each(q.match(/^\??(.*)$/)[1].split('&'),function(i,p){
		p = p.split('=');
		p[1] = o.f(p[1]);
		params[p[0]] = params[p[0]]?((params[p[0]] instanceof Array)?(params[p[0]].push(p[1]),params[p[0]]):[params[p[0]],p[1]]):p[1];
	});
	return params;
}
var q = $.parseQuery();


// -- BEG SITE SETTINGS ----------------------------------------------------------------------------------------
var autoforwardTo, autoforwardWait, default_scroll_settings={};
$(function($){
  $.fn.toggleFade = function(settings)
  {
  	settings = jQuery.extend(
  		{
        speedIn: "normal",
        speedOut: settings.speedIn
  		}, settings
  	);
  	return this.each(function()
  	{
  	  var isHidden = jQuery(this).is(":hidden");
      jQuery(this)[ isHidden ? "fadeIn" : "fadeOut" ]( isHidden ? settings.speedIn : settings.speedOut);
    });
  };
		   
	// STICKYFOOTER FLEX HEIGHT LAYOUT FIX	   
	//alert($('#fullscr_spcr_btm').offset().top);
		   
	// SPASH PAGE
	if (autoforwardTo && autoforwardWait) {
		window.setTimeout(function(){
			$('#ceibw, #ceibw > *').animate({width:'0px',height:'0px', top:'50%'}, 750, function(){
																						$('#ceibw, #ceibw > *').hide();
																						if (autoforwardTo.match(/http:\/\//)) {
																							window.location = autoforwardTo;
																						} else {
																							window.location = rootPath+autoforwardTo;
																						}
																					 });
			}, autoforwardWait);
	}
	
	
	
	// SETTINGS FOR SITE-WIDE NAV MENU(S)
	if ($('#menu_cntr_cntr').length > 0) {
		$('#menu_cntr').accordion({
			header: 'h3[class!="acc_txthdr"][class!="acc_spcr"]',
			animated: 'easeOutCirc',
			autoHeight: false,
			collapsible: true,
			active: false,
			changestart: function(event, ui) {
				if (ui.newHeader.children('a').eq(0).length > 0) {
					if (parseInt(ui.newHeader.children('a').eq(0).css('border-bottom-width')) > 0) {
						ui.newHeader.children('a').eq(0).css({cursor:'n-resize'});
					} else {
						if (ui.newHeader.children('a').eq(0).attr('href') !== '#') {
							ui.newHeader.children('a').eq(0).css({cursor:'pointer'});
						} else {
							ui.newHeader.children('a').eq(0).css({cursor:'n-resize'});
						}
					}
				}
				if (ui.oldHeader.children('a').eq(0).length > 0) {
					if (parseInt(ui.oldHeader.children('a').eq(0).css('border-bottom-width')) > 0) {
						ui.oldHeader.children('a').eq(0).css({cursor:'s-resize'});
					} else {
						if (ui.oldHeader.children('a').eq(0).attr('href') !== '#') {
							ui.oldHeader.children('a').eq(0).css({cursor:'pointer'});
						} else {
							ui.oldHeader.children('a').eq(0).css({cursor:'s-resize'});
						}
					}
				}
			},
			navigation: true,
			navigationFilter: function (i) {
				var url = window.location.toString().replace(rootPath,'');
				url = url.replace(/index\.php/,'');
				var href = $(this).attr('href');
				var rel = $(this).attr('rel');
				var filter = /^[^mailto]|galleries/;
				var hrefsplit = href.split('#');
				var urlsplit = url.split('#');
				if (href == '#') { 
					$(this).mouseover(
						function(){ 
							if ($(this).parent().hasClass('ui-state-default')) {
								$(this).css({cursor:'s-resize'});
							}
							if ($(this).parent().hasClass('ui-state-active')) {
								$(this).css({cursor:'n-resize'});
							}
						}
					);
				}
				
				// fix RELATIVE links, leaving offsite (http://...) alone.
				if ($(this).attr('href').match(/(^[^http|mailto|ftp\:\/\/])/)) {
					if ($(this).attr('href') != url) {
						this.href = ($(this).attr('href') =='#') ? (window.location.toString().replace(/\#*.*$/,'')+'#') : (rootPath+href);
					}
					//if ($(this).attr('href') == url) {
						//alert($(this).attr('href')+'\n'+url);
						//$(this).attr('href', window.location.toString().replace(/\#*.*$/,'')+'#');
						//$(this).href = '#';
					//}
				}
				
				if ((href.match(filter)) && (href != '#')) {// just get gallery links
					if (hrefsplit[0] == urlsplit[0]) {// show set from current gallery
						$(this).click(function(){
							this.href = rootPath+href;
							if (url.match(/\/publications\/|\/projects\//)) {
								//$('.bookcover_cntr').fadeOut(750, function(){
									$('.gallery_info_cntr').eq(rel).html('<h4>'+$('.ui-accordion-header').eq(i).children('a').eq(0).html()+'</h4>' + $('.ui-accordion-content').eq(i).html()).fadeIn(750);
									$('.gallery_info_cntr').eq(rel).children().eq(0).addClass('first');
									$('.gallery_info_cntr').eq(rel).children('p').eq(0).addClass('first');
									$('.gallery_info_cntr:not(:eq('+rel+'))').css({display:'none'});
									$('.gallery_info_cntr').eq(rel).fadeIn(750);
									if ($('#ss_cntr').length > 0) {
										ss_reinit(rel,0);
									}
									$('.ui-accordion-header').eq(i).unbind('click');
									
								//});
							} else {
								if ($('#ss_cntr').length > 0) {
									ss_reinit(rel,0); 
								}
							}
							$('#menu_cntr > h3 > a').css({border:'none', cursor:'pointer'});
							$(this).css({'border-bottom-width':'1px', 'border-bottom-color':'#E38E39', 'border-bottom-style':'dotted', cursor:'default'});

							window.location = this.href;
						});
					} else { // follow href as link
						$(this).click(function(){
							this.href = rootPath+href;
							$('#menu_cntr').accordion('option','disabled',true);
							window.location = this.href;
						});
					}
				}

				if ($(this).attr('href').match(url) && (linkchkcount < 1)) {
					if (url) { 
						if (!url.match(/\/publications\/$|\#$/) || !url.match(/\/projects\/$|\#$/)) {
							if (!url.match(/\/publications\/$/) || !url.match(/\/projects\/$/)) {
								$(this).css({'border-bottom-width':'1px', 'border-bottom-color':'#E38E39', 'border-bottom-style':'dotted', cursor:'default'});
							}
						}
					}
					window.setTimeout(function(linkchkcount){
											   //alert(rootPath+'\n'+url);
						if (url.match(/\/publications\/|\/projects\//) || url==rootPath) {
							//$('#menu_cntr').accordion('option','active',0);
							$('.bookcover_cntr').css({display:'none'});
							ss_reinit(rel,0);
							$('.gallery_info_cntr').eq(rel).html('<h4>'+$('.ui-accordion-header').eq(i).children('a').eq(0).html()+'</h4>' + $('.ui-accordion-content').eq(i).html()).fadeIn(750);
							$('.gallery_info_cntr').eq(rel).children().eq(0).addClass('first');
							$('.gallery_info_cntr').eq(rel).children('p').eq(0).addClass('first');
						} else {
							if (window.location.toString().replace(rootPath,'') != '') {//we're not on the home page;
								if (!(url.match(/news\//))) {//DISABLE FOR EMPTY ACCORDIONS - elimataes verticle jiggle
									$('#menu_cntr').accordion('option','active',i);
								}
								//alert(url+'\n'+$('.ui-accordion-header').eq(i).children('a').eq(0).attr('href'));
								$('.ui-accordion-header').eq(i).children('a').eq(0).attr('href','#').css({cursor:'default'}).unbind('click');
								$('.ui-accordion-header').eq(i).unbind('click');
							}
							if ($('#ss_cntr').length > 0){
								if (url.match(/\/publications\/|\/projects\//)) { 
									$('.bookcover_cntr').css({display:'none'});
									ss_reinit(rel,0); 
								} else {
									ss_reinit(rel,0);
								}
							}
						}
					},10);
					linkchkcount++;
				}
			}
		});
		
	}

//	if ($("#menu_cntr_cntr2").length > 0) {
//		ddsmoothmenu.init({
//			parentid: "hdr", //the id of the PARENT of #menu_cntr_cntr (usually hdr)
//			mainmenuid: "menu_cntr", //menu DIV id
//			orientation: 'h', //Horizontal or vertical menu: Set to "h" or "v"
//			classname: 'nav_menu', //class added to menu's outer DIV
//			showArrows: false // will show arrows to indicate submenus. note the arrows load in the browser last.
//			//customtheme: ["#1c5a80", "#18374a"],
//			//contentsource: ["menu_cntr_cntr2", rootPath+"shw_menu/nav_menu2.html"] //"markup" or ["container_id", "path_to_menu_file"] // NOTE: Disable contentsource when using a php include to insert the menu html.
//		});
//	}

	// SETTINGS FOR GENERAL USE VERTICAL SCROLL PANES
	if ($('.scroll-pane').length > 0) {
			default_scroll_settings = {
			scrollbarWidth: 11, // the width of the created scrollbar in pixels (defaults to 10)
			scrollbarMargin: 0, // [int] - the margin to leave to the left of the scrollbar in pixels (defaults to 5)
			//wheelSpeed [int] - controls how fast the mouse wheel makes the content scroll in pixels (defaults to 18)
			showArrows:true, // controls whether to display arrows for the user to scroll with (defaults to false)
			arrowSize: 11, // the height of the arrow buttons if showArrows=true (calculated from CSS if not provided)
			animateTo: true, // whether to animate when calling scrollTo and scrollBy (defaults to false)
			dragMinHeight: 25, // [int] - the minimum height to allow the drag bar to be (defaults to 0)
			//dragMaxHeight: 100, // [int] - the maximum height to allow the drag bar to be (defaults to 99999!)
			//animateInterval [int] - The interval in milliseconds to update an animating scrollPane (default 100)
			//animateStep [int] - The amount to divide the remaining scroll distance by when animating (default 3)
			maintainPosition: false, //[boolean] - Whether the contents of the scroll pane maintains its position when you re-init it(so it doesn't scroll as you add more content) (default true)
			//scrollbarOnLeft [boolean] - Whether the scrollbar should appear on the left hand side of the panes content (make sure your CSS also reflects this)
			reinitialiseOnImageLoad: true // [boolean] - Whether the jScrollPane should automatically re-initialise itself when any contained images are loaded (defaults to false)
		};
		$('.scroll-pane').jScrollPane(default_scroll_settings);
	}
	
	// SET UP ROLLOVER ANIMATIONS
	$(".fadeCol").hover( 
		function() {
			if ($(this).parent().hasClass('slctd')) { return false; }
			else { $(this).stop(true, true).animate({color:"#D65D06"}, 750); }
		}, 
		function() {
			if ($(this).parent().hasClass('slctd')) { return false; }
			else { $(this).stop(true, true).animate({color:"#E38E39"}, 750); }
		}
	);
	$(".fadeHdrCol").hover( 
		function() {
			$(this).stop(true, true).animate({color:"#E38E39"}, 750);
		}, 
		function() {
			$(this).stop(true, true).animate({color:"#AAA"}, 750);
		}
	);
	$(".fadeFtrCol").hover( 
		function() { 
			$(this).stop(true, true).animate({color:"#E98F49"}, 750);
			//$(this).children("div").stop(true,true).fadeTo(750, 0.75);
		}, 
		function(){
			$(this).stop(true, true).animate({color:"#EABE9D"}, 750)
			//$(this).children("div").stop(true,true).fadeTo(750, 0);
		}
	);
	
	// FIX HREFS & SRCS IN DYNAMICALLY LOADED CONTENT
//	if ($('#copy').length > 0) {
//		$('#copy').find('a').each(function(){
//			alert( 'href: '+$(this).attr('href') +'\n'+ 'rel: '+$(this).attr('rel'));
//		});
//	}
	if ($('.bookcover_cntr').length > 0) {
		$('.bookcover_cntr').each(function(){
			$(this).find('img').each(function(){ 
				$(this).attr('src', rootPath+$(this).attr('src'));
			});
			$(this).find('a').each(function(){
				if($(this).attr('href')) { 
					$(this).attr('href',rootPath+$(this).attr('href'));
					if ($('#ss_cntr').length > 0) {
						$(this).click(function(){
							$('.bookcover_cntr').fadeOut(500);
							ss_reinit($(this).attr('rel'),0);
						});
					}
				}
			});
		});
	}

	// SHOW MORE TEXT SETUP
	if ($('.more_btn').length > 0) {
		//alert($('.more_btn').length);
		$('.more_btn').each(function(i){
			$(this).click(function(){
				if ($('.more_cntr').eq(i).is(':hidden')) {
					$('.more_btn').eq(i).css({display:'none'});
					$('.more_cntr').eq(i).slideDown(500,'easeInOutQuart',function(){ $('.less_btn').eq(i).css({display:'block'}); });
				}// else {
				//	$('.more_btn').eq(i).css({display:'block'});
				//	$('.more_cntr').eq(i).slideUp(1000,'easeInOutQuart');
				//}
			});
		});
		$('.less_btn').each(function(i){
			$(this).click(function(){
				if ($('.more_cntr').eq(i).is(':visible')) {
					$('.less_btn').eq(i).css({display:'none'});
					$('.more_cntr').eq(i).slideUp(500,'easeInOutQuart',function(){ $('.more_btn').eq(i).css({display:'block'}); });
				}
			});
		});
	}

	// SET UP VERTICAL POSITIONING ON INFO PAGES
	// Call this function to vertically center a content container in its parent. Takes jq selectors as arguments.
	function vCenter(content_cntr, parent_cntr, topOff) {
		//if ($(parent_cntr).height() === $(content_cntr).height()) {
			if (!topOff) { topOff = 0; }
			var topPos = $(parent_cntr+' > div:first').css({paddingTop:'0', marginTop:'0'}).offset().top;
			var btmPos = $(parent_cntr+' > div:last').css({paddingBottom:'0', marginBottom:'0'}).offset().top + $(parent_cntr+' > div:last').height();
			var content_h = btmPos - topPos;
			if (content_h < 1) {
				//alert(parseInt($(content_cntr).css('height')));
				//fall back to CSS if content h comes back 0
				content_h = parseInt($(content_cntr).css('height'));
			}
			var newTopPos = Math.floor((($(parent_cntr).height() / 2) - (content_h / 2)));
			//newTopPos = newTopPos - 30; //OFFSET ADJUSTMENT LINE - use any arbitrary math.
			$(content_cntr).css({top:(newTopPos+topOff)+'px'}); // use top: or paddingTop: depending on content 'push' needs, css, etc.
		//}
	}
	if ($("#main_txt_scr_cntr").length > 0) {
			//setTimeout(function(){vCenter('#v_center_wrap', '#main_txt_scr_cntr');},5000);
			vCenter('#v_center_wrap', '#main_txt_scr_cntr')
	}
	
	
	// INIT SLIDESHOW
	if ($('#ss_cntr').length > 0) {
		//NOTE: DISABLED HERE BECAUSE THE MENU ACCORDION RE-INITS THE SS BASED ON THE URL
		//ss_init();
	}
	if ($('#auto_ss_cntr').length > 0) {
		ss_init();
	}
	
	
	
	// INIT VIDEO
	if ($('#vid_cntr').length > 0) {
											jwplayer("vid_plyr").setup({
												flashplayer: "../shw_lib/mediaplayer5/player.swf",
												file: rootPath+"events/img/video1.mp4",
												height: 240,
												width: 320,
												events: {
													onComplete: function(event) { olayHide(); }
												},
												autostart: true,
												controlbar: 'none'
												
											});
	}
	
	$('#overlay_bg').click(function(){olayHide();});

	// SET UP PUCHASE DIV
//	if ($('#purch_cntr_bg').length > 0) {		
//		$(function() {
//
//			function togglePurchDiv () {
////				IE FIX: USE FIX IF HAVING UNSOLVABLE CSS/POSITIONING PROBLEMS AT BEG/END OF SLIDE ANIMATIONS
//		//		if (isitIE === false) {
//					$("#purch_cntr_bg,#purch_cntr").toggle("slide", {direction: "right"}, 1000);
//		//		} else { 
//		//			$("#purch_cntr_bg").css({filter:'alpha(opacity=55'});
//		//			$("#purch_cntr_bg,#purch_cntr").toggle(500);
////					USE THE FOLLOWING TO HIDE/SHOW MANUALLY (NOT TOGGLE)
////					if($("#purch_cntr").position().top <= 0){ // hidden, now show...
////						$("#purch_cntr_bg").toggle("slide", {direction: "left"}, 1000, function(){
////							$("#purch_cntr").show(100);
////						});
////					} else { // showing, now hide...
////						$("#purch_cntr").hide(100, function(){
////							$("#purch_cntr_bg").toggle("slide", {direction: "left"}, 1000);
////						});
////					}
//		//		}
//			}
//			
//			
//			$('#qty_cntr').hover(
//				function(){
//					if ($(this).children('#qty_bg').hasClass('fcsd') === false) {
//						$(this).children('input').stop(true, true).animate({color:'#000'},750);
//						$(this).children('#qty_bg').stop(true, true).fadeTo(750, 1);
//					}
//				},
//				function(){
//					if ($(this).children('#qty_bg').hasClass('fcsd') === false) {
//						$(this).children('input').stop(true, true).animate({color:'#D4D4D4'},750);
//						$(this).children('#qty_bg').stop(true, true).fadeTo(750, 0);
//					}
//				}
//			);
//			
//			$("#quantity").focus(function(){
//					$(this).stop(true, true).animate({color:'#000'},750);
//					$('#qty_bg').stop(true, true).fadeTo(750, 1).addClass('fcsd');
//			});
//			$("#quantity").blur(function(){
//					$(this).stop(true, true).animate({color:'#D4D4D4'},750);
//					$('#qty_bg').stop(true, true).fadeTo(750, 0).removeClass('fcsd');
//			});
//			
////			$('#opt0_cntr > p').each(function(){
////				$(this).click(function(){
////					$('.label').css({background:'none'});
////					var i = $('#opt0_cntr > p').index(this);
////					$('#opt0_cntr > p:eq('+i+')').removeClass().addClass('slctd');
////					$('#opt0_cntr > p:not(:eq('+i+'))').removeClass().addClass('deslctd');
////					$('#opt0_cntr > p > .optbg').each(function(){
////						if ($(this).parent().hasClass('slctd')) {
////							$('#os0 > option:eq('+i+')').attr('selected','selected');
////							$('#os0_option_select'+i).val(slideshow[current].option_list_0[i]);
////							$('#os0_option_amount'+i).val(slideshow[current].option_amts_0[i]);
////							$(this).stop(true, true).fadeTo(750, 1);
////						}
////						if ($(this).parent().hasClass('deslctd')) {
////							$(this).stop(true, true).fadeTo(750, 0);
////						}
////					});
////					$('#opt0_cntr > p > a').each(function(){
////						if ($(this).parent().hasClass('slctd')) {
////							$(this).stop(true, true).animate({color:"#000"}, 500);
////						}
////						else if ($(this).parent().hasClass('deslctd')) {
////							$(this).stop(true, true).animate({color:"#D4D4D4"}, 500);
////						}
////					});
////				});
////			}).hover(
////				function() {
////					$(this).children('a').stop(true, true).animate({color:"#000"}, 500);
////					$(this).children('.optbg').stop(true, true).fadeTo(750, 1);
////				}, 
////				function() {
////					$(this).children('a').stop(true, true).animate({color:"#D4D4D4"}, 500);
////					$(this).children('.optbg').stop(true, true).fadeTo(750, 0);
////				}
////			);
//			
////			$('#opt1_cntr > p').each(function(){
////				$(this).click(function(){
////									   $('.label').css({background:'none'});
////					var i = $('#opt1_cntr > p').index(this);
////					$('#opt1_cntr > p:eq('+i+')').removeClass().addClass('slctd');
////					$('#opt1_cntr > p:not(:eq('+i+'))').removeClass().addClass('deslctd');
////					$('#opt1_cntr > p > .optbg').each(function(){
////						if ($(this).parent().hasClass('slctd')) {
////							$('#os1 > option:eq('+i+')').attr('selected','selected');
////							$(this).stop(true, true).fadeTo(750, 1);
////						}
////						if ($(this).parent().hasClass('deslctd')) {
////							$(this).stop(true, true).fadeTo(750, 0);
////						}
////					});
////					$('#opt1_cntr > p > a').each(function(){
////						if ($(this).parent().hasClass('slctd')) {
////							$(this).stop(true, true).animate({color:"#000"}, 500);
////						}
////						else if ($(this).parent().hasClass('deslctd')) {
////							$(this).stop(true, true).animate({color:"#D4D4D4"}, 500);
////						}
////					});
////				});
////			}).hover(
////				function() {
////					$(this).children('a').stop(true, true).animate({color:"#000"}, 500);
////					$(this).children('.optbg').stop(true, true).fadeTo(750, 1);
////				}, 
////				function() {
////					$(this).children('a').stop(true, true).animate({color:"#D4D4D4"}, 500);
////					$(this).children('.optbg').stop(true, true).fadeTo(750, 0);
////				}
////			);
//
//			$("#submit_add").hover(
//				function() {
//					$(this).stop(true, true).animate({color:"#000"}, 500);
//					$('#addBtn_bg').stop(true, true).fadeTo(750, 1);
//				}, 
//				function() {
//					$(this).stop(true, true).animate({color:"#D4D4D4"}, 500);
//					$('#addBtn_bg').stop(true, true).fadeTo(750, 0);
//				}
//			);
//			
//			$("#submit_view").hover( 
//				function() {
//					$(this).stop(true, true).animate({color:"#000"}, 500);
//					$('#viewBtn_bg').stop(true, true).fadeTo(750, 1);
//				}, 
//				function() {
//					$(this).stop(true, true).animate({color:"#D4D4D4"}, 500);
//					$('#viewBtn_bg').stop(true, true).fadeTo(750, 0);
//				}
//			);		
//			
//			$("#buy_btn_cntr,#purch_close").click(function(){
//				togglePurchDiv();
//				return false;
//			});
//		});
//	}
	
});


// OVERLAY FUNCTIONS
function olayShow(content_cntr,vcenter) {
	$('#overlay').css({top:($('#main_cntr').offset().top)+'px', left:(parseInt($('#main_cntr').offset().left)+10)+'px', width:($('#main_cntr').width()-18)+'px', height:($('#main_cntr').height())+'px'});
	$('#overlay').fadeIn(500,function(){
										$('#overlay').show();
									   });
	if (vcenter === true) {
		//VCENTER VIDEO CNTR
		var newTopPos = Math.floor((($(window).height() / 2) - (parseInt($(content_cntr).css('height')) / 2))) + ($(document).scrollTop()) - ($('#main_cntr').offset().top);
		if (newTopPos < 250) {
			newTopPos = 250;
		}
		$(content_cntr).css({top:(newTopPos)+'px'}); // use top: or paddingTop: depending on content 'push' needs, css, etc.
	}

}
function olayHide() {
	$('#vid_cntr').hide(0);
	jwplayer().stop();
	$('#overlay').fadeOut(500,function(){$('#overlay').hide();});
}
function olayToggle() {
	$('#overlay').toggleFade(500);
}

function centerVideoCntr() {
	//$('#vid_cntr').css({top:(($('#vid_cntr').parent().height() - $('#vid_cntr').height()) / 2) + 'px', left:(($('#vid_cntr').parent().width() - $('#vid_cntr').width()) / 2) + 'px'});
	vCenter('#vid_cntr','#overlay');
}
// VIDEO FUNCTIONS



// -- END SETTINGS ----------------------------------------------------------------------------------------









