/**
 * 
 * Loads all common scripts used on YBO
 * 
 * 
 */

var currentPage; // global location value

/**
 * Scrollpane initialization
 * 
 * @param none
 * @returns none
 */
var initScrollPane = function() {

	$('.pane').jScrollPane();

	// $('.pane').scrollTo(200);

};

/**
 * sIFR initialization and configuration
 * 
 * @param none
 * @returns none
 */

var initSifr = function() {

	/* Font files reference */

	var avenirLight = {
		src : '../../_LAYOUTS/assets/flash/fonts/avenir_light.swf'
	};
	var arial = {
		src : '../../_LAYOUTS/assets/flash/fonts/arial.swf'
	};
	var avenirBook = {
		src : '../../_LAYOUTS/assets/flash/fonts/avenir_45_book.swf'
	};

	var bahuer = {
		src : '../../_LAYOUTS/assets/flash/fonts/bahuer_bodoni.swf'
	};

	sIFR.activate(avenirLight, avenirBook, bahuer, arial);// Activation… duh!

	/* Finds and replaces fonts on DOM accoding to the selector */

	sIFR.replace(avenirLight, {
				selector : 'h2 .head',
				wmode : 'transparent',
				css : ['.sIFR-root {color: #dd2559; leading: -10;}']
			});
			
	sIFR.replace(avenirLight, {
	               selector : '#headline, #center h3',
	               wmode : 'transparent',
	               css : ['.sIFR-root {color: #3c3c3c;}']
	           });
	           sIFR.replace(avenirLight, {
	               selector: '#center h4',
	               wmode: 'transparent',
	               css: ['.sIFR-root {color: #a4bf0f;}']
	           });
	           sIFR.replace(avenirLight, {
	               selector : '#center #info h2',
	               wmode : 'transparent',
	               css : ['.sIFR-root {font-style:bold;color: #a4b40f; leading: -10;text-transform: Capitalize;}']
	           });

	sIFR.replace(avenirLight, {
				selector : '#main h2, .avenir-book-h2 h2',
				wmode : 'transparent',
				css : ['.sIFR-root {color: #dd2559;}']
});
			sIFR.replace(arial, {
			selector: 'span.h2Italic',
				wmode : 'transparent',
				css : ['.sIFR-root {font-style:italic;color: #dd2559;}']
});

sIFR.replace(avenirLight, {
    selector: '#modules .module h2',
    wmode: 'transparent',
    fitExactly: true,
    css: ['.sIFR-root {color: #3c3c3c;}'],
    ratios: [9, 1.16, 16, 1.09, 24, 1.06, 37, 1.04, 74, 1.02, 1.01]
});

	sIFR.replace(avenirLight, {
				selector : 'h2',
				wmode : 'transparent',
				css : ['.sIFR-root {color: #3c3c3c; leading: -10;}'],
				ratios : [8, 1.41, 9, 1.33, 12, 1.35, 14, 1.29, 15, 1.3, 19,
						1.28, 20, 1.27, 32, 1.26, 33, 1.24, 34, 1.25, 44, 1.24,
						52, 1.23, 53, 1.24, 64, 1.23, 67, 1.22, 68, 1.23, 69,
						1.22, 71, 1.23, 72, 1.22, 73, 1.23, 81, 1.22, 82, 1.23,
						1.22]
			});
	sIFR.replace(avenirLight, {
				selector : '.profileInfo p',
				wmode : 'transparent',
				css : ['.sIFR-root {color: #3c3c3c; leading: 10;}'],
				ratios : [8, 1.41, 9, 1.33, 12, 1.35, 14, 1.29, 15, 1.3, 19,
						1.28, 20, 1.27, 32, 1.26, 33, 1.24, 34, 1.25, 44, 1.24,
						52, 1.23, 53, 1.24, 64, 1.23, 67, 1.22, 68, 1.23, 69,
						1.22, 71, 1.23, 72, 1.22, 73, 1.23, 81, 1.22, 82, 1.23,
						1.22]
			});

	sIFR.replace(avenirLight, {
				selector : '.tips h2',
				wmode : 'transparent',
				css : ['.sIFR-root {color: #7b7b7b; leading: 10;}']
			});
	sIFR.replace(avenirLight, {
				selector : '.tips p',
				wmode : 'transparent',
				css : ['.sIFR-root {color: #7b7b7b; leading: 10;}'],
				ratios : [8, 1.41, 9, 1.33, 12, 1.35, 14, 1.29, 15, 1.3, 19,
						1.28, 20, 1.27, 32, 1.26, 33, 1.24, 34, 1.25, 44, 1.24,
						52, 1.23, 53, 1.24, 64, 1.23, 67, 1.22, 68, 1.23, 69,
						1.22, 71, 1.23, 72, 1.22, 73, 1.23, 81, 1.22, 82, 1.23,
						1.22]
			});
	sIFR.replace(avenirBook, {
				selector : '.avenir-book-h3 h3',
				wmode : 'transparent',
				css : ['.sIFR-root {color: #7b7b7b;}'],
				ratios : [8, 1.41, 9, 1.33, 12, 1.35, 14, 1.29, 15, 1.3, 19,
						1.28, 20, 1.27, 32, 1.26, 33, 1.24, 34, 1.25, 48, 1.24,
						52, 1.23, 53, 1.24, 73, 1.23, 76, 1.22, 77, 1.23, 78,
						1.22, 80, 1.23, 81, 1.22, 82, 1.23, 1.22]
			});
	sIFR.replace(avenirBook, {
				selector : '.promo-1 h3, .promo-1 h4',
				wmode : 'transparent',
				css : ['.sIFR-root {color: #7b7b7b; leading: -10;}'],
				ratios : [8, 1.41, 9, 1.33, 12, 1.35, 14, 1.29, 15, 1.3, 19,
						1.28, 20, 1.27, 32, 1.26, 33, 1.24, 34, 1.25, 48, 1.24,
						52, 1.23, 53, 1.24, 73, 1.23, 76, 1.22, 77, 1.23, 78,
						1.22, 80, 1.23, 81, 1.22, 82, 1.23, 1.22]
			});

	sIFR.replace(bahuer, {
		selector : '#subHeader h1',
		wmode : 'transparent',
		fitExactly : true,
		css : ['.sIFR-root {font-style:italic;color: #dd2559; leading: -10;text-transform: capitalize;}'],
		ratios : [8, 1.41, 10, 1.32, 11, 1.31, 15, 1.29, 19, 1.28, 26, 1.25,
				31, 1.24, 37, 1.23, 41, 1.22, 42, 1.23, 53, 1.22, 56, 1.21, 57,
				1.22, 92, 1.21, 93, 1.2, 97, 1.21, 98, 1.2, 101, 1.21, 103,
				1.2, 104, 1.21, 108, 1.2, 109, 1.21, 113, 1.2, 114, 1.21, 118,
				1.2, 119, 1.21, 1.2]
	});

	sIFR.replace(avenirBook, {
				selector : '#promos h3, #promos h4',
				wmode : 'transparent',
				css : ['.sIFR-root {color: #7b7b7b; leading: -10;text-align:right;}'],
				ratios : [8, 1.41, 9, 1.33, 12, 1.35, 14, 1.29, 15, 1.3, 19,
						1.28, 20, 1.27, 32, 1.26, 33, 1.24, 34, 1.25, 48, 1.24,
						52, 1.23, 53, 1.24, 73, 1.23, 76, 1.22, 77, 1.23, 78,
						1.22, 80, 1.23, 81, 1.22, 82, 1.23, 1.22]
			});
	sIFR.replace(bahuer, {
		selector : 'h1',
		wmode : 'transparent',
		fitExactly : true,
		offsetLeft:8,
		tuneWidth:8,
		css : ['.sIFR-root {color: #a4b40f;leading: -15;letter-spacing: -3; text-transform: lowercase;}'],
		ratios : [8, 1.41, 10, 1.32, 11, 1.31, 15, 1.29, 19, 1.28, 26, 1.25,
				31, 1.24, 37, 1.23, 41, 1.22, 42, 1.23, 53, 1.22, 56, 1.21, 57,
				1.22, 92, 1.21, 93, 1.2, 97, 1.21, 98, 1.2, 101, 1.21, 103,
				1.2, 104, 1.21, 108, 1.2, 109, 1.21, 113, 1.2, 114, 1.21, 118,
				1.2, 119, 1.21, 1.2]
	});
};
    initSifr();

