var homeTimer;

function startAnimate() {
	homeTimer = setTimeout('homeAnimate()', 10000);
}

function homeAnimate() {
	jQuery('.home-ctrl .right').click();
	homeTimer = setTimeout('homeAnimate()', 10000);
}

if ( ! Array.indexOf) {
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}
	
jQuery(function($) {

	/***********************************************
	 * Home Feature
	 ***********************************************/	
	$('.home-feature li:gt(0)').hide();

	$('.home-ctrl .left, .home-ctrl .right').click(function(e) {
		e.preventDefault();
	    var currentFeature = $('.home-feature li:visible');

	    var count =  $('.home-feature li').length;
	    var index = currentFeature.prevAll().length;

	    $(this).attr('class') === 'right' ? index++ : index--;

	    if (index === -1) { index = count - 1 }
	    if (index === count) { index = 0 }

	    currentFeature.hide();

	    $('.home-feature li:eq(' + index + ')').fadeIn(500);
	});
	
	startAnimate();
    
	$('.home-ctrl .pause').live('click', function() {
		$(this).removeClass('pause').addClass('play');
		clearTimeout(homeTimer);
	});

	$('.home-ctrl .play').live('click', function() {
		$(this).removeClass('play').addClass('pause');
	    startAnimate();
	});
	
	/***********************************************
	 * Gallery
	 ***********************************************/
	var gallery = $('#gallery');
	//gallery.find('.panel').not(':first').css({visibility:'hidden'});
    tabs = gallery.find('.vtabs');
    
	$('#gallery .vtabs a').click(function(e) {
	    var currentTab = $(this).parents('ul.vtabs').find('li.active');
	    currentTab.removeClass('active');
	    var currentIndex = currentTab.prevAll().length;
	    $('#gallery .panel:eq(' + currentIndex + ')').css({visibility:'hidden', zIndex:0, position:'absolute'});
	    
	    var tab = $(this).parents('li:first');
	    var index = tab.prevAll().length;
	    
	    tab.addClass('active');
	    var panel = $('#gallery .panel:eq(' + index + ')');
	    panel.css({visibility:'visible', zIndex:500, position:'relative'});

        var slug = $(this).html().toLowerCase().replace(' ', '-');

        var tiles = panel.find('div.tiles');
        
        if (tiles.html() === '') {
    	    tiles.load(this.href, function() {
                var slider = tiles.find('ul');
                var num_tiles = slider.find('li').length;
                var incr = slider.find('li:first').outerWidth(true);
                var width = num_tiles * incr;
                
                slider.find('a').each(function() {
                    $(this).attr('rel', slug);
                });
                slider.find('a').fancybox({titlePosition:'inside'});

                if (num_tiles > 3) {
                    slider.css({width:width + 'px'});
                    tiles.append('<a href="javascript:;" class="tiles-left"></a>');
                    tiles.append('<a href="javascript:;" class="tiles-right"></a>');
                    tiles.find('.tiles-left').click(function() {
                        if (parseInt(slider.css('marginLeft')) < 0)
                            slider.not(':animated').animate({marginLeft:'+=' + incr + 'px'});
                        return false;
                    });
                    tiles.find('.tiles-right').click(function() {
                        if (Math.abs(parseInt(slider.css('marginLeft'))) < (width - (3 * incr)))
                            slider.not(':animated').animate({marginLeft:'-=' + incr + 'px'});
                        else
                            slider.not(':animated').animate({marginLeft:0});
                        return false;
                    });
                }
                
                var terms = tiles.find('ul a').map(function() {
                    var $a = $(this);
                    var $img = $a.find('> img');
                    if ($img.attr('alt'))
                        return $img.attr('alt').toLowerCase();
                    else if ($img.attr('title'))
                        return $img.attr('title').toLowerCase();
                    else if ($a.attr('title'))
                        return $a.attr('title').toLowerCase();
                    
                    return '';
                }).get();

            	panel.find('.panel-search').autocomplete({
            		source: terms,
            		select: function(event, ui) {
            		    tiles.find('a:eq(' + terms.indexOf(ui.item.value) + ')').click();
            		}
            	});
                
                
            });
        }

        return false;        
	});

    $(function() {
        $(window).bind('load', function() {
        	$('#gallery .vtabs a:first').click();
        });
    });

    $('.panel-search').val('Search this category');
    $('.panel-search').click(function() {
        if (this.value === 'Search this category') this.value = '';
    });
    $('.panel-search').blur(function() {
        if (this.value === '') this.value = 'Search this category';
    });
    
	/***********************************************
	 * Contact Form
	 ***********************************************/
	$('#contact-form input, #contact-form textarea').each(function() {
		var field = $(this);
		var title = field.attr('title');
		if ( ! title) return false;

		if (field.val().length == 0) field.val(title);
		field.click(function() {
			var el = $(this);
			if (el.val() == el.attr('title')) el.val('');
		});
		field.blur(function () {
			var el = $(this);
			if (el.val().length == 0) field.val(el.attr('title'));
		})
	});
    
});
