$(document).ready(function() {
    lightbox();
    htcIEFix();
    cufonReplace();
    searchBoxSwitch();
    newsletterSwitch();
    startTabs();
    toolTips();
    if($(".flash").length > 0){
        fixFlash(".flash");
    }
    if(typeof bindImages == "function"){
        bindImages();
    }
    if(typeof bindGallery == "function"){
        bindGallery();
    }
});

function cufonReplace(){
    Cufon.replace('.cufon', {
        fontWeight: 'normal',
        fontSize: '21px',
        letterSpacing: '3px',
        textTransform: 'uppercase'
    });
    Cufon.replace('.cufon-main', {
        fontWeight: 'normal',
        fontSize: '30px',
        letterSpacing: '3px',
        fontStyle: 'italic',
        textTransform: 'uppercase'
    });
}

function lightbox(){
    if($('.cms-gallery a').length > 0){
        $('.cms-gallery a').lightBox();
    }
}

function searchBoxSwitch(){
    if($('#event-search').length > 0){
        $('#event-search').bind('click', function(){
            if($('#event-search').val() == "Enter Keyword"){
                $('#event-search').val("");
            }
        });
        $('#event-search').bind('blur', function(){
            if($('#event-search').val() == ""){
                $('#event-search').val("Enter Keyword");
            }
        });
    }
}

function newsletterSwitch(){
    var fieldName = {
        "newsletter_first_name":"First Name",
        "newsletter_last_name":"Last Name",
        "newsletter_email":"E-mail"
    };
    
    for(var i in fieldName){
        $('#'+i).bind('click', function(){
            if($(this).val() == fieldName[$(this).attr("id")]){
                $(this).val("");
            }
        });
        $('#'+i).bind('blur', function(){
            if($(this).val() == ""){
                $(this).val(fieldName[$(this).attr("id")]);
            }
        });
    }
}

function startTabs(){

    //Events
    if($('.tabs').length > 0){
        $('.tabs').tabs();
    }

    //Home
    if($('#wide .tabs').length > 0){
        var $tabs = $('#wide .tabs').tabs().tabs('rotate', 10000);
        $('#wide .ui-tabs-panel').mouseover(function() {
            $tabs.tabs('rotate', false);
        });
        $('#wide .ui-tabs-panel').mouseout(function() {
            $tabs.tabs('rotate', 10000);
        });
    }
    
    if($('#narrow .tabs').length > 0){
        $('#narrow .tabs').tabs();
    }
}

function htcIEFix(){
    $('#header ul li').hover(function() {
        $(this).addClass('hover');
    },
    function() {
        $(this).removeClass('hover');
    }
    );
}

function toolTips(){
    if($("#portfolio-thumbs-lrg ul li a").length > 0){
        $("#portfolio-thumbs-lrg ul li a").tooltip({
        track: true, 
    	delay: 250,
	opacity: 1,
    	showURL: false,
    	showBody: " - ",
    	top: -90, 
    	left: 0,
		positionLeft: true,
	fade: 150
        });
    }
    if($("#narrow #p_sub-items ul li a").length > 0){
        $("#narrow #p_sub-items ul li a").tooltip({
        track: true,
    	delay: 250,
	opacity: 1,
    	showURL: false,
    	showBody: " - ",
    	top: -90,
    	left: 0,
		positionLeft: true,
	fade: 150
        });
    }
}

function fixFlash(selector){
    var foundFlash = false;
    $(selector).each(function(){
		/*
        //Init jQ Flash
        var src = $("object param[name='movie']",this).val();

        //Get height and width
        var width = $('object', this).attr("width");
        var height = $('object', this).attr("height");

        //Reset the div
        $(this).html("");

        //Add Flash
        $(this).flash({
             "src" : src,
             "width" : width,
             "height" : height,
             "wmode" : "transparent"
        });*/

        foundFlash = true;
    });

    return foundFlash;
}
