﻿String.format = function( text )
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ),
                                                arguments[ token + 1 ] );
    }
    return text;
};

// function to style buttons
function styleButtons() {
    var buttonClass = "button_control";
    
    jQuery(".buttons").each(function() {
        jQuery(this).wrap("<div class='"+ buttonClass +"'></div>");
    });
    jQuery(".disabled").each(function() {
        jQuery(this).parent("."+ buttonClass +"").addClass("disabled");
    });
}
// function to remove any style buttons
function removeStyleButtons() {
    var buttonClass = "button_control";
    jQuery("." + buttonClass).each(function() {
        jQuery(this).children().filter("input").clone().insertBefore(jQuery(this));
        jQuery(this).remove();
    });
}

function reDrawButtons() {
    removeStyleButtons();
    styleButtons();
}


// Toggle Wizard steps box
function toggleWizard(obj) {
    if (!jQuery(obj).hasClass("on")) {
        jQuery(obj).addClass("on");
    } else {
        jQuery(obj).removeClass("on");
    }
}

// Adds events to wizard list
function setWizardBehavior(p_WizardId) {
   jQuery("#" + p_WizardId + " li").each(function() {
        jQuery(this).hover(
            function() {
                toggleWizard(jQuery(this));       
            },
            function() {
                toggleWizard(jQuery(this));            
            }
        );
   });
}
// Typography GetCurrent font size
var css_font_class = "font_size_";
var css_selected = "selected";
var css_increase_id = "action_increase";
var css_decrease_id = "action_decrease";

function getCurrentTypographySize() {
    var str_class = jQuery("body").attr("class");
    if (str_class.indexOf(css_font_class) != -1)  {
        var tmpSize = str_class.substr(str_class.indexOf(css_font_class), css_font_class.length+1);
        var currentSize = tmpSize.charAt(tmpSize.length-1);
        return(parseInt(currentSize, 10));
    } else {
        return(0);
    }
}
// Typography Toggle selected state
function toggleTypographySelectedState(p_isIncrease) {
    if (p_isIncrease) {
        jQuery("#"+css_increase_id).addClass(css_selected);
        jQuery("#"+css_decrease_id).removeClass(css_selected);    
    } else {
        jQuery("#"+css_increase_id).removeClass(css_selected);
        jQuery("#"+css_decrease_id).addClass(css_selected);    
    }
}
// Typography Resize
function increaseTypography(p_state) {
    var max_size = 1;
    var min_size = 1;
    var str_size;
    var current_size;
    
    if (p_state) {
        // Increase
        if (jQuery("body").attr("class").indexOf(css_font_class) != -1)  {
            if (getCurrentTypographySize() < max_size) {
                current_size = getCurrentTypographySize();
                str_size = css_font_class + getCurrentTypographySize();
                jQuery("body").removeClass(str_size);
                
                str_size = css_font_class + (current_size + 1);
                jQuery("body").addClass(str_size);
            }
        } else {
            jQuery("body").addClass(css_font_class + min_size);
        }
        toggleTypographySelectedState(true);
        
    } else {
        // Decrease
        if (jQuery("body").attr("class").indexOf(css_font_class) != -1)  {
            if (getCurrentTypographySize() > min_size) {
                current_size = getCurrentTypographySize();
                str_size = css_font_class + getCurrentTypographySize();
                jQuery("body").removeClass(str_size);
                
                str_size = css_font_class + (current_size - 1);
                jQuery("body").addClass(str_size);
            } else {
                jQuery("body").removeClass(css_font_class + min_size);
                toggleTypographySelectedState(false);
            }
        }
    }
    var sizeTypo = css_font_class + getCurrentTypographySize();
    cookie.set("font",sizeTypo,30);
}
// Set font size vt cookie
function setFontSize() {
    var savedSize = cookie.get("font");
    if ((savedSize != "") && (savedSize != null)) {
        var str_size = css_font_class + getCurrentTypographySize();
        jQuery("body").removeClass(str_size);
        $("body").addClass(savedSize);
    }
}

