$(document).ready(function(){
	initForms();
	initArtists();
	initProducts();
});

function initProducts(){
	
	$(".product").click(function(e){
		e.preventDefault();
		var info = $(".info", this).clone();
		$("#onShow").empty().append(info);
		var active = $("#onShow .product_info h2").text();
		$("#enquiry_regarding").val(active);
		
	});
	
	$(".product:eq(0)").trigger("click");
	
	
}

function initArtists(){


	
	$(".artist a").each(function(){
		var href= $(this).attr('href') + "&colorbox=true";
		$(this).colorbox({
			transition: 'fade',
			speed: 500,
			href: href,
			width: '830px',
			height: '900px'
		});
	});
	
	$().bind('cbox_complete', function(){
		initPhotos();
	});
}

function initPhotos(){
	var height = 0;
	var images = $("#photos img");
	var selectedIndex = 0;
	$(images).each(function(i){
		
		if(i>0){
			$(this).hide();
		}
		
		if(i == 0){
			height = $(this).height();
			$(this).addClass('active');
		}
	});
	
	$("#photo_holder").height(height);
	
	$("#photos .next").click(function(e){
		e.preventDefault();
		
		var next = selectedIndex + 1;
		if(next >= images.length){
			next = 0;
		}
		selectedIndex = next;
		$(images).removeClass('active');
		var nextImage = $("#photos img:eq(" + next + ")").addClass('active');
		$("#photo_holder").animate({
			opacity: 0
		}, 600, 'swing', function(){
			var height = $(nextImage).height();
			$(images).hide();
			$(nextImage).show();
			$("#photo_holder").animate({
				height: height,
				opacity: 1
			});
		});
		
	});
	
	$("#photos .previous").click(function(e){
		e.preventDefault();
		
		var next = selectedIndex - 1;
		if(next < 0){
			next = images.length - 1;
		}
		selectedIndex = next;
		$(images).removeClass('active');
		var nextImage = $("#photos img:eq(" + next + ")").addClass('active');
		$("#photo_holder").animate({
			opacity: 0
		}, 600, 'swing', function(){
			var height = $(nextImage).height();
			$(images).hide();
			$(nextImage).show();
			$("#photo_holder").animate({
				height: height,
				opacity: 1
			});
		});
		
	});
	
}

function initForms(){
	$('input[title!=""]').hint();
}


$(window).load(function(){
	$("#left_column").imagefit();
});

/* jquery.imagefit 
 *
 * Version 0.2 by Oliver Boermans <http://www.ollicle.com/eg/jquery/imagefit/>
 *
 * Extends jQuery <http://jquery.com>
 *
 */
(function($) {
	$.fn.imagefit = function(options) {
		var fit = {
			all : function(imgs){
				imgs.each(function(){
					fit.one(this);
					})
				},
			one : function(img){
				$(img)
					.width('100%').each(function()
					{
						$(this).height(Math.round(
							$(this).attr('startheight')*($(this).width()/$(this).attr('startwidth')))
						);
					})
				}
		};
		
		this.each(function(){
				var container = this;
				
				// store list of contained images (excluding those in tables)
				var imgs = $('img', container).not($("table img"));
				
				// store initial dimensions on each image 
				imgs.each(function(){
					$(this).attr('startwidth', $(this).width())
						.attr('startheight', $(this).height())
						.css('max-width', $(this).attr('startwidth')+"px");
				
					fit.one(this);
				});
				// Re-adjust when window width is changed
				$(window).bind('resize', function(){
					fit.all(imgs);
				});
			});
		return this;
	};
})(jQuery);

/**
* @author Remy Sharp
* @url http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
*/

(function ($) {

$.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }
    
  return this.each(function () {
    // get jQuery version of 'this'
    var $input = $(this),
    
    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = $(this.form),
      $win = $(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title
      
      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

})(jQuery);

