$(document).ready(function() {

// 	$('.image-delay img').each(function () {
// 		$(this).attr('delaysrc',$(this).attr('src')).attr('src','/sites/all/themes/herkimer2011/images/loading.gif');
// 	});

	$('.view-work-lightbox-multiview .views-field-field-work-images-fid img,'
	+'.view-work-lightbox-multiview .views-field-field-work-images-fid-2 img').each(function() {
		if($(this).attr( "complete" )) {
			$(this).addClass('visible');
		} else {
			$(this).load(function() {
				$(this).addClass('visible');
			});
		}
	});

	// ---  Start Input Box Interactivity

	$('#footer input').each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				$(this).css('background','url(/sites/all/themes/herkimer2011/images/input-bg-focus.png)');
				if(this.value == default_value) {
					this.value = '';
				}
			});
			$(this).blur(function() {
					if(this.value == '') {
							$(this).css('background','url(/sites/all/themes/herkimer2011/images/input-bg.png)');
							this.value = default_value;
					}
			});
	});

	$('#footer textarea').each(function() {
			var default_value = this.value;
			$(this).focus(function() {
				$(this).css('background','url(/sites/all/themes/herkimer2011/images/input-textarea-bg-focus.png)');
				if(this.value == default_value) {
						this.value = '';
				}
			});
			$(this).blur(function() {
					if(this.value == '') {
					$(this).css('background','url(/sites/all/themes/herkimer2011/images/input-textarea-bg.png)');
					this.value = default_value;
					}
			});
	});

	/** Request a proposal logic - handles both front page and full form **/
	/* Hide captcha in request proposal BLOCK - user will be directed to the real form page, where they can continue with the captcha */
//	$('#block-block-4 fieldset').hide();
	/* On the real request a proposal page (node/36), remove the error message and replace it with something friendlier */
//	if($('.page-contact-request-proposal .messages.error').length > 0) {
//		if($('.messages.error li:contains(\'The answer you entered\')').length > 0) {
//			$('.messages.error li:contains(\'The answer you entered\')').remove();
//			$('#content-area').prepend('<div class=\'messages warning\'>Please type the words below to verify you are a real person.</div>');
//		} else {
//			$('.messages.error:contains(\'The answer you entered\')').remove();
//			$('#content-area').prepend('<div class=\'messages warning\'>Please type the words below to verify you are a real person.</div>');
//		}
//	}

	/* Determine form defaults so we know when to blank the fields and when not to (real data input) */
	$('#webform-client-form-36 #webform-component-name .form-text').attr('defaultvalue','Name');
	$('#webform-client-form-36 #webform-component-e_mail .form-text').attr('defaultvalue','e-mail');
	$('#webform-client-form-36 #webform-component-estimated_timeline .form-text').attr('defaultvalue','Estimated Timeline');
	$('#webform-client-form-36 #webform-component-question__request .form-textarea').attr('defaultvalue','Tell us about your project.');
	/* Defaulting/blanking logic for both inputs and textareas */
	$('#webform-client-form-36 input,#webform-client-form-36 textarea').each(function() {
			var default_value = $(this).attr('defaultvalue');
			$(this).focus(function() {
				if(this.value == default_value) {
						this.value = '';
				}
			});
			$(this).blur(function() {
					if(this.value == '') {
					this.value = default_value;
					}
			});
	});

	//---Sample if statement
	if ($('#site-slogan').length>0) {
		$('#site-slogan').html($('#site-slogan').html().replace('Web development and media creation.','<div class="slogan-emphasis">Web development & media creation.</div'));
	};



//--- Makes seperate lines of links function as one

	$('.views-row').bind('mouseenter', function() {
		$(this).addClass('hover');
	}).bind('mouseleave', function() {
		$(this).removeClass('hover');
	});

	$('.block').bind('mouseenter', function() {
		$(this).addClass('hover');
	}).bind('mouseleave', function() {
		$(this).removeClass('hover');
	});

	$('#nice-menu-1>li').bind('mouseenter', function() {
		$(this).addClass('lihover');
	}).bind('mouseleave', function() {
		$(this).removeClass('lihover');
	});

	$('#node-36 input, .node-type-webform input').bind('focus', function() {
		$(this).addClass('input-active');
	}).bind('blur', function() {
		$(this).removeClass('input-active');
	});

	$('#node-36 textarea, .node-type-webform textarea').bind('focus', function() {
		$(this).addClass('input-active');
	}).bind('blur', function() {
		$(this).removeClass('input-active');
	});