// Set print function
function setPrintAction() {
    jQuery(".action_print a").each(function() {
        jQuery(this).click(function() {
            window.print();
            return false;
        });
    });
}

// Set login status image
function setLoginLinkImage() {
    var jQuerytmpImg = jQuery("#header_options .action_close img").clone();
    jQuery("#header_options .action_close img").remove();
    
    jQuerytmpImg.attr("id","asset_login");
    jQuerytmpImg.prependTo("#header_options .action_close a");
}

// ReRender Grid Tables
function reRenderTables() {
    jQuery("table").each(function() {
        if (jQuery(this).hasClass("table_standard")) {
            jQuery(this).attr("rules","all");        
        } else {
            jQuery(this).attr("rules","none");        
        }
    });
}

// render ajax loading indicator
function renderProgressDialog() {
    jQuery(".update_progress").each(function() {
        jQuery(this).css("width", jQuery(window).width());
        jQuery(this).css("height", jQuery(window).height());
        jQuery(this).css("top", jQuery(document).scrollTop() + "px");
        var left, top;

        jQuery(".progress_content").each(function() {
            left = parseInt((jQuery(window).width() - parseInt(jQuery(this).css("width"), 10)) / 2, 10) + "px";
            if (left != 'NaNpx')
                jQuery(this).css("left", left);
            top = parseInt((jQuery(window).height() - parseInt(jQuery(this).css("height"), 10)) / 2, 10) + "px";
            if (top != 'NaNpx')
                jQuery(this).css("top", top);
        });

        jQuery(".UploaderProgressContainer").each(function() {
            jQuery(jQuery(this).parent()).css("height", "auto").css("width", "auto");
            left = parseInt((jQuery(window).width() - parseInt(jQuery(this).css("width"), 10)) / 2, 10) + "px";
            if (left != 'NaNpx')
                jQuery(this).css("left", left);
            top = parseInt((jQuery(window).height() - parseInt(jQuery(this).css("height"), 10)) / 2, 10) + "px";
            if (top != 'NaNpx')
                jQuery(this).css("top", top);
        });
    });
}

// Close Ajax progress popup
function closeProgressDialog() {
    jQuery(".update_progress").each(function() {
        jQuery(this).toggleClass("hide");
    });
}