/**
 * Modal window initialization
 * 
 * @param none
 * @returns none
 */
var linkUrl;
var initModals = function() {
//this.modalElements = "<div class='share-modal' style='display:none'><div class='sm-wrap'></div></div>";
this.modalElements = "<div class='before-save-modal' style=''><div class='bs-wrap'></div></div>";
this.modalElements += "<div class='overlay' id='overlay'><div class='wrap'></div></div>";

$('body').append(this.modalElements);



this.loadModal = $('#overlay').overlay({
    oneInstance: false,
    api: true,
    onLoad: function() {
        var wrapper = this.getContent().find('div.wrap');
        
            wrapper.load(linkUrl);
    
    },
    expose: {
        color: '#a5a5a5',
        opacity: 0.7,
        closeSpeed: 1000
    }
});


	/**
	 * Initializes sharing options modal
	 * 
	 * @private
	 * @param none
	 * @returns none
	 

	this.shareModal = $('div.share-modal').overlay({
				oneInstance : false,
				api : true,
				onLoad : function() {
					var wrap = this.getContent().find('div.sm-wrap');
					if (wrap.is(':empty')) {
						wrap.load('../../share/default.aspx', null, function() {
									$('#shareForm').jFormm();
									focusClear();
								}); // this load the share form in page 7
					}
				},
				expose : {
					color : '#a5a5a5',
					opacity : 0.7,
					closeSpeed : 1000
				}
			});*/

	/**
	 * Initializes register before saving modal
	 * 
	 * @private
	 * @param none
	 * @returns none
	 */

	this.beforeSaveModal = $('div.before-save-modal').overlay({
				oneInstance : false,
				api : true,
				onLoad : function() {
					var wrap = this.getContent().find('div.bs-wrap');
					if (wrap.is(':empty')) {
						wrap.load('beforeSaveModal.html'); // this one loads the html for page 1
					}
				},
				expose : {
					color : '#a5a5a5',
					opacity : 0.7,
					closeSpeed : 1000
				}
			});
};



