$(document).ready(function() {

    var cache = new Array();
    preloadImages('images/quick-gallery-button-over-left.png', 'images/quick-gallery-button-over-right.png', 'images/sidebar-option-over-bkg.png');
    
    /*
     * Cufon replacement
     */
    Cufon.replace('#logo h1, #logo h2');

    $('#root .tags a').corner("round 8px");

    /*
     * Search bar initialization
     */

    $('#search-box').focusin(function() {
	if ( $(this).val() == 'Search...' ) {
	    $(this).val('');
	}
    });
    $('#search-box').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Search...');
	}
    });

    /*
     * Menu Initialization
     */
    menuInit();

    /*
     * Slider Initialization
     */
    sliderInit();

    /*
     * Lightbox init
     */
    lightboxInit();


    /*
     * Ajax Image Preload
     */
    ajaxImagePreload();

    /*
     * Quick gallery initialization
     */
    quickGalleryInit();

    /*
     * Post comments
     */
    commentFormInit();

    /*
     * Contact Form fields
     */
    contactFormInit();


    function preloadImages() {
	var img;
	var arg = arguments.length;
	for ( i = 0; i < arg ; i++ ) {
	    img = document.createElement('img');
	    img.src = arguments[i];
	    cache.push(img);
	}
    }

});






function lightboxInit() {
    $('a[rel*=lightbox]').lightBox();


    $('a[rel=lightbox]').hover(
	function() {
	    $(this).find('img').stop().animate({
		opacity: 0.6
	    }, 400);
	},
	function() {
	    $(this).find('img').stop().animate({
		opacity: 1
	    }, 400);
	});
}


function ajaxImagePreload() {
    $('#root div.ajax-preload').each(function() {
	var dims, _w, _h, image, src, preloader;
	dims = $(this).attr('lang');
	dims = dims.split('x');
	src = $(this).attr('title');
	preloader = $(this);

	_w = 1*dims[0];
	_h = 1*dims[1];
	preloader.css({
	    width: _w + 'px',
	    height: _h + 'px'
	});
	if ( preloader.find('a').html() != null ) {
	    preloader.find('a').prepend('<div class="ajax-image-preloader"></div>');
	} else {
	    preloader.prepend('<div class="ajax-image-preloader"></div>');
	}
	preloader.find('.ajax-image-preloader').css({
	    width: _w + 1 + 'px',
	    height: _h + 1 + 'px'
	});
	image = new Image();
	$(image).attr('alt', 'Image');
	$(image).load(function () {
	    if ( preloader.find('a').html() != null ) {
		preloader.find('a').prepend(this);
	    } else {
		preloader.append(this);
	    }
	    preloader.removeAttr('lang').removeAttr('title');
	    preloader.find('.ajax-image-preloader').fadeOut();
	}).attr('src', src);

    });
}


function commentFormInit() {
    $('#comment-name').focusin(function() {
	if ( $(this).val() == 'Name (required)' ) {
	    $(this).val('');
	}
    });
    $('#comment-name').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Name (required)');
	}
    });
    $('#comment-email').focusin(function() {
	if ( $(this).val() == 'Email (required)' ) {
	    $(this).val('');
	}
    });
    $('#comment-email').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Email (required)');
	}
    });
    $('#comment-website').focusin(function() {
	if ( $(this).val() == 'Website' ) {
	    $(this).val('');
	}
    });
    $('#comment-website').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Website');
	}
    });
    $('#comment-message').focusin(function() {
	if ( $(this).val() == 'Message (required)' ) {
	    $(this).val('');
	}
    });
    $('#comment-message').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Message (required)');
	}
    });
}


function contactFormInit() {
    $('#contact-name').focusin(function() {
	if ( $(this).val() == 'Name (required)' ) {
	    $(this).val('');
	}
    });
    $('#contact-name').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Name (required)');
	}
    });
    $('#contact-email').focusin(function() {
	if ( $(this).val() == 'Email (required)' ) {
	    $(this).val('');
	}
    });
    $('#contact-email').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Email (required)');
	}
    });
    $('#contact-subject').focusin(function() {
	if ( $(this).val() == 'Subject (required)' ) {
	    $(this).val('');
	}
    });
    $('#contact-subject').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Subject (required)');
	}
    });
    $('#contact-message').focusin(function() {
	if ( $(this).val() == 'Message (required)' ) {
	    $(this).val('');
	}
    });
    $('#contact-message').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Message (required)');
	}
    });

    $('#send-email-button').click(function() {
	$.post('email.php', {
	    name: $('#contact-name').val(),
	    email: $('#contact-email').val(),
	    subject: $('#contact-subject').val(),
	    message: $('#contact-message').val()
	}, function(data) {
	    var response = '<div class="messages">'+ data +'</div>';
	    $('#contact-form div.messages').remove();
	    $('#contact-form').prepend(response);
	})
	return false;
    });
}


function quickGalleryInit() {

    var gals = $('#content ul.gallery');
    var index = gals.length;
    var selGallery = 0;

    if ( index > 0 ) {

	var navBar = '<ul class="gallery-buttons">';
	gals.each(function(i) {
	    if ( index == gals.length ) {
		navBar += '<li class="on"><a href="#"><span>'+ $(this).attr('title') +'</span></a></li>';
	    } else {
		navBar += '<li><a href="#"><span>'+ $(this).attr('title') +'</span></a></li>';
	    }
	    index--;
	    if ( i == 0 ) {
		$(this).css({
		    display: 'block'
		});
	    }	    
	});
	navBar += '</ul>';
	
	gals.parent().prepend(navBar);

	$('#content ul.gallery-buttons li a').each(function(index) {
	    $(this).click(function() {
		$('#content ul.gallery-buttons li').eq(selGallery).removeAttr('class');
		$('#content ul.gallery-buttons li').eq(index).attr('class', 'on');
		$('#content ul.gallery').eq(selGallery).css({
		    display: 'none'
		});
		$('#content ul.gallery').eq(index).css({
		    display: 'block'
		});
		selGallery = index;
		return false;
	    });
	});
	
    }
}


function sliderInit() {

    if ( window.SLIDER_TYPE == undefined ) {
	SLIDER_TYPE = 1;
    }
    
    $("#transition-slider").transitionSlider({
	slideWidth: 930,
	slideHeight: 290,
	selSlide: 0,
	delay: 10,
	animationStyle : SLIDER_TYPE,
	animationSpeed: 700,
	autoScrollTimer: 10,
	pieceV: 4,
	pieceH: 25,
	hasNavBar: true
    });
}


function menuInit() {
    $('#menu-bar ul.children li').each(function() {
	var elem = $(this).find('ul.children').html()
	if ( elem != null ) {
	    $(this).find('a:first').append(' [+]');
	}
    });
    $('#menu-bar ul.children').css('display', 'none');
    $('#menu-bar ul.options li').hover(
	function() {
	    $(this).find('ul.children:first').stop().css({
		overflow: 'hidden',
		height: 'auto',
		display: 'none'
	    }).slideDown(500, function() {
		$(this).css({
		    overflow: 'visible'
		})
	    });
	},
	function() {
	    $(this).find('ul.children:first').stop().slideUp(250, function() {
		$(this).css({
		    overflow: 'hidden',
		    height: 'auto',
		    display: 'none'
		})
	    });
	} );
}


String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};
