/// <reference path="/_resources/js/jquery-1.2.6.min.js" />
/// <reference path="/_resources/js/universal.js" />
/* Genzyme Carticel Global Javascript Functions

By  - ISITE Design

*/

// jquery no conflict. use $j or jQuery outside of ready function
var $j = jQuery.noConflict();

//start the jQuery functions
$j(document).ready(function() {
    // show Brightcove video
    insertBrightcove();
    
    // insert print and email actions and run thickbox init		
	if ($j("#utilitynav li").hasClass("control-printpage") == false)
	{
	    var mailurl = "/emailpage.aspx";//?height=560&width=490&url="+window.location;
	    $j("ul#utility-nav").replaceWith('<ul id="utility-nav"><li class="control-emailpage"><a href="'+mailurl+'">'+STRINGS.PAGE_UTILITY_STRINGS.EmailThisPage+'</a></li><li class="control-printpage"><a href="#">'+STRINGS.PAGE_UTILITY_STRINGS.PrintThisPage+'</a></li></ul>');					
	    $j(".control-printpage").click(function(){ 
		    printCall(); 
		    return false; 
	    });	    
        standard_email_tb_init('.control-emailpage a.thickbox');
	}
	$j("a.print").click(function(){
	    printCall();
	    return false;
	});
    
    // alternate version of the above plugin that pulls the label, sets as default value and hides it
	$j("#search input").inputSetter();	
	


	/* add class to drop downs and buttons for IE <6 */
	if(document.all){
	    $j("#nav li a").hover(
	            function() {
					$j(this).addClass("over");
				},
	            function() {
					$j(this).removeClass("over");
				}
	    );
	}// if document.all
	
	/* add class to drop downs and buttons for IE <6 */
	if(document.all){
	    $j("#nav li ul li").hover(
	            function() {
					$j(this).addClass("drop");
				},
	            function() {
					$j(this).removeClass("drop");
				}
	    );
	}// if document.all
	
	// Add a class to drop down <ul>
	$j("#nav li ul li ul").addClass("nav-dropdown");
	
	initHomePageVideo();
    registerVideo("#video-trigger-kevin, .view-kevin-story", "width:386 height:290 playerID:8804000001 videoPlayer:6971949001");
    registerVideo("#video-trigger-paulina", "width:386 height:290 playerID:8804000001 videoPlayer:6970543001");
    registerVideo("#video-trigger-john", "width:386 height:290 playerID:8804000001 videoPlayer:6970541001");
    registerVideo("#video-trigger-manufacturing", "width:386 height:290 playerID:8804000001 videoPlayer:946049705001");

});// document ready / end jquery functions

var initHomePageVideo = function()
{
    var $video = $j("#video");
	$video.addClass("js-on").hide();
	$video.prepend('<div class="player"><div></div></div>');
	$video.prepend('<a href="#" class="close">close</a>');
	$j("#video .close").click(function() {
	    var $oldTrigger = $j($video.data("triggerSelector"));
	    $oldTrigger.removeClass("active");
	    $oldTrigger.removeAttr("disabled");
	    $j("#video .player").html('<div></div>');
	    $video.removeData("triggerSelector");
	    $video.slideToggle(100);
	    return false;
	});
}
var registerVideo = function(selector, options)
{
    $j(selector).click(function(){
        if ($j(selector).hasClass("active") == true)
            return false;
            
        var $video = $j("#video");
        var oldTriggerSelector = $video.data("triggerSelector");
        var hasOldTrigger = (typeof oldTriggerSelector == "string")
        if (hasOldTrigger == true)
        {
            $j("#video .player").html('<div></div>');
            var $oldTrigger = $j(oldTriggerSelector);
            $oldTrigger.removeClass("active");
            $oldTrigger.removeAttr("disabled");          
        }
        $j(selector).addClass("active");
        $j(selector).attr("disabled","disabled");
        var $playerInner = $j("#video .player div");
        $playerInner.addClass("brightcove");
        $playerInner.addClass(options);
        insertBrightcove();
        $video.data("triggerSelector", selector);
        if (hasOldTrigger == false)
            $video.slideToggle(100);
        return false;
    });
}

// implements c# style String.format("{0}: {1}", 'key', 'value)
String.prototype.format = function()
{
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture){ return args[capture.match(/\d+/)]; });
}