var closeFancyModal = function() {
$.fn.fancybox.close()
}


/**
 * Displays Modal window for "Add this" and recommend form
 * 
 * @public
 * @param none
 * @returns none
 */

var showShareModal = function() {
	//this.shareModal.load();
};

/**
 * Displays Modal window for Alert before saving
 * 
 * @public
 * @param none
 * @returns none
 * @type none
 */
var showBeforeSaveModal = function() {
	this.beforeSaveModal.load();
};


var showExtModal = function(url) {
    linkUrl = url;
    this.loadModal.load();
    return false;
};
/**
 * Set equal heights in all modules presented in home page
 * 
 * @public
 * @param none
 * @returns none
 */
var adjustHomeModules = function() {
	var moduleContainer = $('#modules');
	var modules = $('#modules > div.module');
	var moduleHeight = moduleContainer.height();

	$.each(modules, function() {
				$(this).height(300);
			});

};

/**
 * Clear input fields when user clicks on it
 * 
 * @public
 * @param none
 * @returns none
 */
/*var focusClear = function() {
	var defaultData = '';

	// clear input on focus
	$('input:text').focus(function() {
				defaultData = $(this).val();
				$(this).val('');
			});

	// if field is empty afterward, add text again
	$('input:text').blur(function() {
				if ($(this).val() === '') {
					$(this).val(defaultData);
				}
			});

};*/
$.fn.focusClear = function() {
    return this.focus(function() {
        if (this.value == this.defaultValue) {
            this.value = "";
        }
    }).blur(function() {
        if (!this.value.length) {
            this.value = this.defaultValue;
        }
    });
};


