/* globals */
$.fn.exists=function(callback){var rv = this.length>0;if(typeof callback =='function') {if(rv) callback();rv = this;}return rv;};

$(document).ready(function() {
	/* cufon */
	Cufon.replace('.face', { fontFamily: 'Frutiger LT Std', hover: true });
	Cufon.replace('#nav li a', {
		hover: true, 
		fontFamily: 'Frutiger LT Std',
		fontWeight: 700,
		textShadow: '-1px -1px rgba(0, 0, 0, 0.1)'
	});
	
	/* slider */
	var slider = '#slides';
	var pager = '.switcher';

	$(slider).exists(function() {//main promotion slider
		$(slider)
			.cycle({
				fx:		'fade', 
				timeout:	6000, //how long to wait between slides
				speed:	600, //how long transition takes
				pause:	1, //stop on mouseover
				pager:	pager,
				pagerAnchorBuilder: function(idx, slide) { 
					return '<li><a href="#">'+(idx+1)+'</a></li>'; 
				}
		});
	});
	
	alignImages();
	
	externalLinks();
	
	clearFormFields({
		clearInputs: true,
		clearTextareas: true,
		passwordFieldText: true,
		addClassFocus: "focus",
		filterClass: "default"
	});
	
	flickrSets();

	/* Context Menus */
  var lis = $('#nav').children('li').get();
  $.each(lis, function() {
      var navParent = this;
      var contextTarget = $(this).children('a').first().text(); //grab nav option name
      var fmt = contextTarget.toLowerCase(); //make name lowercase
      var cleanTarget = fmt.replace(/[^a-z]+/g, '') + '_menu'; //strip everything but letters, append handle
      $(this).hover(

      function() { //nav li ON
          $('#' + cleanTarget).exists(function() { //if there's a context menu for this nav, use it
              $('#' + cleanTarget).hover( //assign mirror of hover behavior to context container, too


              function() { //context container ON
                  $(navParent).addClass('active'); //force nav item to stay in 'active' state
                  $(slider).cycle('pause').hide(); //pause and hide cycle
                  $('ul.context').show(); //show context container
                  $('#' + cleanTarget).show(); //show context content container
              }, function() { //context container OFF
                  $('#' + cleanTarget).hide(); //hide context content container
                  $('ul.context').hide(); //hide context container
                  $(slider).cycle('resume').show(); //resume and show cycle
                  $(navParent).removeClass('active'); //take nav item 'active' state away
              });
              $(slider).cycle('pause').hide(); //pause and hide cycle
              $('ul.context').show(); //show context container
              $('#' + cleanTarget).show(); //show context content container
          });
      }, function() { //nav li OFF
          $('#' + cleanTarget).hide(); //hide context content container
          $('ul.context').hide(); //hide context container
          $(slider).cycle('resume').show(); //resume and show cycle
          //$('#'+cleanTarget).exists(function() { $(navParent).addClass('active'); }); //force nav item to stay in 'active' state
      });
  });
});

/* external links */
function externalLinks() {
	var exs = $('a[rel=external]');
  $.each(exs, function() {
    if ($(this).attr('href')) { $(this).attr('target','_blank'); }
  });
}

/* image alignment */
function alignImages() {
  $('img[align$=left]').addClass('alignLeft').removeAttr('align');
  $('img[align$=right]').addClass('alignRight').removeAttr('align');
}

/* flickr sets */
function flickrSets() {
	if ($('#photoSets a').exists()) {
		$('#photoSets a')
			.colorbox({initialWidth: 300, initialHeight: 300, maxWidth:'80%', maxHeight: '60%'})//.attr('rel', 'Photos')
			.exists(function() {
				var thumbsSet = false;
				var thumbs = null;
				$(document)
				.bind('cbox_complete', function() {
					var s = $('#cboxContent .slideshow').colorbox({slideshow:true, slideshowSpeed: 6400});
					if(s.length == 0 || thumbsSet) return;
					thumbs = $('#cboxLoadedContent div:first')[0];
					thumbsSet = true;
				})
				.bind('cbox_load', function() { if(thumbs!=null&&$('#cboxThumbs').length==0){thumbs.id = 'cboxThumbs';$('#cboxWrapper').append(thumbs);} })
				.bind('cbox_cleanup', function() { thumbsSet = false;$(thumbs).remove(); thumbs = null;})
		});
	}
}

/* clear form fields */
function clearFormFields(o)
{
	if (o.clearInputs == null) o.clearInputs = true;
	if (o.clearTextareas == null) o.clearTextareas = true;
	if (o.passwordFieldText == null) o.passwordFieldText = false;
	if (o.addClassFocus == null) o.addClassFocus = false;
	if (!o.filter) o.filter = "default";
	if(o.clearInputs) {
		var inputs = document.getElementsByTagName("input");
		for (var i = 0; i < inputs.length; i++ ) {
			if((inputs[i].type == "text" || inputs[i].type == "password") && inputs[i].className.indexOf(o.filterClass)) {
				inputs[i].valueHtml = inputs[i].value;
				inputs[i].onfocus = function ()	{
					if(this.valueHtml == this.value) this.value = "";
					if(this.fake) {
						inputsSwap(this, this.previousSibling);
						this.previousSibling.focus();
					}
					if(o.addClassFocus && !this.fake) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				inputs[i].onblur = function () {
					if(this.value == "") {
						this.value = this.valueHtml;
						if(o.passwordFieldText && this.type == "password") inputsSwap(this, this.nextSibling);
					}
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
				if(o.passwordFieldText && inputs[i].type == "password") {
					var fakeInput = document.createElement("input");
					fakeInput.type = "text";
					fakeInput.value = inputs[i].value;
					fakeInput.className = inputs[i].className;
					fakeInput.fake = true;
					inputs[i].parentNode.insertBefore(fakeInput, inputs[i].nextSibling);
					inputsSwap(inputs[i], null);
				}
			}
		}
	}
	if(o.clearTextareas) {
		var textareas = document.getElementsByTagName("textarea");
		for(var i=0; i<textareas.length; i++) {
			if(textareas[i].className.indexOf(o.filterClass)) {
				textareas[i].valueHtml = textareas[i].value;
				textareas[i].onfocus = function() {
					if(this.value == this.valueHtml) this.value = "";
					if(o.addClassFocus) {
						this.className += " " + o.addClassFocus;
						this.parentNode.className += " parent-" + o.addClassFocus;
					}
				}
				textareas[i].onblur = function() {
					if(this.value == "") this.value = this.valueHtml;
					if(o.addClassFocus) {
						this.className = this.className.replace(o.addClassFocus, "");
						this.parentNode.className = this.parentNode.className.replace("parent-"+o.addClassFocus, "");
					}
				}
			}
		}
	}
	function inputsSwap(el, el2) {
		if(el) el.style.display = "none";
		if(el2) el2.style.display = "inline";
	}
}
