function mycarousel_initCallback(carousel) {
    jQuery('.template-preview .nav a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
};

jQuery.noConflict();
jQuery(document).ready(function($){
	$('.blink').
	    focus(function() {
	        if(this.title==this.value) {
	            this.value = '';
	        }
	    }).
	    blur(function(){
	        if(this.value=='') {
	            this.value = this.title;
	        }
	    });

	$('a.img').hover(
		function(){ $(this).addClass('img-hover'); },
		function(){ $(this).removeClass('img-hover'); }
	);	
	
	$('#landing .search .submit').hover(
		function(){ $(this).addClass('submit-hover'); },
		function(){ $(this).removeClass('submit-hover'); }
	);
	
	$('#landing .search .field').
	focus(function(){
		$(this).parents('.search:eq(0)').addClass('search-focus');
	}).
	blur(function(){
		$(this).parents('.search:eq(0)').removeClass('search-focus');
	});
	
	$('.page-heading .search .field input').
	focus(function(){
		$(this).parents('.search').addClass('search-focus');
	}).
	blur(function(){
		$(this).parents('.search').removeClass('search-focus');
	});
	
	
	$('.page-heading .search .submit').hover(
		function(){$(this).addClass('submit-hover'); },
		function(){$(this).removeClass('submit-hover'); }
	);
	
	$('a[rel*=facebox]').facebox();
	
	$('#contact-link').live('click', function(){
		$('#footer .contact-us-link').trigger('click');
		return false;
	});
	
	$('#facebox .close, .cancel-button').live('click', function() {
		$(document).trigger('close.facebox');
		return false;
	});	
	
	$('#facebox .submit-button').live('click', function() {
		$(this).parents('form').submit();
		return false;
	});
	
	$('.template-preview .images ul').jcarousel({
		scroll: 1,
		visible: 1,
		wrap: "both",
		initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
	});
	
	$('.template-preview .nav a').click(
		function(){
			$('.template-preview .nav a').removeClass('active');
			$(this).addClass('active');
			return false; 
		}
	);
	
	$('.paging .pages').each(function(){
		var width = $(this).outerWidth();
		
		$(this).css({
			'width' : width,
			'float' : 'none',
			'display' : 'block' 
		});
	});
	
	$('.nav-links > ul > li').hover(
		function() {
			$(this).addClass('hover');
			$(this).find('.dd').show();
		},
		function() {
			$(this).removeClass('hover');
			$(this).find('.dd').hide();
		}
	);
	
	$('.download-button').live('click', function(e) {
		var datastring = 'count_download=' + $(this).attr('rev');
		var th = $(this);
		$.ajax({
			type: "GET",
			url: $(this).attr('rel'),
			data: datastring,
			success: function() {
				window.location.href = th.attr('href');
			}
		});
		return false;
	});
	$('.purchase-button').live('click', function(e) {
		var url = $(this).attr('href').replace(/(\?.*)/, '');
		var datastring = $(this).attr('href').replace(/([^?]*\?)/, '?');
		$.ajax({
			type: "GET",
			url: url,
			data: datastring,
			success: function() {
				jQuery('#paypal-btn').submit();
			}
		});
		return false;
	});
});
