/*
 * MAIN INITIALIZATION
 */
$(document).ready(function() {   


    /*
     * Cufon replacement */
    Cufon.replace('#content h1[class!=simple], #content h2[class!=simple], #content h3[class!=simple], #content h4[class!=simple], #content h5[class!=simple]');
    Cufon.replace('#empty-slider-content h2, #empty-slider-content p, #content a.bubble strong');

    
    /*
     * MENU INITIALIZATION */
    menuInit();

    /*
     *	FRONTPAGE SLIDER INITIALIZATION */    
    sliderInit();

    /*
     * LIGHTBOX INITIALIZATION */
    lightboxInit();


    /*
     * SEARCH BAR INITIALIZATION */
    $('#footer .search-input').focusin(function() {
	if ( $(this).val() == 'Search...' ) {
	    $(this).val('');
	}
    });
    $('#footer .search-input').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Search...');
	}
    });

    /*
     * QUICK TABS */
    quickTabInit();


    /*
     * Post comments
     */
    commentFormInit();


    /*
     * Contact Form fields
     */
    contactFormInit();
    
    
});



function menuInit() {
    /*
    $('ul#menubar ul.children').each(function() {
	$(this).find('li:first a').removeAttr('class').attr('class', 'first');
	if ( $(this).find('ul.children').html() != null ) {
	    $(this).find('ul.children').parent().find('a').removeAttr('class').attr('class', 'last');
	} else {
	    $(this).find('li:last a').removeAttr('class').attr('class', 'last');
	}
    });
    */

    $('ul#menubar ul.children li').each(function() {
	var elem = $(this).find('ul.children').html()
	if ( elem != null ) {
	    $(this).find('a:first').append('&nbsp;&nbsp;&raquo;');
	}
    });
    //    $('ul#menubar ul.children').css('display', 'none');
    $('ul#menubar li').hover(
	function() {
	    var w = (160 - $(this).width())/2 * (-1);
	    $(this).find('ul.children:first').stop().css({
		left: w+'px',
		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'
		})
	    });
	} );
	
//    $('ul#menubar ul.children').corner("round 8px");
//    $('ul#menubar ul.children li a.first').corner("top round 8px");
//    $('ul#menubar ul.children li a.last').corner("bottom round 8px");
}


function sliderInit() {
    $("#accordion-slider").accordionSlider({
	sliderFullWidth: 960,
	slideWidth: 560,
	slideHeight: 350,
	selSlide: 0,
	animSpeed: 1500,
	onEvent: 'mouseover',
	descriptionOpacity: 0.6,
	descriptionAnimSpeed: 1400,
	blurUnselectedSlides: true,
	blockerOpacity: 0.4,
	markedOpacity: 1,
	elasticMode: true,
	slideHasShadow: true,
	markSelected: true,
	animType: 'easeOutExpo'
    });
}


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 quickTabInit() {

    var gals = $('#content ul.tab');
    var index = gals.length;
    var selGallery = 0;

    if ( index > 0 ) {

	var navBar = '<ul class="tabs-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.tabs-buttons li a').each(function(index) {
	    $(this).click(function() {
		$('#content ul.tabs-buttons li').eq(selGallery).removeAttr('class');
		$('#content ul.tabs-buttons li').eq(index).attr('class', 'on');
		$('#content ul.tab').eq(selGallery).css({
		    display: 'none'
		});
		$('#content ul.tab').eq(index).css({
		    display: 'block'
		});
		selGallery = index;
		return false;
	    });
	});
	Cufon.replace('#content ul.tabs-buttons a');
    }
}


function commentFormInit() {
    $('#comment-name').focusin(function() {
	if ( $(this).val() == 'Name *' ) {
	    $(this).val('');
	}
    });
    $('#comment-name').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Name *');
	}
    });
    $('#comment-email').focusin(function() {
	if ( $(this).val() == 'Email *' ) {
	    $(this).val('');
	}
    });
    $('#comment-email').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Email *');
	}
    });
    $('#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 *' ) {
	    $(this).val('');
	}
    });
    $('#comment-message').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Message *');
	}
    });
}



function contactFormInit() {
    $('#contact-name').focusin(function() {
	if ( $(this).val() == 'Name *' ) {
	    $(this).val('');
	}
    });
    $('#contact-name').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Name *');
	}
    });
    $('#contact-email').focusin(function() {
	if ( $(this).val() == 'Email *' ) {
	    $(this).val('');
	}
    });
    $('#contact-email').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Email *');
	}
    });
    $('#contact-subject').focusin(function() {
	if ( $(this).val() == 'Subject *' ) {
	    $(this).val('');
	}
    });
    $('#contact-subject').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Subject *');
	}
    });
    $('#contact-message').focusin(function() {
	if ( $(this).val() == 'Message *' ) {
	    $(this).val('');
	}
    });
    $('#contact-message').focusout(function() {
	if ( $(this).val().trim() == '' ) {
	    $(this).val('Message *');
	}
    });

    $('#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="output-messages">'+ data +'</div>';
	    $('#contact-form div.output-messages').remove();
	    $('#contact-form').prepend(response);
	})
	return false;
    });
}


/*
 * TRIM FUNCTION
 */
String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};