// ToolTip Recoded
this.tooltip = function(){	
	// these 2 variable determine popup's distance from the cursor
	var xOffset = 10;
	var yOffset = 20;		

	jQuery(".tooltip").hover(function(e){											  
		this.tmpTitle = this.title;
		this.title = "";
		
		if (this.tmpTitle !== '') {
		    jQuery("body").append("<div id='tooltip'>"+ this.tmpTitle +"</div>");
		    jQuery("#tooltip")
			    .css("top",(e.pageY - xOffset) + "px")
			    .css("left",(e.pageX + yOffset) + "px")
			    .fadeIn("fast");
			    
		    if (jQuery("#tooltip").width() > 300) {
		        jQuery("#tooltip").css("width", "300px");
		    }
        }
    },
	function(){
		this.title = this.tmpTitle;		
		jQuery("#tooltip").remove();
    });	

	jQuery(".tooltip").mousemove(function(e){
		jQuery("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

//Media event listener
function playerReady(obj)
{
    if (obj["id"] == "VideoPlayerView")
    {
        var playerVideo = document.getElementById(obj["id"]);
        playerVideo.addModelListener("STATE", "CountVideo");
    }
    if (obj["id"] == "AudioPlayerView")
    {
        var playerAudio = document.getElementById(obj["id"]);
        playerAudio.addModelListener("STATE", "CountAudio");
    }
}
function sortfunction(a, b){
	return (a - b);
}
// Set the height of containers with the highest Height
// 
// Usage:
// setDivsHeight(elementNode, n);
//
// Examples:
// setDivsHeight('#myDivId1','#myDivId2');
function setDivsHeight() {
	var intReset = 0;
	var tmpArr = new Array();

    // Reset height
	for (var i=0; i<setDivsHeight.arguments.length;i++) {
		jQuery(setDivsHeight.arguments[i]).css("height","auto");
	}

	// Get Container height
	for (i=0; i<setDivsHeight.arguments.length;i++) {
		tmpArr[i] = jQuery(setDivsHeight.arguments[i]).height();
	}

	// Set Container Height
	tmpArr.sort(sortfunction);
	var maxHeight = (tmpArr[tmpArr.length - 1] / 12) + "em";
	for (i=0; i<setDivsHeight.arguments.length;i++) {
		jQuery(setDivsHeight.arguments[i]).each(function() {
		    jQuery(this).css("height", maxHeight);
		});
	}
}
function LightBoxPatchIE6(enable) {
	if (enable) {
		jQuery("select").addClass("hide");
	} else {
		jQuery("select").removeClass("hide");
	}
}
function hidePageSelects() {
    LightBoxPatchIE6(true);
}
function showPageSelects() {
    LightBoxPatchIE6(false);
}
function isIE() {
	if (window.ActiveXObject) {
		return true;
	}
	return false;
}
function getIEVersion() {
	if (isIE()) {
		var ua = navigator.userAgent;
		var MSIEOffset = ua.indexOf("MSIE ");
		
		if (MSIEOffset == -1) {
			return 0;
		} else {
			return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
		}
	} else {
		return 0;	
	}
}
// LightBoxImage
// 
// Usage:
// lightBoxImage(".mySelector");
function lightBoxImage(selector) {
    jQuery(selector).each(function() {

        jQuery(this).click(function() {
            // Remove previous lightbox
            jQuery("#lightBox_wrapper").remove();
            jQuery("#lightBox_container").remove();

            var wrapper = jQuery(document.createElement("div")).attr("id", "lightBox_wrapper");
            jQuery(wrapper).css("width", jQuery(window).width());
            jQuery(wrapper).css("height", jQuery(document).height());
            jQuery(wrapper).appendTo("body");
            jQuery(wrapper).css("display", "block");

            var container = jQuery(document.createElement("div")).attr("id", "lightBox_container").addClass("loading");
            jQuery(container).appendTo("body");

            // Close actions
            jQuery(wrapper).click(function() {
                jQuery(this).remove();
                jQuery("#lightBox_container").remove();
                LightBoxPatchIE6(false);
            });

            var img = new Image();
            jQuery(img).load(function() {
                jQuery(this).hide();

                jQuery("#lightBox_container").removeClass("loading", "slow").append(this);

                // Resize Container
                jQuery(this).wrap("<div class='content_popup'></div>");
                var iWidth = jQuery(this).width() + parseInt(jQuery(jQuery(this).parent()).css("padding-left").replace("px", ""), 10) + parseInt(jQuery(jQuery(this).parent()).css("padding-right").replace("px", ""), 10);
                if (iWidth < 430) {
                    iWidth = 430;
                }
                jQuery("#lightBox_container").css("width", iWidth + "px");
                jQuery("#" + jQuery(container).attr("id")).css("height", "auto");


                // Add classic header
                var header = jQuery(".id_contact_us .header_popup:first").clone();
                jQuery(header).attr("id", "lightBox_header");
                jQuery(header).find("span").each(function() {
                    jQuery(this).attr("id", "");
                });
                jQuery(header).find("a").each(function() {
                    jQuery(this).attr("id", "");
                });
                jQuery(header).find("img").each(function() {
                    jQuery(this).attr("id", "");
                });
                jQuery(header).prependTo("#lightBox_container");

                // FireFox Issue here need to do centering stuff here
                var left = parseInt((jQuery(window).width() - jQuery(this).width()) / 2, 10);
                if (left < 0) {
                    left = 20;
                }

                var top = parseInt((jQuery(window).height() - jQuery(this).height() - jQuery(header).height()) / 2, 10) + jQuery(window).scrollTop();
                if (top < 0) {
                    top = 20;
                }
                jQuery("#" + jQuery(container).attr("id")).css("left", left + "px");
                jQuery("#" + jQuery(container).attr("id")).css("top", top + "px");

                jQuery(this).fadeIn("slow");

                // Create Content
                var content_data = jQuery(document.createElement("div")).attr("id", "lightBox_data");
                jQuery(content_data).appendTo("#" + jQuery(container).attr("id"));

                if (jQuery(this).children().attr("title") !== '' && jQuery(this).children().attr("title") !== null) {
                    jQuery("#" + jQuery(content_data).attr("id")).text(jQuery(this).children().attr("title"));
                }

                // Close actions
                jQuery(header).find(".action_close a").click(function() {
                    jQuery("#lightBox_wrapper").remove();
                    jQuery("#lightBox_container").remove();
                    LightBoxPatchIE6(false);
                    return false;
                });
            }).error(function() {
                alert("error while loading image!");
                jQuery("#lightBox_wrapper").remove();
                jQuery("#lightBox_container").remove();
            }).attr('src', jQuery(this).attr("href"));


            // Center content
            var left = parseInt((jQuery(window).width() - parseInt(jQuery("#lightBox_container").css("width"), 10)) / 2, 10) + "px";
            var top = parseInt((jQuery(window).height() - jQuery("#lightBox_container").height()) / 2, 10) + jQuery(window).scrollTop() + "px";
            jQuery("#" + jQuery(container).attr("id")).css("left", left);
            jQuery("#" + jQuery(container).attr("id")).css("top", top);
            LightBoxPatchIE6(true);

            return false;
        });
    });
}
function setHeaderOptions() {
    if ($("#header_options_secondary li").length === 1) {
        $("#header_options_secondary li").addClass("child_1");
    }
}
function renderGrid() {
    $(".box .grid_candidate").parents(".box").addClass("summary");
    $(".box .grid_candidate tr").each(function() {
        $(this).children().each(function(idx) {
            $(this).addClass("child_"+(idx+1));
        });
    });
    
    $(".box .grid_results").parents(".box").addClass("summary");    
    $(".box .grid_results tr").each(function() {
        $(this).children().each(function(idx) {
            $(this).addClass("child_"+(idx+1));
        });
    });
}
function toggleSelectCheckBox(ul_actions, checkbox) {
    $(ul_actions).find(".select_all").click(function() {
        $(checkbox).find("input[type='checkbox']").each(function() {
            $(this).attr("checked", true);
        });
        return false;
    });   
    $(ul_actions).find(".unselect_all").click(function() {
        $(checkbox).find("input[type='checkbox']").each(function() {
            $(this).attr("checked", false);
        });
        return false;
    });
}
var cookie = {
	set : function(name,value,days) {
		var expires = "";
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		document.cookie = name+"="+value+expires+"; path=/";
	},
	get : function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') {
				c = c.substring(1,c.length);
			}
			if (c.indexOf(nameEQ) === 0) {
				return c.substring(nameEQ.length,c.length);
			}
		}
		return null;
	},
	remove : function(name) {
		cookie.set(name,"font",-1);
	}		
}
function renderCaptchaLabel() {
    var objTRs = $("#recaptcha_table").children().children();
    $($(objTRs)[0]).attr("id","captcha_image");
    var objTR = $(objTRs)[1];
    var objTD = $(objTR).children()[0];
    $(objTD).prepend($("#label_captcha"));
    var inputHTML = $(objTD).html();
    $(objTR).children().each(function() {
        $(this).remove();
    });
    var td = $(document.createElement("td")).attr("colspan","3").attr("id","td_captcha").css("padding-left","7px");
    $(objTR).append($(td).html(inputHTML));
    $("#label_captcha").removeClass("hide");
    
    var objImg = $("#recaptcha_image").clone();
    $("#recaptcha_image").parent().remove();
    $(".recaptcha_image_cell").append($(objImg));
}
