﻿var ipad_orient

function ChooseWork(type)
{
    if(type == "")
        location.href = "archive.aspx";
    else
        location.href = "archive.aspx?type="+type;
}

function LoadFeatureDetail(project_id, page_url, width, height, sections)
{
    RedirectPage(page_url+"?id="+project_id+"&width="+width+"&height="+height+"&sections="+sections+"&type=f");

}

function InitPrompt(p_width, p_height)
{
    var winWidth = $(window).width();
    var winHeight = $(window).height();
    var promptWidth = eval(p_width);
    var promptHeight = eval(p_height);
    
    var posLeft = Math.floor((winWidth - promptWidth)/2);
    var posTop = Math.floor((winHeight - promptHeight) / 2);
    
    if($.browser.opera || $.browser.safari)
        posTop = posTop - 60;
    
    if(posLeft <0) posLeft = 0;
    if(posTop <0) posTop = 0;

    $("#PromptContainer").css({"position":"absolute","top":""+posTop+"px","left":""+posLeft+"px"});
    
    //Add Opaque div
    $("body").append("<div id='opaque' style='display: none;' onclick='hidePromptNoEffect()'></div>");
    $("body").append("<div id='opaque_1' style='display: none;' onclick='hidePromptNoEffect()'></div>");
}

function hidePromptNoEffect()
{
    $("#opaque").hide();
    $("#opaque_1").hide();
    $(".prompt_div").hide();
    document.getElementById("content_div").innerHTML = "";
}

function ShowVideoPrompt(video_url, video_width, video_height, video_desc)
{
    
    var p_width = eval(video_width) + 44;
    var p_height = eval(video_height) + 70;
    
    if($.browser.msie)
        p_height = p_height+16;
    
    InitPrompt(p_width, p_height);
    document.getElementById("PromptContainer").innerHTML = "<div id='content_div' class='prompt_div'></div>";
    $(".prompt_div").css({"margin": "0px", "width":""+p_width+"px", "height":""+p_height+"px", "background-color":"#3b3b3b", "position":"fixed", "z-index":"1000"});
    
    var url = window.location.href;
    var type = getURLParam("type",url);
    if(type == "a")
        $("#opaque_1").show();
    else
        $("#opaque").show();
        
    $(".prompt_div").show();
    var str = "";
    
    str += "<div style=\"padding-left:22px; padding-top:24px\">";
    str += "<div style=\"position:absolute; top:-12px; right:-12px\"><img src=\"images/button/prompt_close.png\" onmouseover=\"mOver(this)\" onmouseout=\"mOut(this)\" onclick=\"hidePromptNoEffect()\" /></div>";
    str += "<div class=\"video-js-box\">";

    var uagent = navigator.userAgent.toLowerCase();

    if (uagent.search("mac os x 10_7_2") != -1 && uagent.search("chrome") != -1) {
        var qt_height = eval(video_height) + 16;
        str += "<object width=\"" + video_width + "\" height=\"" + qt_height + "\" classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\">";
        str += "<param name=\"src\" value=\"http://www.farmcp.com/videos/" + video_url + ".mp4\">";
        str += "<param name=\"autoplay\" value=\"true\">";
        str += "<param name=\"controller\" value=\"true\">";
        str += "<embed src=\"http://www.farmcp.com/videos/" + video_url + ".mp4\" width=\"" + video_width + "\" height=\"" + qt_height + "\" scale=\"tofit\" autoplay=\"true\" controller=\"true\" pluginspage=\"http://www.apple.com/quicktime/download/\"></embed>";
        str += "</object>";
    }
    else {

        if ($.browser.safari && (uagent.search("chrome") == -1) && !detectQTSafari()) {
            var text_m_top = Math.round(eval(video_height) / 2) - 15;
            str += ShowInstallQT(text_m_top);
        }
        else {
            str += "<video class=\"video-js\" width=\"" + video_width + "\" height=\"" + video_height + "\" controls=\"controls\" preload=\"auto\" autoplay=\"autoplay\" >";
            str += "<source src=\"http://www.farmcp.com/videos/" + video_url + ".mp4\" type=\"video/mp4\" />";
            str += "<source src=\"http://www.farmcp.com/videos/" + video_url + ".webm\" type=\"video/webm\" />";


            var qt_height = eval(video_height) + 16;

            if ($.browser.msie && eval(jQuery.browser.version) < 9) {
                if (detectQuickTime()) {
                    str += "<object width=\"" + video_width + "\" height=\"" + qt_height + "\" classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\">";
                    str += "<param name=\"src\" value=\"http://www.farmcp.com/videos/" + video_url + ".mp4\">";
                    str += "<param name=\"autoplay\" value=\"true\">";
                    str += "<param name=\"controller\" value=\"true\">";
                    str += "<embed src=\"http://www.farmcp.com/videos/" + video_url + ".mp4\" width=\"" + video_width + "\" height=\"" + qt_height + "\" scale=\"tofit\" autoplay=\"true\" controller=\"true\" pluginspage=\"http://www.apple.com/quicktime/download/\"></embed>";
                    str += "</object>";
                }
                else {
                    var text_m_top = Math.round(qt_height / 2) - 15;
                    str += ShowInstallQT(text_m_top);

                }
            }

            str += "</video>";
        }
    }
    
    str += "</div>";
    if (uagent.search("mac os x 10_7_2") != -1 && uagent.search("chrome") != -1) {
        str += "<div style=\"position:absolute; bottom:8px; left:23px;\" class=\"prompt_text\">" + video_desc + "</div>";
    }
    else {
        str += "<div style=\"position:absolute; bottom:18px; left:23px;\" class=\"prompt_text\">" + video_desc + "</div>";
    }
    str += "</div>";
    
    document.getElementById("content_div").innerHTML = str;
    if(!($.browser.msie && eval(jQuery.browser.version)<9))
        VideoJS.setupAllWhenReady();
}

