/*
// ------------------------------------------------------------------  
// 
//  Dene Terrace Website Launch 2011
//   
//  File:   DT__main.orig.js
//     
//  Author: C. Schmidt chris(at)2309(dot)net
// 
// ------------------------------------------------------------------
*/


function startup() {
	$('#houses').fadeTo(80,1);
	$('.box:not(#boxhouses)').fadeOut(80,function(){ $('#blank').fadeOut(400); });
}

function quoteslide(_self){
  if (!_self&&qsrun) return; else qsrun=true;
	if (!$('#boxpress').is(':visible')) { qsrun=false; return; }
	$("#delay").fadeTo(4500,0,function(){
	  if (!$('#boxpress').is(':visible')) { qsrun=false; return; }
    var curr= $('.linkhover[switchtxt="'+$('.npqs:visible').attr('id')+'"]');
    var next= curr.nextAll('.linkhover[switchtxt]:first').length ? curr.nextAll('.linkhover[switchtxt]:first') : $(".linkhover[switchtxt='eve']");
    //alert(next.html());  
    if (gstop) { gstop=false; qsrun=false; return; }
    next.click();
    quoteslide(true);
	})
}

function nextimg(_self){
  if (!_self&&grun) return; else {grun=true; $('#stst').html('Stop');}
  if (!$('#boxgallery').is(':visible')) { grun=false; $('#stst').html('Start'); return; }
  $("#delay").fadeTo(3500,0,function(){
    if (!$('#boxgallery').is(':visible')) { grun=false; $('#stst').html('Start'); return; }
    if (gstop) { gstop=false; grun=false; $('#stst').html('Start'); return; }
    $('#gright').click();
    nextimg(true);
  })
}


	var loaded = 0,
		imgUrls = [],
		errorTimer;
		
function onImgComplete(){
		clearTimeout(errorTimer);
		if (imgUrls && imgUrls.length && imgUrls[loaded]) {
			loaded++;
			  if (!imgUrls[loaded]) setTimeout( function(){ startup(); }, 1800 );
				/*var nowloading = (imgUrls[loaded]) ? 
					'Now Loading: <span>' + imgUrls[loaded].split('/')[imgUrls[loaded].split('/').length - 1] : 
					'Loading complete'; // wrong status-text bug fixed
				jQuery('#prestatus').html('<span class="numLoaded">' + loaded 
				+ '</span> of <span class="numTotal">' + imgUrls.length 
				+ '</span> loaded (<span class="percentLoaded">' 
				+ (loaded / imgUrls.length * 100).toFixed(0) + '%</span>) <span class="currentImg">' 
				+ nowloading + '</span></span>'); */
				jQuery('#prestatus').html('<span class="percentLoaded" style="float:right;">' 
				+ (loaded / imgUrls.length * 100).toFixed(0) + '%</span>');
			  loadImgs();
		}
	}

function loadImgs(){
		if(imgUrls && imgUrls.length && imgUrls[loaded]){
			var img = new Image(); 
			img.src = imgUrls[loaded];	
			if(!img.complete){
				jQuery(img).bind('error load onreadystatechange', onImgComplete);
			} else {
				onImgComplete();
			}
			errorTimer = setTimeout(onImgComplete, 999);
		}
	}

function collectImgs() {
		var everything = $('body').find("*:not(script)").each(function() {
			var url = "";
			if ($(this).css("background-image") != "none") {
				var url = $(this).css("background-image");
			} else if (typeof($(this).attr("src")) != "undefined" && $(this).is('img') ) {
				var url = $(this).attr("src");
			}
			url = url.replace("url(\"", "");
			url = url.replace("url(", "");
			url = url.replace("\")", "");
			url = url.replace(")", "");
			if (url.length > 0) {
				imgUrls.push(url);
			}
		});
		setTimeout( function(){ loadImgs(); },10 );
	}



