/**
 * JavaScript behaviors for content elements.
 *
 * @copyright  Copyright (c) 2005-2010 Found Line, Inc. (http://www.foundline.com/)
 * @version    $Id: content.js 5 2009-05-30 22:21:15Z bradley.holt $
 */

var headlinesSecond = 0;
var searchText = "enter search terms";
var emailText = "enter email address";
var currentInfoWindow;

$("document").ready(function() {

    if($("input.search").length > 0 && $("input.search").val().length == 0) {
        $("input.search").val(searchText);
    }
    $("input.search").focus(function() {
        if($(this).val() == searchText) {
            $(this).val("");
        }        
    }).blur(function() {
        if($(this).val().length == 0) {
            $(this).val(searchText);
        }    
    });
    
    if($("input#email_address").length > 0 && $("input#email_address").val().length == 0) {
        $("input#email_address").val(emailText);
    }
    $("input#email_address").focus(function() {
        if($(this).val() == emailText) {
            $(this).val("");
        }        
    }).blur(function() {
        if($(this).val().length == 0) {
            $(this).val(emailText);
        }    
    });
    
    $(".section-details").mouseover(function () {
		$(this).addClass("hover");
	});
	$(".section-details").mouseout(function () {
		$(this).removeClass("hover");
	});
    
    $("ul.embed.video li.video").each(function(i) {
        
        var videoUrl = $(this).find("a[rel=video]").attr("href");
        // Since we can't trust mime-type from feeds, we will use the file extension
        var videoUrlExtension = getFileExtension(videoUrl);
        
        var videoThumbUrl = $(this).find("a[rel=video] img").attr("src");
        
        $(this).addClass("current"); // hack to make width and height accessible in IE6
        var width = $(this).find("a[rel=video] img").attr("width");
        var height = $(this).find("a[rel=video] img").attr("height");
        $(this).removeClass("current"); // hack to make width and height accessible in IE6
        
        $(this).empty();
        
        var playerTargetElement = $(this).append("<li></li>")[0];
        
        PokkariPlayerOptions.useShowPlayer = true;
        PokkariPlayerOptions.useDocumentWrite = false;
        PokkariPlayerOptions.maxWidth = 320;
        PokkariPlayerOptions.maxHeight = 265;
        PokkariPlayerOptions.forceAspectWidth = true;
        
        var mimeType;
        
        // Determine player
        var flashPlayerVersion = deconcept.SWFObjectUtil.getPlayerVersion();
        if (flashPlayerVersion.major > 0) {
            var html = "<object width='" + width + "' height='" + (height + 24) + "'>";
            html += "<param name='movie' value='../scripts/flash/player.swf' />";
            html += "<param name='allowfullscreen' value='true' />";
            html += "<param name='allowscriptaccess' value='always' />";
            html += "<param name='wmode' value='transparent' />";
            html += "<param name='flashvars' value='file=" + videoUrl + "&amp;image=" + videoThumbUrl + "' />";
            html += "<embed ";
            html += "type='application/x-shockwave-flash' ";
            html += "src='../scripts/flash/player.swf' ";
            html += "width='" + width + "' ";
            html += "height='" + (height + 24) + "' ";
            html += "allowscriptaccess='always' ";
            html += "allowfullscreen='true' ";
            html += "wmode='transparent' ";
            html += "flashvars='file=" + videoUrl + "&amp;image=" + videoThumbUrl + "' ";
            html += "/>";
            html += "</object>";
            $(this).append().html(html);
            $(this).siblings("li").removeClass("current").end().addClass("current");
        } else if (!(flashPlayerVersion.major > 0) && videoUrlExtension == ".m4v") {
            PokkariPlayerOptions.showPlayerOptions = {
                maxWidth: width,
        		maxHeight: height,
                thumb: videoThumbUrl,
                allowm4v: true,
                smallPlayerMode: true,
                controller: true
            };
            mimeType = 'video/mp4';
            var player = PokkariPlayer.GetInstanceByMimeType(mimeType);
            if (player instanceof PokkariQuicktimePlayer) {
                // Quicktime player will hold up the whole document if a video not marked for streaming is loaded.
                player.setPrimaryMediaUrl(videoUrl);
                player.setThumbnail(videoThumbUrl);
                player.setAutoPlay(false);
            }
            player.setPlayerTarget(playerTargetElement);
            // By setting the size ridiculously large, we'll trick PokkariPlayer in resizing with aspect.
            player.setWidth(480000);
            player.setHeight(270000);
            player.render();
            $(this).removeAttr("style");
            $(this).find("object").attr("width", width).attr("height", height);
            $(this).find("embed").attr("width", width).attr("height", height);
            $(this).siblings("li").removeClass("current").end().addClass("current");
        } else {
            $(this).siblings("li.image.current").find("p.message").text("This video format is not compatible with your browser, plugins and/or settings. Click to view on the source website.");
        }
    });
    
    if (!($.browser.msie)) {
        $(".headlines").addClass("enhanced"); // fixes IE6 bug
        $(".headlines .hentry li.entry-title:first").addClass("current");
        $(".headlines .hentry li.entry-title:not(:first)").hide();
        headlinesPlay();
    }
    
    $(".share").each(function() {
        var shareElement = $(this);
        hideElement(shareElement);
    });
    $(".hentry").hover(function() {
        var shareElement = $(this).find(".share");
        showElement(shareElement);
    }, function() {
        var shareElement = $(this).find(".share");
        hideElement(shareElement);
    });
    
    $.timer(1000, function (timer) {
		if ($(".headlines .hentry").hasClass("loop")) {
			headlinesSecond++;
			if (headlinesSecond >= 6) {
				headlinesSecond = 0;
				headlinesMoveNext();
			}
		}
	});
	
	// GA Event Tracking
    //subscribe newsletter
    $("form[action='/list/list-member']").submit(function() {
        _gaq.push(['_trackEvent', 'newsletter', 'subscribe']);
    });
    //blog post
    $("form[action='http://blog.commongoodvt.org/wp-comments-post.php']").submit(function() {
        _gaq.push(['_trackEvent', 'comment', 'submit']);
    });
    //forum post
    $("form[action='http://forums.commongoodvt.org/bb-post.php']").submit(function() {
        _gaq.push(['_trackEvent', 'post', 'submit']);
    });
    //create account(blog/forum)
    $("form[action='http://forums.commongoodvt.org/register.php']").submit(function() {
        _gaq.push(['_trackEvent', 'account', 'create']);
    });
    //search
    $("form[action='/search']").submit(function() {
        _gaq.push(['_trackEvent', 'search', 'submit']);
    });
    //social
    $(".share a.twitter").click(function() {
        _gaq.push(['_trackEvent', 'share', 'twitter']);
    });
    $(".share a.facebook").click(function() {
        _gaq.push(['_trackEvent', 'share', 'facebook']);
    });
    $(".share a.email").click(function() {
        _gaq.push(['_trackEvent', 'share', 'email']);
    });
    $(".share a.tiny").click(function() {
        _gaq.push(['_trackEvent', 'share', 'short-url']);
    });
    
});

function showElement(element) {
    $(element).fadeTo(100, 1.0);
}
function hideElement(element) {
    $(element).fadeTo(0, 0.5);
}

function headlinesMoveNext() {
	if ($(".headlines .hentry li.entry-title.current").next("li").size() == 0) {
		$(".headlines .hentry li.entry-title").fadeOut(1000).delay(1000).removeClass("current");
		$(".headlines .hentry li.entry-title:first").addClass("current").fadeIn(1000);
	} else {
		$(".headlines .hentry li.entry-title.current").fadeOut(1000).delay(1000).removeClass("current")
		.next("li").addClass("current").delay(1100).fadeIn(1000);
	}
};
function headlinesPlay() {
	headlinesSecond = 0;
	$(".headlines .hentry").addClass("loop");
};
function headlinesPause() {
	$(".headlines .hentry").removeClass("loop");
};

function getFileExtension(filename) { 
    if (filename.length == 0) {
        return ""; 
    }
    var dot = filename.lastIndexOf("."); 
    if (dot == -1) {
        return ""; 
    }
    var extension = filename.substr(dot, filename.length); 
    return extension; 
}