/**
 * Languages tooltip initialization
 * 
 * @public
 * @param none
 * @returns none
 */

var initLangTooltip = function() {
	var tooltip = $('#languageLink').qtip({
				// content: 'This is an active list element',
				content : {
					url : 'listOfLanguages.html' //declare html
				},
				show : 'mouseover',
				hide : 'unfocus',
				// hide: false,
				style : {
					// name: 'defaults', // Inherit from preset style
					tip : 'topMiddle',
					border : {
						radius : 5,
						color : '#f9f9f9'
					},
					padding : '5px 0px 7px 0px',
					width : {
						min : 120, // Defaults to 0, but in IE it doesn't show
									// nothing
						max : 120
					}
				},
				position : {
					corner : {
						target : 'bottomMiddle',
						tooltip : 'topMiddle'
					}
				},
				api : {
					onRender : function() {
						var qtip = $(this)[0].elements.tooltip;
						var qtipClose = qtip
								.append('<div class="close"></div>');
						$(qtipClose).bind('click', function(e) {
									$(qtip).qtip('hide'); // Hides the tooltip
								});
					}
				}

			});
	initQtipCloseEvents();
};

var initQtipCloseEvents = function() {
	// events to hide the tooltip when mouse leaves it
	$('.qtip').livequery(function() {
				// use the helper function hover to bind a mouseover and
				// mouseout event
				$(this).hover(function() {
						}, function() {
							$('.qtip').qtip('hide'); // Hides the tooltip
						});
			}, function() {
				// unbind the mouseover and mouseout events
				$(this).unbind('mouseover').unbind('mouseout');
			});

	// hide the tooltip when the user clicks some item
	$('.qtip a').livequery('click', function(e) {
				$('.qtip').qtip('hide'); // Hides the tooltip
			});
};

var initPhysicianTooltip = function(selectorId, content) {
    var tFavorites = $(selectorId).qtip({
        content: content,
        show: 'mouseover',
        hide: 'mouseout',
        // hide: false,
        style: {
            // name: 'defaults', // Inherit from preset style
            tip: 'bottomLeft',
            border: {
                radius: 2,
                color: '#da2a3e'
            },
            background: '#da2a3e',
            color: '#fff',
            padding: '0px 4px 0px 4px',
            textAlign: 'center',
            fontSize: '11px',
            fontWeight:'bold',
            width: {
                min: 100
                // Defaults to 0, but in IE it doesn't show nothing
                // ,max: 100
            },
            height:15
        },
        position: {
            corner: {
                target: 'topMiddle',
                tooltip: 'bottomLeft'
            }
        },
        api: {
            onRender: function() {
                var qtip = $(this)[0].elements.tooltip;
                qtip.addClass('qtip-physician');
            }
        }
    });
};

var goToPage = function(pageName) {
    window.location.href = pageName;

}
/*
 Individual Select decorator
*/


