/// <reference path="..\sample\script\jquery-1.4.2.js" />
/* jQuery KeyTips Plugin 1.0.2 - http://damianedwards.com */
(function($) {
    jQuery.extend({
        trace: function(msg, isDebug) {
            if (isDebug === false) return;
            if ((typeof (Debug) !== "undefined") && Debug.writeln) {
                Debug.writeln(msg);
            }
            if (window.console && window.console.log) {
                window.console.log(msg);
            }
            if (window.opera) {
                window.opera.postError(msg);
            }
            if (window.debugService) {
                window.debugService.trace(msg);
            }
        },
        keyTips: function(options) {
            // ready
            $(function() {
                var requiresHighlighting, requiresShiftAlt, accessKeysHighlighted,
                    accessKeyPopups, accessKeyPopupFields, requiresCtrltAlt, settings;
                
                requiresHighlighting = !/opera/.test(navigator.userAgent); // exit if opera as it has its own dedicated access key selection interface
                
                if (!requiresHighlighting) return;
                requiresShiftAlt = $.browser.mozilla;
                requiresAutoIntro = $.browser.msie;
                accessKeysHighlighted = false;
                requiresCtrltAlt = true;
                accessKeyPopups = [];
                accessKeyPopupFields = [];
                
                // define settings
                settings = $.extend({
                    debug: false,
                    highlightClass: "akh__highlighted",
                    popupClass: "akh__popup",
                    popupGroupClass: "akh__popupGroup",
                    highlightMode: "popup", // alternative is "toggleClass"
                    accessKeyTag: "em", // could be any inline tag, e.g. em, strong, span
                    offsets: {
                        label: {
                            left: -20,
                            top: 2
                        },
                        button: {
                            left: -3,
                            top: -3
                        },
                        anchor: {
                            left: 2,
                            top: 12
                        },
                        text: {
                            left: -3,
                            top: -3
                        },
                        group: {
                        	left: -20,
                        	top: 2
                        },
                        other: {
                            left: -3,
                            top: -3
                        }
                    }
                }, options);
                
                // functions
                var isSelectorMatch = function(element, selector) {
                    return $(element).filter(selector).length == 1;
                };
                
                var getOffset = function(element) {
                    if (isSelectorMatch(element, "label")) {
                        return settings.offsets.label;
                    } else if (isSelectorMatch(element, ":button, :submit, :reset, :image")) {
                        return settings.offsets.button;
                    } else if(isSelectorMatch(element, "a")) {
                        return settings.offsets.anchor;
                    } else if (isSelectorMatch(element, ":text, textarea")) {
                        return settings.offsets.text;
                    } else if (isSelectorMatch(element, "ul")) {
                    	return settings.offsets.group;
                    } else {
                        return settings.offsets.other;
                    };
                };
                
                var getPopupLocation = function(element) {
                    var popupLocation = $(element).offset();
                    var offset = getOffset(element);
                    popupLocation.left = popupLocation.left + offset.left;
                    popupLocation.top = popupLocation.top + offset.top;
                    return popupLocation;
                };
                
                var getPopupClass = function(element) {
                	var css = settings.popupClass;
                	if( /^ul$/i.test( element.nodeName )  ) {
                		css += " " + settings.popupGroupClass;
                	}
                	return css;
                };
                
                var createPopup = function(field, accessKey) {
                    /// <summary>Creates an access key popup and adds it to the global array</summary>
                    var popupLocation = getPopupLocation(field);
                    var popupClass = getPopupClass(field);

                    // Create popup element, set its location and classs and add to the global array
                    var popup = $(document.createElement("div"))
                        .text(accessKey)
                        .css("left", popupLocation.left)
                        .css("top", popupLocation.top)
                        .addClass(popupClass)
                        .appendTo("body")
                        .get(0);
                    
                    accessKeyPopups.push(popup);
                    accessKeyPopupFields.push(field);
                    return popup;
                };
                
                var clearPopups = function() {
                    /// <summary>Clears all access key popups from the form</summary>
                    $(accessKeyPopups).remove();
                    accessKeyPopups = [];
                    accessKeyPopupFields = [];
                };
                
                var insertAccessKeyTags = function() {
                    /// <summary>Find access key text and surround with a tag</summary>
                    
                    $('label[for], label[accesskey]').each(function() {
                        // Get accesskey from corresponding form field, otherwise from the label
                        // BUG: Need to allow for when for is empty
                        var relatedFieldId = "#" + $(this).attr("for");
                        var accessKey = $.trim(relatedFieldId !== "#" ? $(relatedFieldId).attr("accesskey") : "");
                        if (typeof(accessKey) === "undefined" || accessKey === "") {
                            // Get accesskey from label
                            accessKey = $(this).attr("accesskey");
                        }
                        if (typeof(accessKey) === "undefined"  || accessKey === "") return true;
                        var labelHtml = $(this).html();
                        var accessKeyIndex = labelHtml.toUpperCase().indexOf(accessKey.toUpperCase());
                        if (accessKeyIndex < 0) return true;
                        // <tagName>accessKeyFromLabel</tagName>
                        var accessKeyMarkup = "<" + settings.accessKeyTag + ">" + labelHtml.substr(accessKeyIndex, 1) + "</" + settings.accessKeyTag + ">";
                        var newLabelHtmlLeft = labelHtml.substring(0, accessKeyIndex);
                        var newLabelHtmlRight = labelHtml.substr(accessKeyIndex + 1);
                        var newLabelHtml = newLabelHtmlLeft + accessKeyMarkup + newLabelHtmlRight;
                        if (labelHtml.indexOf(accessKeyMarkup) < 0) $(this).html(newLabelHtml);
                    });
                };
                
                var createPopups = function() {
                    /// <summary>Creates popups for access keys on the form</summary>
                    if (settings.highlightMode == "popup") {
                        clearPopups();
                        var accessKeyPopupFormFields = [];

                        // Create popups for labels
                        $("label > " + settings.accessKeyTag).each(function() {
                            var text = $(this).text();
                            var label = $(this).parent();
                            var formField = $("#" + label.attr("htmlFor"));
                            var accessKey = formField.attr("accessKey") != "" ? formField.attr("accessKey") : label.attr("accessKey");
                            if (text.toUpperCase() == accessKey.toUpperCase()) {
                                createPopup(label[0], accessKey);
                            }
                            accessKeyPopupFormFields.push(formField[0]);
                        });
/*cambios para seo*/
                        $('a[href][accesskey], textarea[accesskey], input[accesskey], ul[class^="accesskey"], , ul[class^="MM"] ').each(function() {
                            if ($.inArray(this, accessKeyPopupFormFields) === -1) {
                            	if($(this).attr("tagName").toUpperCase() == "UL"){
                            		if($(this).attr("className").toUpperCase()!="MM"){
	                            		var ackey= ($(this).attr("className").toUpperCase()).substring(10, 11);
				                    	
				                    	if(ackey!=null && ackey!="" && ackey!=" ")
	                            		 createPopup(this, ackey);
                            		 }
                            		 else{
                            		 	createPopup(this, "M");
                            		 }
                            	}
                            	else{
                                 createPopup(this, $(this).attr("accessKey"));
                                }
                            }
                        });
                    }
                };
                
                var refreshPopups = function() {
                    /// <summary>Clears and the re-creates access key popups for the form</summary>
                    clearPopups();
                    createPopups();
                };
                
                var doHighlightAccessKeys = function(highlight) {
                    /// <summary>Toggles the highlighting of access keys on the page</summary>
                    if (settings.highlightMode === "popup") {
                        // Popups
                        var i = 0;
                        $.each(accessKeyPopups, function() {
                            var field = accessKeyPopupFields[i]; i++;
                            var popupLocation = getPopupLocation(field);
                            $(this).css("left", popupLocation.left)
                                   .css("top", popupLocation.top)
                                   .toggle(highlight);
                        });
                    } else if (settings.highlightMode === "toggleClass") {
                        // Toggle label class
                        $("label > " + settings.accessKeyTag).each(function() {
                            var text = $(this).text();
                            var label = $(this).parent();
                            var formField = $("#" + label.attr("htmlFor"));
                            var accessKey = formField.attr("accessKey") !== "" ? formField.attr("accessKey") : label.attr("accessKey");
                            if (text.toUpperCase() === accessKey.toUpperCase()) {
                                $(this).toggleClass(settings.highlightClass, highlight);
                            }
                        });
                    }
                };
                
                var highlightAccessKeys = function() {
                    doHighlightAccessKeys(true);
                };
                
                var unhighlightAccessKeys = function() {
                    doHighlightAccessKeys(false);
                };
                
                var initMenu = function( menu, link ) {
                	var oldLink = $(menu).find("a[menu]")[0];
                	if( oldLink ) {
                		oldLink.menu = null;
                		$(oldLink).unbind( 'keydown', menuKeyListener );
                	}
                	link.focus();
                	link.menu = menu;
                	if( $.browser.msie ) {
                		$(link).addClass("ie_focus");
                		$(link).bind( 'blur', function(){
                			$(link).removeClass("ie_focus");
                			$(link).unbind('blur');
                		});
                	}
                	$(link).bind( 'keydown', menuKeyListener );
                };
                var menuKeyListener = function( evelyn ) {
                	var link = evelyn.target;
                	var menu = link.menu;
                	
                	switch( evelyn.keyCode ) {
	                	case 38: // up arrow
                		case 37: // left arrow
                			var links = $(menu).find("a");
                			for( var i = 0; i < links.length; i++ ) {
                				if( links[i] == link ) {
                					var prev = i - 1;
                					if( prev == -1 ) {
                						prev = links.length - 1;
                					}
                					initMenu( menu, links[prev] );
                				}
                			}
                			evelyn.stopPropagation();
                			evelyn.preventDefault();
                			break;
                		case 40: // down arrow
                		case 39: // right arrow
                			var links = $(menu).find("a");
                			for( var i = 0; i < links.length; i++ ) {
                				if( links[i] == link ) {
                					var next = i + 1;
                					if( next == links.length ) {
                						next = 0;
                					}
                					initMenu( menu, links[next] );
                				}
                			}
                			evelyn.stopPropagation();
                			evelyn.preventDefault();
                			break;
                		case 27: // escape
                			link.blur();
                			break;
                	}
                };
                
                // bind handlers
                $(document)
                    .keydown(function(e) {
                        $.trace("AccessKeyHighlighter document.keyDown: keyCode=" + e.keyCode +
                                ", altKey=" + e.altKey +
                                ", ctrlKey=" + e.ctrlKey, settings.debug);
                        if (!accessKeysHighlighted && (
                                
                                (e.keyCode == 17 && e.altKey && requiresCtrltAlt) ||
                                (e.keyCode == 18 && e.ctrlKey && requiresCtrltAlt))) {
                            // Highlight all the access keys
                            highlightAccessKeys();
                            accessKeysHighlighted = true;
                        }
                    })
                    .keyup(function(e) {
                        $.trace("AccessKeyHighlighter document.keyUp: keyCode=" + e.keyCode +
                                ", altKey=" + e.altKey +
                                ", shiftKey=" + e.shiftKey, settings.debug);
                        // Un-highlight access keys
                        if (accessKeysHighlighted) {
                            unhighlightAccessKeys();
                            accessKeysHighlighted = false;
                        	var char = String.fromCharCode(e.keyCode);
                        	for( var i = 0; i < accessKeyPopupFields.length; i++ ) {
                        	/*cambio para seo*/
                        		if( $(accessKeyPopupFields[i]).attr("accesskey") == char || $(accessKeyPopupFields[i]).attr("className").substring(10, 11) == char) {
                        			var element = accessKeyPopupFields[i];
                        			if( isSelectorMatch(element, "ul") ) {
										var isSmartmenus = element.MM?true:false;
                        				element.focus();
                        				var link = $(element).find("a")[0];
                   						if( link ) {
                   							if( isSmartmenus && typeof c_oF == "function" ) {
                   								link.focus();
                   								c_oF.apply(link); // smartmenus
                   							} else {
                   								initMenu( element, link );
                   							}
                   						}
                        			}  else if( requiresAutoIntro ) {
				                		$(element).addClass("ie_focus");
				                		$(element).bind( 'blur', function(){
				                			$(element).removeClass("ie_focus");
				                			$(element).unbind('blur');
				                		});
				                		if( element.href ) {
                        					document.location = element.href; //TODO tenir en compte el target
                        				}
	                        		}
                        			return;
                        		}else if($(accessKeyPopupFields[i]).attr("className").toUpperCase() == "MM" && char=="M"){
                        			var element = accessKeyPopupFields[i];
                        			if( isSelectorMatch(element, "ul") ) {
										var isSmartmenus = element.MM?true:false;
                        				element.focus();
                        				var link = $(element).find("a")[0];
                   						if( link ) {
                   							if( isSmartmenus && typeof c_oF == "function" ) {
                   								link.focus();
                   								c_oF.apply(link); // smartmenus
                   							} else {
                   								initMenu( element, link );
                   							}
                   						}
                        			}
                        		
                        		}
                            }
                        }
                    });
                
                $(window)
                    .resize(function(e) {
                        $.trace("resize event", settings.debug);
                        // Hide the popups
                        if (accessKeysHighlighted && settings.highlightMode == "popup") {
                            unhighlightAccessKeys();
                            accessKeysHighlighted = false;
                        }
                    })
                    .blur(function(e) {
                        $.trace("blur event", settings.debug);
                        // Un-highlight access keys
                        if (accessKeysHighlighted) {
                            unhighlightAccessKeys();
                            accessKeysHighlighted = false;
                        }
                    })
                    .focus(function(e) {
                        $.trace("focus event", settings.debug);
                        // Un-highlight access keys
                        if (accessKeysHighlighted) {
                            unhighlightAccessKeys();
                            accessKeysHighlighted = false;
                        }
                    });

                // Create the access key popups
                insertAccessKeyTags();
                createPopups();
            });

            // unload
            $(window).unload(function() {
                $(document).unbind();
                $(window).unbind();
            });
        }
    });
})(jQuery);