function ShowInstallQT(text_m_top)
{
    var str = "";
    str += "<div align=\"center\" style=\"font-family:Helvetica; font-size:17px; margin-top:"+text_m_top+"px; \">";
        str += "<span style=\"color:#ffffff\">To watch this video, please download the latest quicktime player </span>";
        str += "<span><a style=\"color:#e54e1a; text-decoration:none; font-weight:bold\" href=\"http://www.apple.com/quicktime/download/\" target=\"_blank\">here</a></span>";
        str += "<span style=\"color:#ffffff\">.</span>";
    str += "</div>";
    return str;
}

function ShowInstructionPrompt(type)
{
    var p_width = 401;
    var p_height = 254;
    
    InitPrompt(p_width, p_height);
    document.getElementById("PromptContainer").innerHTML = "<div id='content_div' class='prompt_div'></div>";
    $(".prompt_div").css({"margin": "0px", "width":""+p_width+"px", "height":""+p_height+"px", "background-color":"#333333", "position":"fixed", "z-index":"1000"});
    $("#opaque").show();
    $(".prompt_div").show();
    
    var str = "";
    str += "<div style=\"position:absolute; top:-12px; right:-12px\"><img src=\"images/button/prompt_close_over.png\" class=\"btn\" onclick=\"hidePromptNoEffect()\" /></div>";
    
    if(type == "a")
        str += "<div><img src=\"images/background/archive_drag_prompt.png\" /></div>";
    else
        str += "<div><img src=\"images/background/feature_drag_prompt.png\" /></div>";
    
    document.getElementById("content_div").innerHTML = str;
}

/*
function CenterContent(id,width)
{
    var nWidth = eval(width);
    var width_margin = ($(window).width()-nWidth)/2;
    width_margin = Math.round(width_margin);
    $('#'+id).css('margin-left',width_margin+'px');
}
*/

function CenterContent(id,width,scroll_offset)
{
    var nWidth = eval(width);
    var width_margin = ($(window).width()+scroll_offset-nWidth)/2;
    width_margin = Math.round(width_margin);
    if (width_margin < 0)
        width_margin = 0;
    $('#'+id).css('margin-left',width_margin+'px');
}

/* cookies */
function setCookie(isName,isValue,dExpires){
   
	document.cookie = isName+"="+isValue+";expires="+dExpires.toGMTString();
}