$(document).ready(function(){	
  qsrun=false; grun=false; gstop=false; timeout=null;target=null;
  $(".navi:not(:last)").css('margin-right','36px');
	setTimeout(function(){$('#navibar').css('left',(924-$('#navibar').width())/2+'px');},200);
	$(".npqs:not(:first)").fadeOut(80);
  $("#boxgalleryimg>img:not(:first)").fadeOut(80);
  $("#boxgalleryimg>img").css('position','absolute');
  $("#boxgalleryimgthumbs img:first").fadeTo(80,0.6).attr('border','0px');
  
  $('.navi,.linkhover:not(.nofade)').css({'color':'#111111'}).fadeTo(80,0.4); 
	$(".linkhover[switchtxt='eve']").fadeTo(80,1);
  
	$('.navi,.linkhover:not(#creditline)').hover( function(){
		//$(this).stop().css({'color':'#323232'});
		if (!$(this).hasClass('nofade')) $(this).stop().fadeTo(500,1);		
	},function(){
	  //$(this).stop().css({'color':'#B2B2B2'});
	  if (!$(this).hasClass('nofade')) if (!$('#box'+$(this).attr('id')).is(':visible') 
	      && !$("#"+$(this).attr('switchtxt')).is(':visible') ) $(this).stop().fadeTo(280,0.4);
	}).css('cursor','pointer').click(function(){ 
		$(this).stop().fadeTo(180,0.4,function(){$(this).fadeTo(180,1)});
	});
	
	$('#creditline').hover( function(){
		//$(this).stop().css({'color':'#323232'});
		$(this).stop().css({'color':'#064700'}).fadeTo(500,1);		
	},function(){
	  //$(this).stop().css({'color':'#B2B2B2'});
	  $(this).stop().css({'color':'#111111'}).fadeTo(280,0.4);
	}).css('cursor','pointer').click(function(){ 
		$(this).stop().fadeTo(180,0.4,function(){$(this).fadeTo(180,1)});
	});	

	//$('.boximg').css('cursor','pointer').click(function(){ 
	//	alert('opens a larger version of the image');
	//});
	
	$('.navi').click(function(){  
		var _newbox = '#box'+$(this).attr('id');
		var _id = $(this).attr('id');
		if ('#'+$('.box:visible').attr('id')==_newbox) { 
		  //alert('hey');
		} else {
		  if ($(_newbox+' .boxhead').html()=='') $(_newbox+' .boxtext').css('top','82px'); 
			$('.box:visible').fadeOut(600);
			$(_newbox).fadeIn(600);
			$('.navi').not(this).fadeTo(80,0.4);
			$(this).fadeTo(80,1);
			_gaq.push(['_trackPageview', '/'+_id]);
		}
	});
	$('#press').click(function(){quoteslide();})
	$('#gallery').click(function(){nextimg();})
	
	$('.linkhover').click(function(){ 
	  if ($(this).attr('site')!==undefined) {
	  	$('#'+$(this).attr('site')).click();
	  }
	  if ($(this).attr('target')!==undefined) {
	    target=$(this).attr('target');
	  	_gaq.push(['_trackEvent', 'Link', 'outbound', target]);
	  	setTimeout(function(){window.open(target);},200);
	  }
	  if ($(this).attr('switchtxt')!==undefined) {  
	  	$(".npqs:visible").fadeOut(600);
	  	$("#"+$(this).attr('switchtxt')).fadeIn(600);
	  	$('.linkhover[switchtxt]').not(this).fadeTo(80,0.4);
			$(this).fadeTo(80,1);
	  }
	  if ($(this).attr('switchimg')!==undefined) {  
	  	$("#"+$(this).attr('switchimg')).click();
	  	     _gaq.push(['_trackEvent', 'Gallery', 'ThumbClick' ]);
	  }
	  if ($(this).attr('startstop')!==undefined) {  
	  	if (grun) { 
	  	    gstop=true; $('#stst').html('Start'); 
	  	     _gaq.push(['_trackEvent', 'Gallery', 'Start' ]);
	   } else { nextimg();
	  	    _gaq.push(['_trackEvent', 'Gallery', 'Stop' ]);
	  	}
	  }
	  if ($(this).attr('mailto')!==undefined) {
	  	window.location.href = $(this).attr('mailto');
	  	_gaq.push(['_trackEvent', 'Link', 'mailTo', $(this).attr('mailto')]);
	  }
	  if ($(this).attr('submit')!==undefined) {
	  	_f=$(this).parent('form');
	  	if (_f.children('input[name="name"]').val().length==0) alert('Please enter your name!');
	  	else if (_f.children('input[name="email"]').val().length==0) alert('Please enter your email address!');
	  	else if (_f.children('textarea').val().length>300) alert('Please enter only 300 characters in the message field!'); 
	  	else {
	  	  _f.children('.linkhover').html("Please wait while the form is submitting!").unbind();
	  	   _gaq.push(['_trackEvent', 'RegisterForm', 'Submit' ]);
	  	  _f.ajaxSubmit({
       		type: "POST",
       		url: "DT__data/formstore.php",
       		success: function(msg){
         		_f.children('.linkhover').html("Your request was successfully submitted!");
       		}
    	});
	  	}
	  }
	})
	
	$("#scrollbox").click(function(){
		//$(this).animate({left:'-=10px'});
	})
  
  $('#boxgalleryimgthumbs img').css('cursor','pointer').click(function(){
  	var pos= $(this).prevAll("img").size();
  	$("#boxgalleryimg>img:visible").fadeOut(600);
  	$("#boxgalleryimg>img:eq("+pos+")").fadeIn(600);
  	$("#boxgalleryimgthumbs img").fadeTo(80,1).attr('border','0px');
  	$(this).fadeTo(80,0.6).attr('border','0px');
  	if (pos>3) {
  		var newp=5-(88*(pos-3));
  		$("#scrollbox").animate({left:newp+'px'});
  	} else {
  	  $("#scrollbox").animate({left:'5px'});
  	}
  });
  
  $('#gleftb').css('cursor','pointer').click(function(){
		var pos= $("#boxgalleryimg>img:visible").prevAll("img").size();
		var next= $("#boxgalleryimg>img:visible").nextAll("img").size();
		if (pos>0) { var next=pos-1; } else { next = next; }
		$("#boxgalleryimgthumbs img:eq("+next+")").click();
	});
	
	$('#gright').css('cursor','pointer').click(function(){
		var pos= $("#boxgalleryimg>img:visible").prevAll("img").size();
		var next= $("#boxgalleryimg>img:visible").nextAll("img").size();
		if (next>0) { var next=pos+1; } else { next = 0; }
		$("#boxgalleryimgthumbs img:eq("+next+")").click();
	});
	
	$('#gright,#gleftb,#boxgalleryimgthumbs img').mousedown(function(){if (grun) {gstop=true; $('#stst').html('Start'); } })
	$('#boxpress .linkhover').mousedown(function(){if (qsrun) gstop=true;})
	
	$('#gtleftb').css('cursor','pointer').click(function(){
			//if ($("#scrollbox").position().left<5) $("#scrollbox").animate({left:'+=81px'},320);
	}).mousedown(function(){
		  timeout = setInterval(function(){
		      if ($("#scrollbox").position().left<5) $("#scrollbox").animate({left:'+=5px'},30); 
		  }, 30);
		  return false;
	});
	$(document).mouseup(function(){
		  if (timeout!=undefined) clearInterval(timeout);
		  return false;
	});

	
	$('#gtright').css('cursor','pointer').click(function(){
			//$("#scrollbox").animate({left:'-=81px'},320);
	}).mousedown(function(){
		  timeout = setInterval(function(){
		      $("#scrollbox").animate({left:'-=5px'},30); 
		  }, 30);
		  return false;
	});
	
	collectImgs();

});