// --- Make links open in new window

	$("#jdock #block-menu-primary-links").each(function() {
		$("a[href^='http']").attr('target','_blank');
	});


	/**
	*  WORK / MULTIWORK PAGE INTERACTIVITY
	*  ---- Lightbox Multi-View   [ -- Dont' forget the CSS and to enable the Lightbox 2 Module!!! -- ]
	*  These blocks set up a mouseenter event system that replaces the visible detail image with a detail image matching the moused-over thumbnail
	*  Visible Thumbnails are in  .views-field-field-work-images-fid-2
	*  Hidden Details are in      .views-field-field-work-images-fid-1
	*  Visible Detail image is    .views-field-field-work-images-fid
	*/
	var view_class = '.view-id-work_lightbox_multiview';
	var visible_thumbs = '.views-field-field-work-images-fid-2';
	var hidden_details = '.views-field-field-work-images-fid-1';
	var visible_detail = '.views-field-field-work-images-fid';
	var multiview_fader_class = 'multiview-fader';

	//Give field-images matching item numbers
	$(view_class+' .views-row').each(function() {
		var itemid = 0;
		var sibling = $(this).find(hidden_details+' .field-item:first-child');
		$(this).find(visible_thumbs+' .field-item').each(function() {
			$(this).attr('itemid',itemid);
			sibling.attr('itemid',itemid).addClass('item-'+itemid);
			sibling = sibling.next();
			itemid++;
		});
	});

	// Add mouseenter events to each of the thumbnail images on the right
	$(view_class+' '+visible_thumbs+' img').each(function (i) {
		$(this).bind('mouseenter',function(e){
			viewsrow = $(this).parent().parent().parent().parent().parent();
			itemid = $(this).parent().parent().attr('itemid');
			thumblink = $(this).parent();
			detaillink = viewsrow.find(visible_detail+' a');

			oldimg = viewsrow.find(visible_detail+' img:first-child').attr('src');
			newimg = viewsrow.find(hidden_details+' .item-'+itemid+' img').attr('src');
			detaillink.find('img:first-child').attr('src',newimg);
			detaillink.attr('href',thumblink.attr('href'));

			if(oldimg != newimg) {
				$('.'+multiview_fader_class).stop().remove();
				detaillink.append("<img src='"+oldimg+"' class='"+multiview_fader_class+"' />");
				detaillink.find('.'+multiview_fader_class).animate({'opacity':0},500);
			}
		});
	});



	$('.front .view-work .views-field-nothing').hover(
		function () {
			$(this).find('.screen').stop().animate({ 'bottom': '-84px', 'opacity': 0}, 750);
			$(this).find('.field-image-desaturate').stop().animate({ 'opacity': 0}, 500);
		},
		function() {
			$(this).find('.screen').stop().animate({ 'bottom': '4px', 'opacity': 1}, 150);
			$(this).find('.field-image-desaturate').stop().animate({ 'opacity': 1}, 500);
		}
	);

	$('.more').hover(
		function () {
			$(this).stop().animate({ 'width': '75px'}, 250);
		},
		function() {
			$(this).stop().animate({ 'width': '0px'}, 100);
		}
	);


	$('#block-block-4 .handle').bind('click', function () {
		if($('#block-block-4').hasClass('open')) {
			$('#block-block-4').stop().animate({ 'width': '26px'}, 350).removeClass('open');
		} else {
			$('#block-block-4').stop().animate({ 'width': '241px'}, 350).addClass('open');
		}
	});

	$('.more').wrapInner('<div class="more-inner" />');
	$('.more-inner').corner();


	// --- Video Handling

	$('#block-block-8 h2').bind('click', function () {
		if($('#block-block-8 .content').hasClass('open')) {
			$('#block-block-8 .content').css('display','none').removeClass('open');
		} else {
			$('#block-block-8 .content').css('display','block').addClass('open');
		}
	});


	// --- Portfolio Page

	$('.view-work-lightbox-multiview  .views-row').bind('click', function() {
		if(!$(this).hasClass('open')) {
			$('.view-work-lightbox-multiview .views-row.open').each(function() {
				$(this).removeClass('open');
				$(this).find('.field-hidden').stop().animate({'height':'0px'},250);
			});
			$(this).find('.field-hidden').css('height','0px').stop().animate({'height':'444px'},250);
		} else {
			$(this).find('.field-hidden').stop().animate({'height':'0px'},250);
		}
		$(this).toggleClass('open');
	});

	// --- Here there be tooltip codes
	$('.view-work-lightbox-multiview .views-field-field-work-images-fid-2 img').each(function() {
		$(this).qtip({
			content: $(this).attr('alt'),
			style: {
					width: 200,
					padding: 5,
					background: '#fff',
					color: 'black',
					textAlign: 'center',
					border: {
						width: 5,
						radius: 5,
						color: '#FE921C'
					},

					tip: 'bottomLeft',
					name: 'dark' // Inherit the rest of the attributes from the preset dark style
			},
			show: 'mouseenter',
			hide: 'mouseleave'
		});
	});

	$('.view-work-lightbox-multiview [path='+$('#multiwork-path').attr('path')+']').click();
	//Expand the portfolio that appears in the URL
	//$('#portfolio-'+$('.portfolio-expand').attr('nid')).click();


	// --- The Menu

		//--- This eliminates the extra / at the beginning of the path/id

// 			$('#block-views-menuad-block_1 .field-content div').each(function(){
// 				$(this).attr('id', $(this).attr('id').replace('/', ''));
// 			});

	//---  [ Steve, please animate this as a fade?]


			//--- Fixes shadow display issue with IE
	var overlaytimeout;
	$('#nice-menu-1>li').bind('mouseenter', function() {
		clearTimeout(overlaytimeout);
		$('#shade').show().stop().animate({'opacity':0.6},250);
		showpath = $(this).find('>a').attr('href');
		pathid = showpath.replace('/','');
		pathsel = 'div[url="'+showpath+'"]';
		if(showpath && $(pathsel).length > 0 && $(this).find('#menu-promo-'+pathid).length == 0) {
			$(this).append("<div id='menu-promo-"+pathid+"' class='menu-promo'>"+$(pathsel).html()+"</div>");
		} else {
			$(this).find('#menu-promo-'+pathid).show();
		}
	}).bind('mouseleave', function() {
		$('#shade').stop().animate({'opacity':0},150);
		overlaytimeout = setTimeout("$('#shade').hide()",150);
		showpath = $(this).find('>a').attr('href');
		pathid = showpath.replace('/','');
		$(this).find('#menu-promo-'+pathid).hide();

	});

	$('#shade').css('opacity',0);



});
