/* Declare a namespace for the site */
var Site = window.Site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {

	/*- for use with slider */
	$.fn.mousehold = function(timeout, f) {
		if (timeout && typeof timeout == 'function') {
			f = timeout;
			timeout = 10;
		}
		if (f && typeof f == 'function') {
			var timer = 0;
			var fireStep = 0;
			return this.each(function() {
				$(this).mousedown(function() {
					fireStep = 1;
					var ctr = 0;
					var t = this;
					timer = setInterval(function() {
						ctr++;
						f.call(t, ctr);
						fireStep = 2;
					}, timeout);
				})
				clearMousehold = function() {
					clearInterval(timer);
					if (fireStep == 1) f.call(this, 1);
					fireStep = 0;
				}
				$(this).mouseout(clearMousehold);
				$(this).mouseup(clearMousehold);
			})
		}
	}

	//same as $(document).ready();
	$(function() {

//		$('header,div#media,div#copy').hide();

		/*- defaultvalue input */

		var active_color = '#000';
		var inactive_color = '#aaa';
		$("input.defaultValue").css("color",inactive_color);
		var default_values = new Array();
		$("input.defaultValue").focus(function() {
			if (!default_values[this.id]) {
				default_values[this.id] = this.value;
			}
			if (this.value == default_values[this.id]) {
				this.value = '';
				this.style.color = active_color;
			}
			$(this).blur(function() {
				if (this.value == '') {
					this.style.color = inactive_color;
					this.value = default_values[this.id];
				}
			});
		});

		$("form#emailSignup").submit(function(){
			var email = $(this).children('input[name=email]').val();
			var action = $(this).children('input[name=action]').val();
			$.post('actions.php',{ action: action, email: email },function(data) {
				$('span#response').hide().html(data).fadeIn('fast');
			});
			$(this).children('input[name=email]').val('').focus().blur();
			return false;
		});


		/*- fullsize divs */

		var winW = $(window).width();
		var winH = $(window).height();
		$('div#splashflash').width(winW).height(winH);


		/*- fade out the page title box big letter */

		function fadeLetter() {
			$('.bgLetter').show().css({ opacity: '0.2' }).fadeOut(10000);
		}
		if ($('div#splashflash').is(":hidden") || $('div#splashflash').length == 0) { fadeLetter(); }


		/*- site fader (simple hack solution to avoid content flicker on page load) */

/*		$('a').click(function(e){
			if (
				$(this).attr('target') != "_blank"
				&&
				$(this).attr('href') != "#"
				&&
				$(this).attr('href') !== 'mailto:info@degiulio.org'
				&&
				$(this).attr('href') !== 'mailto:careers@degiulio.org'
				) {
				var href = $(this).attr('href');
				e.preventDefault();
				$('header,div#media,div#copy').fadeOut(500,function(){
					window.location.href = href;
				});
			}
		});
*/

		/*- box-1-1 fader */

		var numFTh = $('div.fader').children('img').size();
		$(window).load(function(){
			$('div.fader img').show();
		});
		function fader(i) {
			$('div.fader img:eq('+i+')').delay(2000).fadeOut(5000,function(){
				if (numFTh == 1) {
					numFTh = $('div.fader').children('img').size();
					$('div.fader img').delay(2000).fadeIn(5000,function(){
						fader(numFTh-1);
					});
				}
				else {
					numFTh--;
					fader(numFTh);
				}
			});
		}
		fader(numFTh-1);

	
		/*- box-2-1 gallery */

		$('div.size img').click(function(){
			if ($(this).hasClass('Open')) {
				$(this).removeClass('Open').attr('src','img/size-plus.gif').stop().parent('div.size').parent('div.gallery').animate({ height: '250px' },'slow',function(){
					$(this).parent('div.size').parent('div.gallery').parent().css('overflow','hidden');
				});
				$('div#copy > div').animate({ paddingTop: '20px' },'slow');
			}
			else {
				$(this).addClass('Open').attr('src','img/size-minus.gif').stop().parent('div.size').parent('div.gallery').animate({ height: '501px' },'slow').parent().css('overflow','visible');
				$('div#copy > div').animate({ paddingTop: '271px' },'slow');
			}
		});
		$('div.gallery div.nav img').click(function(){
			var curGa = $(this).parent().parent().parent();
			var numTh = curGa.find('div.thumbs').children('img').size() - 1;
			var imgID = $(this).attr('id').substring(1);
			var prvID = ( parseInt(imgID) - 1 );
			var nxtID = ( parseInt(imgID) + 1 );
			if ( prvID < 0 ) { prvID = numTh; }
			if ( nxtID > numTh ) { nxtID = 0; }
			curGa.stop().animate({ opacity: 0 },'fast',function(){
				curGa.css('background','url(img/uploads/'+galleryImages[imgID]+') 50% 50% no-repeat').animate({ opacity: 1 },'fast');
			});
			curGa.parent().parent().parent().find('#galleryImageCredit').text(galleryImageCreds[imgID]);
			curGa.find('div.prev').children('img').attr('id','#'+prvID);
			curGa.find('div.next').children('img').attr('id','#'+nxtID);
			curGa.find('div.thumbs').children('img').attr('src','img/thumb.gif');
			curGa.find('div.thumbs').children('img[id=#'+imgID+']').attr('src','img/thumb-on.gif');
		});


		/*- slider */

		var numDv = $('div#media div.slider div.slide').children().size();
		var sldrW = numDv * 251;
		var sldrP = 0;
		$('div#media div.navLeft').hide();
		$('div#media div.slider div.slide').width(sldrW);
		$('div#media div.navLeft').mousedown(function(){
			$('div#media div.navRight').fadeIn();
		});
		$('div#media div.navLeft').mousehold(function(){
			if (sldrP < 0) {
				sldrP += 5;
				$(this).next().children('div.slide').css('left',sldrP);
			}
			else {
				$(this).fadeOut();
			}
		});
		$('div#media div.navRight').mousedown(function(){
			$('div#media div.navLeft').fadeIn();
		});
		$('div#media div.navRight').mousehold(function(){
			if (sldrP > -( sldrW - 1003 )) {
				sldrP -= 5;
				$(this).prev().children('div.slide').css('left',sldrP);
			}
			else {
				$(this).fadeOut();
			}
		});
		$('div#media div.slide img').hover(function(){
			$(this).animate({ opacity: .15 },'fast');
		},function(){
			$(this).animate({ opacity: 1 },'fast');
		});


		/*- Press Popup */

		$('a.PDF').colorbox({
			onOpen: function(){
				$('div#pressPop').load("actions.php",{ action: "displayPressEntryImages", eid: $(this).attr('id') });
				$('div#cboxContent').append('<img id="zoomPDF" src="img/zoomPDF-plus.png" />');
				$('div#colorbox').show();
			},
			width: '900px',
			height: (winH - 50),
			inline: true,
			close: '&times;',
			href: '#pressPopContainer'
		});
		$('img#zoomPDF').live('click',function(){
			if ($(this).hasClass('zoomed')) {
				$(this).removeClass('zoomed').attr('src','img/zoomPDF-plus.png');
				$('div#pressPop img').each(function(){
					var newH = $(this).height() / 2;
					$(this).animate({ marginLeft: '200px', width: '400px', height: newH },'slow');
				});
			}
			else {
				$(this).addClass('zoomed').attr('src','img/zoomPDF-minus.png');
				$('div#pressPop img').each(function(){
					var newH = $(this).height() * 2;
					$(this).animate({ marginLeft: '0px', width: '800px', height: newH },'slow');
				});
			}
		});
		$('.slider .overlay').mouseover(function(){
			$(this).stop(true,true).fadeOut(300);
		});
		$('.slider .spacer').mouseout(function(){
			$('.slider .overlay').stop(true,true).fadeIn(300);
		});


		/*- Look Inside Book Popup */

		$('div#lookInsidePop').load("actions.php",{ action: "displayLookInsideImages" },function(){
			$('a.lookInside').colorbox({
				width: '995px',
				height: '580px',
				close: '&times;',
				next: '&gt;',
				previous: '&lt;',
				current: '',
				scrolling: false,
//				transition: 'fade',
				onLoad: function(){
					$('#colorbox').show();
					$('#cboxClose').css({ right: '30px' });
				}
			});
		});


	});


	$(window).resize(function(){

		/*- fullsize divs */

		var winW = $(window).width();
		var winH = $(window).height();
		$('div#splashflash').width(winW).height(winH);


	});

})(jQuery);