// populates div.brightcove with the requested brightcove movie
var insertBrightcove = function() {
	
	$j("div.brightcove").each(function(i) {
		var $this = $j(this);
		var params = $this.attr("class").split(" ");

        var bgcolor = "#FFFFFF";
        var width = "486";
        var height = "412"
        var publisherID = 1572118498;
        var playerID;
        var videoPlayer;
        var isVid = true;
        var isUI = false;
        
        jQuery.each(params, function(i, val) {
            var parts = val.split(":");
            if (parts.length == 2)
            {
                switch(parts[0])
                {
                    case "bgcolor":
                        bgcolor = parts[1];
                        break;
                    case "width":
                        width = parts[1];
                        break;
                    case "height":
                        height = parts[1];
                        break;
                    case "playerID":
                        playerID = parts[1];
                        break;
                    case "publisherID":
                        publisherID = parts[1];
                        break;
                    case "videoPlayer":
                        videoPlayer = parts[1];
                        break;
                    case "isVid":
                        isVid = (parts[1] == "true");
                        break;
                    case "isUI":
                        isUI = (parts[1] == "true");
                    default:
                        break;
                }
            }
        });

        //var $html = '<!-- Start of Brightcove Player --><div style="display:none"></div><!--By use of this code snippet, I agree to the Brightcove Publisher T and C found at http://corp.brightcove.com/legal/terms_publisher.cfm. --><script language="JavaScript" type="text/javascript" src="http://admin.brightcove.com/js/BrightcoveExperiences.js"></script><object id="myExperience{5}" class="BrightcoveExperience"><param name="bgcolor" value="{0}" /><param name="width" value="{1}" /><param name="height" value="{2}" /><param name="playerID" value="{3}" /><param name="publisherID" value="{4}"/><param name="isVid" value="true" /><param name="@videoPlayer" value="{5}" /></object><!-- End of Brightcove Player -->'.format(
        var $html = '<embed src="http://c.brightcove.com/services/viewer/federated_f9/{3}?publisherID={4}{6}{7}" bgcolor="{0}" flashVars="@videoPlayer={5}&playerID={3}&domain=embed&" base="http://admin.brightcove.com" name="flashObj" width="{1}" height="{2}" wmode="transparent" seamlesstabbing="false" type="application/x-shockwave-flash" allowFullScreen="true" swLiveConnect="true" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>'.format(
	    	bgcolor, width, height, playerID, publisherID, videoPlayer,
	    	(isVid == true) ? "&isVid=1" : "",
	    	(isUI == true) ? "&isUI=1" : "");
		
		$this.replaceWith($html);		
	});

};

// see http://jira.isitedesign.com/browse/MACI-14
function printCall(obj) 
{
    if (typeof(s_gi) == 'function')
    {
        var s=s_gi(s_account);
        if (s && typeof s.tl == 'function')        
            s.tl(obj,"o", STRINGS.PAGE_UTILITY_STRINGS.PrintThisPage);        
    }
    window.print();    
}

/* clear search field on click - made into plugin so it can easily be used more than once. */
var inputClear = function(element) {
    var $element = $j(element);
	return $element.focus(function() {	 
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {	
	    if( !this.value.length ) {
    	    this.value = this.defaultValue;	    	
        }
	});
};

// alternate version of the above. pull label and insert as field. clear with click.
jQuery.fn.inputSetter = function() {
	
	return this.each(function() {
	
		var $input = $j(this);
		var $label = $j("label[for='"+$input.attr("id")+"']");
		var labeltext = $label.text();
		
		$label.hide();
		$input.val(labeltext);
		
		$input.focus(function() {
			if (this.value == labeltext) {
				this.value = "";
			}
		}).blur(function() {
			if (!this.value.length) {
				this.value = labeltext;
			}
		});
		
	});
};
// custom validation method for jquery.validation.js. use with inputSetter to validate fields as required but not equal to the label (default value)
// may need to change where to look for the label depending on particular html
/*
jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
	return $j(element).val()!=$j(element).prev("label").text();
}, "error");
*/

// clean console.log
function cl(logit){ if(window.console&&window.console.firebug) { console.log(logit) } };//cl()
// example:
cl("If you use cl() instead of console.log(), it won't break IE when you forget to take it out.");