function getCookie(isName){

	cookieStr = document.cookie;
	startSlice = cookieStr.indexOf(isName+"=");
	if (startSlice == -1){return false}
	endSlice = cookieStr.indexOf(";",startSlice+1)
	if (endSlice == -1){endSlice = cookieStr.length}
	isData = cookieStr.substring(startSlice,endSlice)
	isValue = isData.substring(isData.indexOf("=")+1,isData.length);
	return isValue;
}

/* detect quicktime */
function detectPlugin() {
    
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
	var pluginsArrayLength = navigator.plugins.length;
	// for each plugin...
	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
	    // loop through all desired names and check each against the current plugin name
	    var numFound = 0;
	    for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
		// if desired plugin name is found in either plugin name or description
		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
		    // this name was found
		    numFound++;
		    
		}   
	    }
	    // now that we have checked all the required names against this one plugin,
	    // if the number we found matches the total number provided then we were successful
	    if(numFound == daPlugins.length) {
		pluginFound = true;
		// if we've found the plugin, we can stop looking through at the rest of the plugins
		break;
	    }
	}
    }
    return pluginFound;
} // detectPlugin

function detectQuickTime() {
    pluginFound = detectPlugin('QuickTime');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
    pluginFound = detectQuickTimeActiveXControl();
    }
    
    return pluginFound;
    //return redirectCheck(pluginFound, redirectURL, redirectIfFound);
}

function detectQTSafari()
{
    pluginFound = detectPlugin('QuickTime');
    return pluginFound;
}

// detect mobile devices
function DetectMobile()
{
    var uagent = navigator.userAgent.toLowerCase();
    if(uagent.search("mobile") > -1) {
        if (document.getElementById("nav_barnyard_placeholder") != null)
            document.getElementById("nav_barnyard_placeholder").style.display = "none";
        if (document.getElementById("nav_barnyard_div") != null)
            document.getElementById("nav_barnyard_div").style.display = "none";
        if(document.getElementById("footer_div")!=null)
            document.getElementById("footer_div").style.display = "none";
        $(".contact_us_placeholder").css("width", "160px");

        //font
        if (document.getElementById("about_desc") != null)
            document.getElementById("about_desc").className = "about_desc_div_mobile";

        $(".big_text").css("font-family", "Helvetica");
        $(".contact_text").css("font-family", "Helvetica");

        //dark bg
        if (document.getElementById("dark_bg") != null) {
            document.getElementById("dark_bg").className = "content_bg_mobile";
        }
        
        /*
        var win_width = $(window).width();
        if (win_width == 1024) {
            var left_margin = 57;
            $(".nav_container").css("margin-left", left_margin + "px");
        }
        */

        //alert(uagent);
        var orientation = window.orientation;
        
        if (uagent.search("iphone") > -1) {
            viewport = document.querySelector("meta[name=viewport]");
            

            if (orientation == -90 || orientation == 90) {
                viewport.setAttribute('content', 'initial-scale=0.5; maximum-scale=1.0;');
            }
            else {
                viewport.setAttribute('content', 'initial-scale=0.35; maximum-scale=1.0;');
            }
            
        }
        else if (uagent.search("ipad") > -1) {
            viewport = document.querySelector("meta[name=viewport]");
            viewport.setAttribute('content', 'initial-scale=1.0; maximum-scale=1.0;');

            if (orientation == -90 || orientation == 90) {
                
                var left_margin = 41;
                $(".nav_container").css("margin-left", left_margin + "px");
                $(".content_div").css("padding-left", left_margin + "px");
                $(".footer").css({ "width": "925px", "margin-left": left_margin + "px" });
                var contact_footer = 46;
                $("#contact_footer").css({ "width": "925px", "margin-left": contact_footer + "px" });
            }
            else {
                $(".logo_placeholder").css("margin-right", "40px");
                $(".logo_div").css("margin-right", "40px");
            }
        }
        
        $('body').bind('orientationchange', function(event) {
            changeOrientation(event.orientation)
        })
        
    }
}

function changeOrientation(ori) {
    window.location.href = window.location.href;
}

