$(document).ready(function() {
	$('input[type=text]').focus(function(){
		var currentVal = $(this).val();										// Read initial field value
		$(this).val('');														// Clear initial value

		$(this).blur(function(){
			var getNewVal = $(this).val();										// Read new input value
			if ( getNewVal == '' || getNewVal == ' ' ) {
				$(this).val(currentVal);										// Switch to initial value
			}
		});
	});

	// fix png for IE
	$('img[src$=.png], div').ifixpng();


	/* portfolio filtering */
	
	
	$("#portfolio .filters a").click(function(){
		
		 var trg = $(this).parent()[0];
		
		 if ($(trg).hasClass("all")){
			 $(trg).siblings().removeClass("off");
		 }else{
			 $(trg).removeClass("off").siblings().not(".all").addClass("off");
		 }
		
		 sectionId = $(trg).attr('class').replace('section_','');
		 filter_show(sectionId);
		
		 return false;
	});
	 
	function filter_show($sectionId){
		$("#portfolio .cols").hide('slow',function(){
			$("#portfolio .cols").empty();
			//load the selected section ul
			 $("#portfolio .cols").load("/index.php?fuseaction=ajax.portfolio", { sectionid: $sectionId}, function(){
				$(this).show('slow');
				setPrettyPhoto();
			 });
			//load the selected section pagination
			 $("#paging").load("/index.php?fuseaction=ajax.portfolio&view=pagination", { sectionid: $sectionId});
		});
		
		 
		 
		 
	} 
	
	
	/*
	function filter_show(){
		 var cls = [];
		 var i = 0;
		 $("#portfolio .filters a").parent().each(function(){
			 var on = 0;
			 if( ! $(this).hasClass("off") ) on = 1;
			 var className = this.className.split(" ",1)[0];
			 cls[i++]={cls:className, on:on};
			 
		 });
		
		 for(i=0;i<cls.length;i++){
			// console.log(cls[i]);
			 if (cls[i].on){
				 $("#portfolio .cols li."+cls[i].cls).show("slow");
			 }else{
				 $("#portfolio .cols li."+cls[i].cls).hide("slow");
			 }
		 }
	} 
	*/
	$("body").append("<a id='dummy' rel='prettyPhoto[iframes]' href=''></a>");

	// prettyPhoto stuff
	setPrettyPhoto();

	/* case studies */
	$("#case-studies .tabs a").each(function(){
		var trg = $(this).parent()[0];
		var cls = trg.className.split(" ",1)[0];
		if ( $(trg).hasClass("on") ){
			$("#case-studies .tabs-content > li."+cls).addClass("on").show();
		}else{
			$("#case-studies .tabs-content > li."+cls).removeClass("on").hide();

		}

	});
	$("#case-studies .tabs a").click(function(){
		var trg = $(this).parent()[0];
		var cls = trg.className.split(" ",1)[0];
		$("#case-studies .tabs li").removeClass("on");
		$(trg).addClass("on");
//		$("#case-studies .tabs-content > li.on").removeClass("on").hide();
//		$("#case-studies .tabs-content > li."+cls).addClass("on").show();
		$("#case-studies .tabs-content > li:visible").removeClass("on").fadeOut("medium", function(){
			$("#case-studies .tabs-content > li."+cls).addClass("on").fadeIn("medium");
		});
		return false;
	});


	//select boxes
	if ( typeof $.fn.altselect != "undefined" ){
		$("select").altselect();
	}
	

});

function setPrettyPhoto(){
	if ( $.fn.prettyPhoto ){
		$("a[rel^='prettyPhoto']").prettyPhoto({
			animationSpeed: 'normal', /* fast/slow/normal */
			showTitle: false, /* true/false */
			padding: 29, /* padding for each side of the picture */
			opacity: 0.5, /* Value betwee 0 and 1 */
			theme: 'light_square', /* light_rounded / dark_rounded / light_square / dark_square */
			hideflash: true,
			allowresize: false,
			callback: function(){} /* Called when prettyPhoto is closed */
		});
	}
}

function videoPopUp(url, w, h){
	var href = url+"?iframe=true&width="+w+"&height="+h+"";
	$("#dummy").attr("href",href).click();
}

// Define indexOf for IE
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

jQuery.fn.log = function (msg) {
  console.log("%s: %o", msg, this);
  return this;
};