jQuery.fn.selectdecorator = function($options) {


    var defaults = {
        DIVClass: 'Select_Decorator_Holder',
        onChange: function() { }
    };

    $current = jQuery(this);
    $ID = $current.attr('id');
    jQuery.extend(defaults, $options);

    newSelect = '<div>';

    newSelect += '<select';
    newSelect += ' name="' + $current.attr('name');
    newSelect += '" id="' + $ID;
    newSelect += '" class="' + $current.attr('class');
    newSelect += '">';
    newSelect += $current.html();
    newSelect += '</select>';
    newSelect += '<span></span>';
    newSelect += '<ul class="selectDeco">';
    jQuery.each($current.find('option'), function() {
        newSelect += '<li val="' + jQuery(this).attr('value') + '">';
        newSelect += jQuery(this).html();
        newSelect += '</li>';
    });
    newSelect += '<ul>';
    newSelect += '</div>';

    $decorator = jQuery(newSelect);
    $decorator.addClass(defaults['DIVClass']);


    $current.parent().append($decorator);
    $current.remove();


    $decorator.find('ul').css('position', 'absolute');
    $decorator.find('ul').css('z-index', '999999');
    $decorator.find('ul').hide();
    $decorator.find('span').css('display', 'block');

    $decorator.find('span').bind('click', function() {
        jQuery(this).parent().find('ul').slideDown('fast', function() {
            jQuery('body').bind('click', function() {
                jQuery(this).parent().find('ul.selectDeco').hide();
            });
        });
    }
	);

    $decorator.find('ul>li').bind('click', function() {
        jQuery(this).parent().parent().find('select').val(jQuery(this).attr('val'));
        jQuery(this).parent().parent().find('span').parent().unbind('click');
        jQuery(this).parent().hide();
        jQuery(this).parent().parent().find('span').html(jQuery(this).html());

        if (defaults['onChange'] != null) {
            defaults.onChange();
        }

    });

    $newSelect = jQuery('#' + $ID);

    $newSelect.attr('style', 'opacity:0;position:absolute;top:-3000px;');

    $setFlag = 0;
    $newSelect.find('option').each(
		function() {
		    if ($setFlag == 0) {
		        $newSelect.parent().find('span').html(jQuery(this).html());
		        $newSelect.val(jQuery(this).val());
		        $setFlag = 1;
		    }
		}
	);

}


/*
* Inits the left navigation menu behavior and styling
* 
* @public
* @param none
* @returns none 
**/


var initSideNav = function() {
	/*$("div#sideNav dt:not(.current) + dd").hide();
	$("div#sideNav dt a").hover(function() {
				$me = $(this);
				$me.parent().siblings().removeClass("current");
				$me.parent().addClass("current");

				$("dd:visible").hide();
				$me.parent().next().show();
				return false;
			});*/
    $("#sideNav dd ul li a.current").parent().addClass("current");
};

/*
 * 
 * Global on Dom ready initializations
 * 
 */
$(window).load(function() {
    /* Detect IE and set class for body */
    var isIE = $.browser.msie;
    if (isIE) {
        $('body').addClass('isIE');
    }
    /*
    var zIndexNumber = 1000;
    $('div').each(function() {
    $(this).css('zIndex', zIndexNumber);
    zIndexNumber -= 10;
    });
    */


    /* navigation behavior */
    $('#globalNav > .items').superfish().find('ul').bgIframe({ opacity: false });

    /* Initialize Modal */
    initModals();

    //focusClear();

    $("div.search input:text, #finder input:text").focusClear();

    /* Initialize Tooltip that shows the languages list */
    initLangTooltip();

    /* Binds Modal to "Share" link */

    $("#shareLink, .contentBox").fancybox({
        'frameHeight': 500,
        'frameWidth': 600,
        'hideOnContentClick':false
    });
    $("a.profileEmail").fancybox({
        'frameHeight': 450,
        'frameWidth': 700,
        'hideOnContentClick': false
    });


    /*
    * Global initialization of section specific functionalities
    * Uncomment if config.js is not being used.
    */


    adjustHomeModules();
    initSideNav();
    initScrollPane();


    $("#tabs ul").tabs("#pages > div", { history: true });
    initPhysicianTooltip('.action .favorites', 'Add to Favorites');
    initPhysicianTooltip('.action .share', 'Share Information');
    initPhysicianTooltip('.action .delete', 'Remove from Favorites');
    initPhysicianTooltip('.action .up', 'Up');
    initPhysicianTooltip('.action .down', 'Down');
    $('textarea#expandingText').autoResize({ onResize: function() {
        initScrollPane();
    }
    });


    $("a.maps").fancybox({
    'frameWidth': 800,
    'frameHeight':550
    });

    $("a.iframe").fancybox();

    $("a.ajax").fancybox();


    $("div.search select").selectdecorator(
		         {
		             DIVClass: "searchSelect"
		         }
		      );

});  // ends global init
