//##############################################################################
//##  DOM																	  ##
//##############################################################################
$(document).ready( externalLinks );
/*###########################################
##  Dropdown Menus						   ##
###########################################*/
/* Options */
//var __global_menuContainer = '[elID]';
/* Controls */
/*
$(document).ready(function() {
	var lis = $(__global_menuContainer).children('li').get();
    $.each(lis, function(){
      $(this).hover(
	    function () { //on
	      $(this).addClass("sfhover")
	    }, 
	    function () { //off
	      $(this).removeClass("sfhover")
	    }
	  );
    });
});
*/

/*###########################################
##  Legacy Transparency					   ##
###########################################*/
$(document).ready(function() {
  /* Body */
  $("img").each(function(){ //fix legacy transparency
    var fileExtension = $(this).attr("src").substr($(this).attr("src").lastIndexOf(".png"));
    if (fileExtension == ".png") { $(this).addClass("transparent"); }
  });
});

/*###########################################
##  Content Sliders						   ##
###########################################*/
/* Options */
var __global_sliderObject = '.slides';
var __global_pagerObject = '#slidePages';
var __global_captionObject = '#slideCaption';

/* Controls */
$(function() {
  if ($(__global_sliderObject).exists()) { //main promotion slider
    $(__global_sliderObject)
      .cycle({ 
        fx:		'fade', 
        timeout:	6000, //how long to wait between slides
        speed:	600, //how long transition takes
        pause:	1, //stop on mouseover
        pager:	__global_pagerObject,
        before: clearCaption,
        after: changeCaption
    });
  }
  //create automatic thumbnails
  $(this).createPoints();
});
/* Points */
$.fn.createPoints = function() { 
  var pages = $(__global_pagerObject).children('a').get(); //read pagination
  $.each(pages, function(){ $(this).html('&#8226;'); });
};
/* Before */
function clearCaption() {
  if ($(__global_captionObject).exists()) { $(__global_captionObject).html('').fadeOut(); }
}
/* After */
function changeCaption() {
  if ($(__global_captionObject).exists()) {
    var altSrc = $(this).find("img").attr('alt');
    // console.log(altSrc);
    var pieces = altSrc.split(';');
    $(__global_captionObject).html('<h2>'+ pieces[0] +'</h2><h3>'+ pieces[1] +'</h3>').fadeIn();
    Cufon.replace('#slideCaption h2', { fontFamily: 'Futura Std', fontWeight: '700' });
    Cufon.replace('#slideCaption h3', { fontFamily: 'Futura Std', fontWeight: '500' });
  }
}

/*###########################################
##  Cufón Init							   ##
###########################################*/
Cufon.replace('#navigation h2 a, #columns .left b, #quote h2, #licenses h2, .siteContact label, #quote p b, #quote label, #quote .legend, #clients h2, #featured h2, #breadcrumbs .crumb, #bodyContent h1', { fontFamily: 'Plantagenet Cherokee', hover: true });
Cufon.replace('#contact li, #contact h2, #contact h3, #contact h4, #quote h4, #featured .details h3, #projectList .links h3', { fontFamily: 'Futura Std', fontWeight: '500', hover: true });
Cufon.replace('#contact b, #contact h4 span, #bottom p b, #footer p b', { fontFamily: 'Futura Std', fontWeight: '500', fontStretch: 'condensed', hover: true });
Cufon.replace('#contact h5, .locations p span, #quote h3 a, #featured .details h1 a, #categories .list li a, #projectLink .list li a, #projectList .details h2 a, #bodyContent h1 b, #projectDetail .details h2, .index .item h2', { fontFamily: 'Futura Std', fontWeight: '700', hover: true });
Cufon.replace('#slideCaption h2', { fontFamily: 'Futura Std', fontWeight: '700' });
Cufon.replace('#slideCaption h3', { fontFamily: 'Futura Std', fontWeight: '500' });

/*###########################################
##  Form Ghost							   ##
###########################################*/
$(document).ready( function() { $("input[class*='ghost']").placeGhosts(); });
$.fn.placeGhosts = function() {
  this.each(function(index,text) {
    this.setAttribute("rel",this.value);
	$(this).focus(function () {
				$(this).removeClass('inactive');
				$(this).addClass('active');
				if (this.value === this.getAttribute("rel")) { this.value=''; }
			})
	       .blur(function () {
	       		if (this.value === '') {
	       			$(this).removeClass('active');
	       			$(this).addClass('inactive');
	       			this.value=this.getAttribute("rel");
	       		}
		   });
  });
}; //end placeGhosts

//##############################################################################
//##############################################################################
//##  User Handler Functions												  ##
//##############################################################################
function externalLinks() {
  var exs = $('a[rel=external]');
  $.each(exs, function() {
    if ($(this).attr('href')) { $(this).attr('target','_blank'); }
  });
}

/*###########################################
##  Form Validation						   ##
###########################################*/
$(document).ready(function() {
	$('.siteContact').submit(function() {
    var valid = true;
    var errors = $('<div id="formErrors" style="display: none;"></div>');
    
    $(this).find('.require').each(function() {
      if($(this).attr('value') == '') { valid = false; $(errors).append($(this).parent().prev().find('label').text()+" is required!\n"); }
    });
    
    if(!valid) { alert($(errors).html()); }
    
    $(errors).remove();
    
    return valid;
  });
});
//##############################################################################
//##############################################################################

//##############################################################################
//##############################################################################
//##  Globals																  ##
//##############################################################################
jQuery.fn.exists = function(){return jQuery(this).length>0;}
//##############################################################################
//##############################################################################
