﻿$(window).load(function() {
	$('div.accessories > ul').each(function() {
        var $sameHeightChildren = $(this).find('li');
        var maxHeight = 0;
        $sameHeightChildren.each(function() {
            maxHeight = Math.max(maxHeight, $(this).outerHeight());
        });
        $sameHeightChildren.css({ minHeight: maxHeight + 'px' });
    });
	
	if ($("div.carousel-th ul").length>0) {
		$("div.carousel-th ul").carousel({ visible:3 });
	}	
});

$(window).resize(function() {
	$('div.accessories > ul').each(function() {
        var $sameHeightChildren = $(this).find('li');
        var maxHeight = 0;
        $sameHeightChildren.each(function() {
            maxHeight = Math.max(maxHeight, $(this).outerHeight());
        });
        $sameHeightChildren.css({ minHeight: maxHeight + 'px' });
    });
});

$(document).ready(function() {
	$('input[type=text]').bind('focus blur',function(){clearText(this)});

	if ($('a[rel="lightbox"]').length>0){
		$('a[rel="lightbox"]').fancybox({ titlePosition: 'inside' });
	}
	
});
function clearText(field){if (field.defaultValue == field.value) field.value = '';else if (field.value == '') field.value = field.defaultValue;}


var text_content, current_size, target_size, size_unit;

(function($){
 	$.fn.extend({ 
 		carousel: function(options) {
			var defaults = {
				easing: 'easeInOutQuad',
				speed: 500,
				visible: 3
			}
			var options =  $.extend(defaults, options);

    		return this.each(function() {
				var o = options;
				var el = $(this);
				// alert(el);
				var box = $(el).parent();
				var wrapper=$(box).parent();
				var items = $("li", el);
				var itemWidth = $(items[0]).outerWidth(true);
				var pages = Math.ceil(items.length / o.visible);
				var currentPage = 0;
				// add some empty LIs to complete the page
				for(var i=0; i<(pages*o.visible)-items.length; i++){
					$(el).append('<li class="empty">&nbsp;</li>');
				}
				// count again
				items = $("li", el);
				// put them in line
				 
				el.css({
					overflow: 'hidden',
					width: itemWidth * items.length
				});

				// build the pager
				// if (pages>1) { // if needed
					//$(wrapper).append('<div class="hr" style="display:block"><hr /></div>');
					//$(wrapper).append('<ul class="pages"></ul>');

					// var pager=$(wrapper).children('.pages').css('display', 'block');
					// for(i=0; i<pages; i++){
						// $(pager).append('<li class="page_'+ i +'"><a href="#"></a></li>');
					// }

					// var bullets=$(pager).children();
					// bullets.filter('.page_'+currentPage).addClass('current');
					// bullets.click(function(e){
						// bullets.removeClass('current');
						// $(this).addClass('current');

						// if(bullets.index(this) > currentPage) {
							// currentPage=bullets.index(this)-1;
							// right.click();
						// } else if(bullets.index(this) < currentPage) {
							// currentPage=bullets.index(this)+1;
							// left.click();
						// }
						// return false;
					// });
				// }

				// build the arrow controls
				$(wrapper).append('<span class="previous"><a>&nbsp;</a></span>');
				$(wrapper).append('<span class="next"><a>&nbsp;</a></span>');

				var left=$(box).siblings('.previous').css('display', 'block').css('top', $(box).position().top+30).addClass('disabled');
				var right=$(box).siblings('.next').css('display', 'block').css('top', $(box).position().top+30);
				if (pages<2) { $(right).addClass('disabled'); }

				// add events
				left.click(function(){
					if (currentPage>0) {
						currentPage--;
						$(el).animate({ 'margin-left': 0-currentPage*(itemWidth * o.visible)   }, o.speed, o.easing);
						$(right).removeClass('disabled');
						// bullets.removeClass('current').filter('.page_'+currentPage).addClass('current');
						if(currentPage==0){
							$(this).addClass('disabled');
						}
					}
				});
				right.click(function(){
					if (currentPage<(pages-1)) {
						currentPage++;
						// alert((0-currentPage*(itemWidth * o.visible))-10);
						//alert(0-currentPage*(itemWidth * o.visible)-(currentPage*13));
						$(el).animate({ 'margin-left': (0-currentPage*(itemWidth * o.visible) ) }, o.speed, o.easing);
						$(left).removeClass('disabled');
						// bullets.removeClass('current').filter('.page_'+currentPage).addClass('current');
						if(currentPage==(pages-1)){
							$(this).addClass('disabled');
						}
					}
				});
			});
    	}
	});
})(jQuery);


