// Init tc_main_video_player
// Pre-DOM: Load Global resource files/libraries that are NOT dependent on the DOM being ready
// Load js
//init.preload.js('app/video_share.js');
init.preload.js('app/global_share.js');
init.preload.js('app/playlist.js');
init.preload.js('lib/jquery/plugin/jquery.carousel.js');
init.preload.js('app/video-full.js');
init.preload.js('lib/zeroclipboard.js');
init.preload.js('lib/jquery/plugin/jquery.validate.js');
init.preload.js('app/VideoPlayerWidget.js');
init.preload.js('app/social-old.js');

// Load css
init.preload.css('locations.css');
init.preload.css('video.css');
if (browser.msie7) {
  init.preload.css('ie7.css');
} else if (browser.msie6) {
  init.preload.css('ie6.css');
  init.preload.js('lib/supersleight.js'); // IE 6 only
}

var searchVideo = {
    form:"form[id='searchVideoForm']",
    submitSource:"submitButton",
    configure:function() {
	    searchVideo.form.attr('method', 'post');
	    if (env.isBeta() || env.isOriginStg() || env.isQA2() || env.isProd() || env.isStg() || env.isQA() || env.isQprev() || env.isDev() || env.isLocal()) {
		    var url = env.searchUrl() + encodeURIComponent(($("input[id='searchVideo']").val()).replace(/ /g, "-").toLowerCase()) + "/index.html";
		    searchVideo.form.attr('action', url);
	    } else {
		    searchVideo.form.attr('action', env.searchUrl());
	    }
	    return false;
    },
    init:function() {
	    $("input[id='searchVideo']").change(function() {
		    $("input[id='o']").attr('value', 0);
		    $("input[id='p']").attr('value', 1);
		    $("input[id='d']").attr('value', "|4294965789");
		    $("input[id='dn']").attr('value', "|Video");
	    });
	    
	    $("input[id='searchVideo']").keypress(function(e) {
		    if ($.browser.msie) {
			    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
				    this.blur();
				    this.focus();
			    }
		    }
	    });
	    
	    $("input[id='tc_search_again_submit']").keypress(function(e) {
		    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
			    e.preventDefault();
			    searchVideo.form.submit();
			    return false;
		    }
	    });
	    
	    searchVideo.form.submit(function() {

			if (($("#searchVideo").val()).length > 0) {

				if (searchVideo.submitSource === "submitButton") {
					$("input[id='o']").attr('value', 0);
					$("input[id='p']").attr('value', 1);
					searchVideo.configure(searchVideo.form);
				} else {
					if (typeof searchTerm != "undefined" && searchTerm != null) {
						if (searchTerm != $("input[name='q']").val()) {
							$("input[name='q']").attr('value', searchTerm);
						}
					}
					if (searchVideo.submitSource === 'pagination') {
						if (env.isBeta() || env.isOriginStg() || env.isQA2() || env.isProd() || env.isStg() || env.isQA() || env.isQprev() || env.isDev() || env.isLocal()) {
							var url = env.searchUrl() + ($("input[id='tc_search']").val()).replace(/ /g, "-").toLowerCase() + "/page-" + $("input[id='p']").val() + ".html";
							searchVideo.form.attr('action', url);
							searchVideo.form.attr('method', 'post');
						}
					} else {
						searchVideo.configure(searchVideo.form);
					}
				}
				
			} else {
			    return false;
		    }
			
	    });
    }
};

/* 2011-11-18 MLIU initializing */
if (typeof(SNI) == 'undefined') { SNI = {}; }
if (typeof(SNI.Social) == 'undefined') { SNI.Social = {}; }
if (typeof(SNI.Social.FB) == 'undefined') { SNI.Social.FB = {}; }

// Post-DOM: Load all js files/libraries that ARE dependent on the DOM being ready
function moduleReady(){ // include items here that are not Global
    if ($('#sponsor-title.sponsorship-logo').find('a').length > 0) {
        $('#sponsor-title.sponsorship-logo').prepend('Sponsored by');
    }
    
	/* 2011-02-18 MLIU - Javascript to contain Facebook Like button functionality */
	window.fbAsyncInit = function() {
	    SNI.SocialOLD.FB.init();
        init_event_catchers();
	};
	
	$('#email-a-friend').bind('click', function (e) {
       e.stopPropagation();
    });

    $('body').click(function () {
        $('#email-a-friend').fadeOut();
    });
    wireUpShareFormValidation();
  
	(function() {
		var e = document.createElement('script'); e.async = true;
		e.src = document.location.protocol +
		'//connect.facebook.net/en_US/all.js#xfbml=1';
		document.getElementById('fb-root').appendChild(e);
	})();

	init_event_catchers = function() {
		FB.Event.subscribe('edge.create', function(href, widget) {
            omniClickEvent(true, "Video:FB Like:" + $('#auto-player-info h1').text(), "event11");
            SNI.SocialOLD.FB.checkUpdate(href, "tc_video", SNI.SocialOLD.FB.fbID);
		});
		FB.Event.subscribe('edge.remove', function(href, widget) {
			// placeholder
		});
	}
	/* END FB integration */
	
	searchVideo.form = $(searchVideo.form);
	searchVideo.init(); // Initialize the search features

	tc_autocomplete_init("#searchVideo"); // Initialize Endeca autosuggest feature after the DOM is ready

    $('#share-lnk').bind('click', function(e) {
        e.preventDefault();
        e.stopPropagation();
        if ($('#email-a-friend').is(':hidden')) {
            try {
                thisMovie("videoPlayerWidget").pauseVideo();
            } catch (e) {}
            $('#email-a-friend').show();
        } else {
    	    $('#email-a-friend').fadeOut();
    	    try {
    	        thisMovie("videoPlayerWidget").playVid();
	        } catch(e) {}
        }
    });

	$('#email-a-friend .hd .close, #email-a-friend .form-submit a').bind('click', function(e) {
	    e.preventDefault();
	    $('#email-a-friend').fadeOut();
	    try {
            thisMovie("videoPlayerWidget").playVid();
        } catch (e) {}
	});

}
