// Make console.log not crash IE
if ( !window.console ) {
    window.console = { log: function() {} }
}

function record_sharing(channel) {

    var s = window.location.search

    var user_id = window.hashed_id
    if (user_id == undefined) {
        var match   = /id=(\d*-\d+-.+(:?&|$))/.exec(s)
        if ( match && match.length ) { user_id = match[1] }
    }

    var video = window.bv_id
    if (video == undefined) {
        match = /b=(.+?)(:?&|$)/.exec(s)
        if ( match && match.length ) { video = match[1] }
        else { return }
    }

    // Use the query string since everthing is already URI escaped
    $.post( 'record.pl?b=' + video + '&id=' + user_id + '&channel=' + channel )

    return 
}

function trigger_share_dialog(channel) {
    if (channel == undefined) { channel = 'fb_share_popup' }

    if (FB.Connect) {
        FB.Connect.showShareDialog(
            'http://beck.cnnbcvideo.com/?rc=fb&fb=1&fv=' + window.bv_id,
            function(post, exception) {
                if (post_id && post_id != "null") { 
                    record_sharing(channel)
                }
            }
        )
    }
    else {
        record_sharing('fb_share_direct')
    }

    return false
}

function examplify() {
	$('input[type="text"]').example(function() {
		return $(this).attr('title');
	});
}

$(document).ready(function() {
	var taf_rows = 5;

    examplify();

	$('#ViralVideo').hide();

	$("#dialog").dialog({
		bgiframe: true,
		resizable: false,
		height:262,
		width: 600,
		modal: true,
		autoOpen: false,
		draggable: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.3
		}
	});

	$("#dialog_nofb").dialog({
		bgiframe: true,
		resizable: false,
		height:262,
		width: 600,
		modal: true,
		autoOpen: false,
		draggable: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.3
		}
	});

	$("#dialog_fb").dialog({
		bgiframe: true,
		resizable: false,
        height: 342,
		width: 600,
		modal: true,
		autoOpen: false,
		draggable: false,
		overlay: {
			backgroundColor: '#000',
			opacity: 0.3
		}
	});

    /*
	$('a#fb_share').click(function() { 
		$('#dialog_fb').dialog('open'); 
		$('input,textarea,select,a').blur();
		return false; 
	});
    */

	$('a#fb_share_direct').click(function() { 
		window.open('http://www.facebook.com', "NewWin", "width=800,height=600");
		$('input,textarea,select,a').blur();
		return false; 
	});

	$('a#dialog_nofb_button').click(function() { 
		$('#dialog').dialog('close'); 
		if (name_hash && bv_id) {
			$.post('name.pl', {"b" : bv_id}, launch_normal_video, "json");
		} else if (name_hash && first_name && last_name) {
			$.post('name.pl', {"first_name": first_name, "last_name": last_name}, launch_normal_video, "json");
		} else {
			$('#dialog_nofb').dialog('open'); 
		        $.post( 'saw_second_shadow.pl' );
			$('input,textarea,select,a').blur();
		}
		return false; 
	});

	$('a#nofb_complete_button').click(function() { 
		$.post('name.pl', $('#nameonly').serialize(), launch_normal_video, "json");
		$('#dialog').dialog('close'); 
		return false; 
	});
	
	$('a#nofb_cancel_button').click(function() { 
		var data = {"first_name": "Stephen", "last_name": "Colbert", "bv_id": bv_id};
		launch_normal_video(data);
		$('#dialog').dialog('close'); 
		return false; 
	});
    
    /*
	$('a#fb_complete_button').click(function() { 
		$('#dialog_fb').dialog('close'); 
		return false; 
	});
	
	$('a#fb_cancel_button').click(function() { 
		$('#dialog_fb').dialog('close'); 
		return false; 
	});
    */
	
	$('a.embed_form_button').click(function() { 
		$('#embed_form').slideDown();
		return false;
	});
	
    $("#generate_embed_code").click(function(e) {

        form = $('#embed_form form') 

        fields = [ 'first_name', 'last_name', 'city', 'employer' ]
        error = false
        for (var i=0; i<fields.length; ++i) {
          if ( !form[0][fields[i]].value ) {
              $('#embed_form label[for="' + 
                fields[i] + 
                '"]').addClass('l-error')
              error = true
          }
        }

        if (!error) {
            $.post(form.attr('action'), form.serialize(), function(data) {
                $('.generated_embed_code').html(data)
            })
        }

        return false
    })

	$(".add_more_friends").click(function() {
		for (i=0; i<5; i++) {
	  		taf_rows++;
			
			$('#email_friends').append($('#email_friends tr:last').clone());
			$('#email_friends tr:last').attr('class', (taf_rows % 2 ? 'even' : 'odd'));
			$('#email_friends tr:last input').each(function(index, input) {
				$(input).attr('name', input.name.replace(/.$/, taf_rows));
                $(input).attr('value', '');
                $(input).example(function() {
                    return $(this).attr('title');
                });
            });
		}
	});

    $('a.gototaf').click(function (e) {
        var link = e.target;
        var target = link.href
        if (/#$/.test(target)) {
            target = window.taf_link;
        }
        if (!/b=b/.test(target)) {
            target = add_b(target);
        }
        window.location = target
        return false;
    })

});

function add_b(target) {
    if (window.bv_id) {
        target = target
                + (target.search('\\?') > -1 ? '&' : '?')
                + 'b='+window.bv_id; 
    }
    return target
}


