/*--------------------------------------------------------------------
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element
 		and sets their min-height to the tallest height (or width to widest width). Sets in em units
 		by default if pxToEm() method is available.
 * Dependencies: jQuery library, pxToEm method	(article:
		http://www.filamentgroup.com/lab/retaining_scalable_interfaces_with_pixel_to_em_conversion/)
 * Usage Example: $(element).equalHeights();
  		Optional: to set min-height in px, pass a true argument: $(element).equalHeights(true);
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		//if (!px || !Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest});
	});
	return this;
};


/*
 *  jQuery Dynamic Background Image Plugin
 *  Requires jQuery v1.2.6 or later
 *
 *  Copyright (c) Thomas Bindzus
 *  (http://bindzus.wordpress.com)
 *
 *  Dual licensed under the MIT and GPL licenses:
 *  http://www.opensource.org/licenses/mit-license.php
 *  http://www.gnu.org/licenses/gpl.html
 *
 *  Version: 1.0
 */
(function($)
{
   $.fn.fitToBackgroundImage = function()
   {
      // Helper function to extract the URL from CSS
      function cleanupUrl(aUrl)
      {
         var regExp = /^url\("?(.*?)"?\)$/ig;
         var matches = regExp.exec(aUrl);

         if(matches == null || typeof matches[1] == "undefined")
            return null;

         return matches[1];
      }

      // Returning the updated HTML elements in a
      // way that allows for other jQuery methods
      // to manipulate them further
      return this.each
      (function(index)
      {
         // Ensure we get a reference to the element
         // which we can use closure on
         var $this = $(this);

         // Get the baground image using jQuery,
         // no need to worry about computed style
         // because jQuery will handle that part
         var backgroundImageUrl = cleanupUrl($this.css("background-image"));

         // If no background image is set then return
         if(backgroundImageUrl == null)
            return;

         // Create a dummy image to get the dimensions
         // of the image
         var dummyImage = document.createElement("img");

         // Get the URL for the background image
         dummyImage.src = backgroundImageUrl;

         // When the image is loaded then
         // set the dimensions
         dummyImage.onload = function()
         {
            // Thanks to JavaScript closure we can access
            // the elt variable from the onload method
            $this.css("width", this.width+"px")
                 .css("height", this.height+"px");
         };

         // Cleanup
         dummyImage = null;
      });
   }
}) (jQuery);


s = new settings();
function clever(){
	this.init = function (){
		$('body').addClass('js');
	}
	this.add_corners = function(){
		var c1= $('<div />',{'class':'corners left_top'});
		var c2= $('<div />',{'class':'corners right_top'});
		var c3= $('<div />',{'class':'corners left_bottom'});
		var c4= $('<div />',{'class':'corners right_bottom'});
		$('#content').append(c1,c2,c3,c4);
		$('.right-image').append($(c1).clone(),$(c2).clone(),$(c3).clone(),$(c4).clone());
		$('#art_gallery li .add_corners').append($(c1).clone(),$(c2).clone(),$(c3).clone(),$(c4).clone());
		$('#image').append($(c1).clone(),$(c2).clone(),$(c3).clone(),$(c4).clone());
		$('#images li').append($(c1).clone(),$(c2).clone(),$(c3).clone(),$(c4).clone());
		$('.prod_image').append($(c1).clone(),$(c2).clone(),$(c3).clone(),$(c4).clone());
		$('#footer li').append($(c1).clone(),$(c2).clone(),$(c3).clone(),$(c4).clone());
		$('#footer_boxes .box_area').append($(c1).clone(),$(c2).clone(),$(c3).clone(),$(c4).clone());
		$('#cform input[type=text], #cform textarea').each(function(){
			var parent = $(this).parent();
			var width = $(this).outerWidth();
			var height = $(this).outerHeight();
			var position = $(this).position();
			var ca1 = $(c1).clone();
			var ca2 = $(c2).clone();
			var ca3 = $(c3).clone();
			var ca4 = $(c4).clone();
			$(parent).append(ca1, ca2, ca3, ca4);
			$(ca1).css({'top':position.top, 'left':position.left});
			$(ca2).css({'top':position.top, 'left':position.left+width-4});
			$(ca3).css({'top':position.top+height-4, 'left':position.left});
			$(ca4).css({'top':position.top+height-4, 'left':position.left+width-4});
		});
	}
	this.fancy_box = function(){
		$("a[rel=lightbox]").fancybox({
			'overlayOpacity' : 0.6,
			'overlayColor' : '#000',
			'scrolling' : false,
			'autoScale':false
		});
	}
	this.lazy_load_images = function(){
		$("#art_gallery li img").lazyload({
	    effect : "fadeIn"
		});
	},
	this.faq_definitions = function(){
		$('#faq #content dt').click(function(){
			$(this).toggleClass('active');
			$(this).next().slideToggle('slow');
		});
	}
	this.carousel = function(){
	   $('#images').jcarousel({
       start: typeof(s.gallery_start!='undefined')?s.gallery_start:0
   });
	   //jQuery('#images').jcarousel('scroll',2);
	}
	this.ajax_carousel = function(){
		$('#next a:first-child, #prev a:first-child').live('click',function(e){
			e.preventDefault();
			var url = $(this).attr('href');
			$.get(url,function(data){
				$('#image').html($(data).find('#image').html());
				$('#next').html($(data).find('#next').html());
				$('#prev').html($(data).find('#prev').html());
				$("#image_title").html($(data).find('#image_title').html());
				$('#images').html($(data).find('#images').html());
			   	$('#images').jcarousel({
			   		start:$('#images li').index($('#images li.active'))+1
			   });
				var c1= $('<div />',{'class':'corners left_top'});
				var c2= $('<div />',{'class':'corners right_top'});
				var c3= $('<div />',{'class':'corners left_bottom'});
				var c4= $('<div />',{'class':'corners right_bottom'});
				$('#image').append($(c1).clone(),$(c2).clone(),$(c3).clone(),$(c4).clone());
				$('#images li').append($(c1).clone(),$(c2).clone(),$(c3).clone(),$(c4).clone());
				$('#image img').load(function(){
					$('#image').animate({
							height: $('#image img:first-child').height(),
							'minHeight': $('#image img:first-child').height()
						});
					}
				);
			});
		});
	}
	this.focus_form_fields = function(){
		$('#cform input, #cform textarea').focus(function(){$(this).addClass('focus');});
		$('#cform input, #cform textarea').blur(function(){$(this).removeClass('focus');});
	}
	this.footer_boxes = function(){
		$('#footer ul').equalHeights();
		$("#footer ul h5 a").bigTarget();
		$("#footer ul li").click(function(){
			$(this).addClass('hover');
		});
	}
	this.index_cycle = function(){
		if ($('body#index').length){
			var div = $('<div/>',{'id':'cycle_container'});
			$('#content').append(div);
			$.get("/pl/index_ajax_load",function(data){
				$('#content #cycle_container').append($('.cycle'));
				$('#content #cycle_container').append(data);
				$('#cycle_container').cycle({
					height:$('.cycle:first').outerHeight(),
					timeout:6000
				});
				$("#content .cycle h3 a").bigTarget({
					clickZone : 'div:eq(0)'
				});
				$("#cycle_container .cycle").fitToBackgroundImage();
				$('#cycle_container').equalHeights();
			});
		}
	}
}
site = new clever();
$(document).ready(function(){
	site.init();
	site.index_cycle();
	site.fancy_box();
	site.lazy_load_images();
	site.add_corners();
	site.faq_definitions();
	site.carousel();
	site.focus_form_fields();
	site.footer_boxes();
	site.ajax_carousel();
});
