function submitSearchForm () {
	$("#txtKeywords").val( $("#txtKeywords").val().replace(/<(?:.|\s)*?>/g, "") );
	submitForm();
}

function watchSearchForm(e) {
    var evt = e ? e : window.event;
    if (evt.keyCode == 13){
        submitSearchForm();
        return false;
    }
}

var Viagra = function () {};

Viagra.init = function ( components ) {
    for (var i = 0; i < components.length; i++ ) {
        components[i].init();
    }
}

Viagra.ContentCallouts = {
	init : function () {
		$('.more .box').mouseover(function () {
			$(this).addClass('box_on');
		}).mouseout(function () {
			$(this).removeClass('box_on');
		});
	}
}

Viagra.Menu = {
    timeout_length : 500,
    close_timeout  : 0,
    current_item   : null,
    current_hilite : null,

    init : function () {
        var self = this;
        $('#main_nav > li').mouseover( function () { self.open( this ) });
        $('#main_nav > li').mouseout( function () { self.set_timer() });
        document.onclick = function () { self.close() };
        if ( self.section_to_highlight ) {
			$('#main_nav_item_' + self.section_to_highlight).addClass('inside');
        }
        var $last_menu_item = $("#main_nav_item_4");
        var $last_menu      = $("#main_nav :last-child ul");
        $last_menu.css(
			"margin",
			"0px 0px 0px "
			+ ($last_menu_item.width() - $last_menu.width() + 39 )
			+ "px"
		);
    },

    open : function ( e ) {
        var self = this;
        self.cancel_timer();
        self.close();
        self.current_item   = $(e).find('ul').css('visibility', 'visible');
        self.current_hilite = $(e).find('a').addClass('active');
    },

    close : function () {
        var self = this;
        if ( self.current_item ) {
            self.current_item.css('visibility', 'hidden');
            self.current_hilite.removeClass('active');
        }
    },

    set_timer : function () {
		var self = this;
        self.close_timeout = window.setTimeout(
            function () { self.close() },
            self.timeout_length
        );
    },

    cancel_timer : function () {
        var self = this;
        if ( self.close_timeout ) {
            window.clearTimeout( self.close_timeout );
            self.close_timeout = null;
        }
    }
};

Viagra.Flash = {
    // members ...
    min_version    : "9.0.0",
    default_width  : 580,
    default_height : 335,
    // methods ...
    embedSWF: function (opts) {
		var self = this;
		var $container = $(opts["container"] || '.flash_container');

		var id;
		if (opts["swf"]) {
			id = self._clean_swf_id( opts["swf"] );
		}
		else {
			id = "viagra_video_player"
		}

		// get the flashvars, and ...
		var flashvars = opts["flashvars"] || {};
		// make sure to include the fonts
		flashvars.fontsURLs = "common/swf/fonts/Arial.swf,common/swf/fonts/Gotham.swf"

		$container.html("<div id='" + id + "'></div>");
		swfobject.embedSWF(
			opts["full_swf_path"] || "PublicResources/ContentAssets/" + opts["swf"],
			id,
			opts["width"]       || self.default_width,
			opts["height"]      || self.default_height,
			opts["min_version"] || self.min_version,
			opts["installer"]   || null,
			flashvars,
			opts["params"]      || { wmode : "opaque" },
			opts["attrs"]       || {},
			opts["callback"]    || function (e) {
				if (e.success == false) {
					self._populate_no_flash_message( $container, opts["swf"] );
				}
			}
		);
    },
    embedVideo: function (opts) {
		var self = this;
		self.embedSWF({
		    full_swf_path : "common/swf/ViagraStandaloneVP.swf",
		    width         : "560",
		    height        : "315",
		    installer     : "ViagraStandaloneVP",
		    flashvars: { dataHTMLstr: self._generate_dataHTML_str(opts), taggingXMLPath:'common/swf/xml/omniture_matrix.xml', deepLinkingXMLPath:'common/swf/xml/deeplink_matrix.xml' },
		    attrs         : { id : "ViagraStandaloneVP" },
		    params        : {
		        menu              : "false",
    			scale             : "noScale",
    			allowFullscreen   : "true",
    			allowScriptAccess : "always",
    			bgcolor           : "#869ca7"
		    },
		    callback : function (e) {
				if (e.success == false) {
					self._populate_no_flash_message( $('.flash_container'), opts["id"] );
				}
			}
		})
    },
    embedQuestionsVideo: function (opts) {
		var self = this;

		var flashvars = opts["flashvars"] || {};
		// make sure to include the fonts
		flashvars.fontsURLs = "common/swf/fonts/Arial.swf,common/swf/fonts/Gotham.swf";
		flashvars.taggingXMLPath = 'common/swf/xml/omniture_matrix.xml';
		flashvars.deepLinkingXMLPath = 'common/swf/xml/deeplink_matrix.xml';
		flashvars.xmlSrc = 'common/swf/html_data/ViagraAccordion-config.html';

		self.embedSWF({
		    full_swf_path : "common/swf/ViagraAccordion.swf",
		    width         : "782",
		    height        : "761",
		    flashvars     : flashvars,
		    attrs         : { id : "ViagraAccordion" },
		    params        : {
				menu: "false",
				scale: "noScale",
				allowFullscreen: "true",
				allowScriptAccess: "always",
				bgcolor: "#ffffff"
		    },
		    callback : function (e) {
				if (e.success == false) {
					$(".flash_container").css("height" , "315px");
					$(".flash_container").css("width" , "560px");
					self._populate_no_flash_message( $('.flash_container'), opts["id"] );
				}
			}
		})
    },
    embedFCP: function (opts) {
		var self = this;
		var $container = $(opts["container"]);

		var id = ( opts["container"] );
		var new_id = id.replace('#', '');
		new_id = new_id.replace('container', 'embed');

		// get the flashvars, and ...
		var flashvars = opts["flashvars"] || {};
		// make sure to include the fonts
		flashvars.fontsURLs = "common/swf/fonts/Arial.swf,common/swf/fonts/Gotham.swf"
		flashvars.taggingXMLPath = 'common/swf/xml/omniture_matrix.xml';
		flashvars.deepLinkingXMLPath = 'common/swf/xml/deeplink_matrix.xml';
        flashvars.xmlSrc = 'common/swf/html_data/ViagraFCP-config_1_' + opts["index"] + '.html';

		$container.html("<div id='" + new_id + "'></div>");
		swfobject.embedSWF(
			opts["full_swf_path"] || "common/swf/ViagraStandaloneFCP.swf",
			new_id,
			opts["width"]       || 917,
			opts["height"]      || 335,
			opts["min_version"] || self.min_version,
			opts["installer"]   || null,
			flashvars,
			opts["params"]      || { wmode : "opaque" },
			opts["attrs"]       || {},
			opts["callback"]    || function (e) {
				if (e.success == false) {
					$(id).css("display" , "none");
				} else {
					$(id).next().css("display" , "none");
				}
			}
		);
    },
    embedMenuVideoPlayer : function () {
    	var self = this;
		self.embedSWF({
		    full_swf_path : "common/swf/ViagraMenuPlayer.swf",
		    width         : "792",
		    height        : "316",
		    installer     : "ViagraMenuPlayer",
		    flashvars: { dataHTMLstr: self._generate_dataHTML_for_MenuPlayer_str(), taggingXMLPath: 'common/swf/xml/omniture_matrix.xml', deepLinkingXMLPath: 'common/swf/xml/deeplink_matrix.xml' },
		    attrs         : { id : "ViagraMenuPlayer" },
		    params        : {
		        menu              : "false",
    			scale             : "noScale",
    			allowFullscreen   : "true",
    			allowScriptAccess : "always",
    			bgcolor           : "#869ca7"
		    },
		    callback : function (e) {
				if (e.success == false) {
					self._populate_no_flash_message( $('.flash_container'), 'menu' );
				}
			}
		})
    },
    _generate_dataHTML_str : function (opts) {
		var plugin = opts["plugin"];
		if (plugin) {
			plugin += "?flv=../../PublicResources/VideoAssets/Videos/ImpSafetyInfo.flv";
		}
        var data = "<div id='videoPlayerContainer'>"
                 + "<video src='../../PublicResources/VideoAssets/Videos/" + opts["id"] + "_p" + opts["position"] + ".flv' />" // <-- was once mp4
                 + "<xml>"
                    + "<images>"
                        + "<img id='startBadge' src='../../PublicResources/VideoAssets/PreRoll/" + opts["id"] + ".jpg' />"
                        + "<img id='endBadge' src='../../PublicResources/VideoAssets/PostRoll/" + opts["id"] + "_end.jpg' />"
                        + "<img id='pausedBadge' src='' />"
                    + "</images>"
                    + "<plugin src='" + plugin + "' />"
                + "</xml>"
            + "</div>";
        if (encodeURIComponent) {
			data = encodeURIComponent(data);
		}
		return data;
    },
    _generate_dataHTML_for_MenuPlayer_str : function () {
		var str ="<div id='videoPlayerContainer'>";
    			str+="<video src='../../PublicResources/VideoAssets/Videos/3.1.2_T1.flv'>"; // <-- was once mp4
    				str+="<xml>";
    					str+="<title>How did ED affect you?</title>";
    					str+="<images>";
    						str+="<img id='startBadge' src='../../PublicResources/VideoAssets/PreRoll/Testimonial_1.jpg' />";
    						str+="<img id='endBadge' src='../../PublicResources/VideoAssets/PostRoll/Testimonial_post.jpg' />";
    						str+="<img id='pausedBadge' src='' />";
    					str+="</images>";
    					str+="<plugin src='' />";
    				str+="</xml>";
    			str+="</video>";
    			str+="<video src='../../PublicResources/VideoAssets/Videos/3.1.2_T2.flv'>"; // <-- was once mp4
    				str+="<xml>";
    					str+="<title>What did you think was causing your ED?</title>";
    					str+="<images>";
    						str+="<img id='startBadge' src='' />";
    						str+="<img id='endBadge' src='../../PublicResources/VideoAssets/PostRoll/Testimonial_post.jpg' />";
    						str+="<img id='pausedBadge' src='' />";
    					str+="</images>";
    					str+="<plugin src='' />";
    				str+="</xml>";
    			str+="</video>";
    			str+="<video src='../../PublicResources/VideoAssets/Videos/3.1.2_T3.flv'>"; // <-- was once mp4
    				str+="<xml>";
    					str+="<title>How long did you wait before asking your doctor?</title>";
    					str+="<images>";
    						str+="<img id='startBadge' src='' />";
    						str+="<img id='endBadge' src='../../PublicResources/VideoAssets/PostRoll/Testimonial_post.jpg' />";
    						str+="<img id='pausedBadge' src='' />";
    					str+="</images>";
    					str+="<plugin src='' />";
    				str+="</xml>";
    			str+="</video>";
    			str+="<video src='../../PublicResources/VideoAssets/Videos/3.1.2_T4.flv'>"; // <-- was once mp4
    				str+="<xml>";
    					str+="<title>How did you ask your doctor?</title>";
    					str+="<images>";
    						str+="<img id='startBadge' src='' />";
    						str+="<img id='endBadge' src='../../PublicResources/VideoAssets/PostRoll/Testimonial_post.jpg' />";
    						str+="<img id='pausedBadge' src='' />";
    					str+="</images>";
    					str+="<plugin src='../../PublicResources/VideoAssets/Videos/3.1.2_T6.flv' />"; // <-- was once mp4
    				str+="</xml>";
    			str+="</video>";
    			str+="<video src='../../PublicResources/VideoAssets/Videos/3.1.2_T5.flv'>"; // <-- was once mp4
    				str+="<xml>";
    					str+="<title>How did your doctor react?</title>";
    					str+="<images>";
    						str+="<img id='startBadge' src='' />";
    						str+="<img id='endBadge' src='../../PublicResources/VideoAssets/PostRoll/Testimonial_post.jpg' />";
    						str+="<img id='pausedBadge' src='' />"
    					str+="</images>";
    					str+="<plugin src='../../PublicResources/VideoAssets/Videos/3.1.2_T6.flv' />";  // <-- was once mp4
    				str+="</xml>";
    			str+="</video>";
    			str+="<video src='../../PublicResources/VideoAssets/Videos/3.1.2_T6.flv'>"; // <-- was once mp4
    				str+="<xml>";
    					str+="<title>How did you feel after the discussion?</title>";
    					str+="<images>";
    						str+="<img id='startBadge' src='' />";
    						str+="<img id='endBadge' src='../../PublicResources/VideoAssets/PostRoll/Testimonial_post.jpg' />";
    						str+="<img id='pausedBadge' src='' />";
    					str+="</images>";
    					str+="<plugin src='../../PublicResources/VideoAssets/Videos/3.1.2_T6.flv' />";  // <-- was once mp4
    				str+="</xml>";
    			str+="</video>";
    			str+="<video src='../../PublicResources/VideoAssets/Videos/3.1.2_T7.flv'>"; // <-- was once mp4
    				str+="<xml>";
    					str+="<title>What is the safety of VIAGRA? (With Dr. Steven Lamm)</title>";
    					str+="<images>";
    						str+="<img id='startBadge' src='' />";
    						str+="<img id='endBadge' src='../../PublicResources/VideoAssets/PostRoll/Testimonial_post.jpg' />";
    						str+="<img id='pausedBadge' src='' />";
    					str+="</images>";
    					str+="<plugin src='../../PublicResources/VideoAssets/Videos/3.1.2_T6.flv' />";  // <-- was once mp4
    				str+="</xml>";
    			str+="</video>";
    		str+="</div>";
    	if (encodeURIComponent) {
			str = encodeURIComponent(str);
		}
    	return str;
    },
    _clean_swf_id : function (id) {
		return id.replace('.', '_').replace('/', '_');
    },
    _populate_no_flash_message : function (e, id) {
		var out = "<a target='_blank' href='http://get.adobe.com/flashplayer/'><img style='margin:0px' src='common/images/";
		if (id == 'menu') {
			out += "3.1.2_NOFLASH.jpg";
		}
		else if (id == 'ViagraAccordion') {
			out += "Video_NOFLASH.jpg";
		}
		else if (id == '1.4.1_p1.swf' || id == '2.3.1_p1.swf') {
			out += "1.4.1_2.3.1_NOFLASH.jpg";
		}
		else if (id == '2.4/2.4_p1.swf') {
			out += "2.4_NOFLASH.jpg";
		}
		else {
			out += "Video_NOFLASH.jpg";
		}
		$(e).html(out + "' border='0' /></a>");
    }
};

Viagra.Carousel = {
    // members ...
    item_width  : 310,
    num_visible : 3,
    num_items   : 7,
    max_scroll  : 4,
    nav         : null,
    carousel    : null,
    anchor      : null,
    // state info ...
    current_lightbox_index : null,
    current_scroll_index   : null,
    is_carousel_active     : true,
    auto_scroll_interval   : null,
    // methods ....
    init : function () {

        if ($('.main_carousel').length == 0) return;

		var self = this;

        self.carousel = $('.main_carousel_inner');
        self.nav      = $('.main_carousel_nav');

        var url = document.location.toString();
        if (url.match('#')) {
			self.anchor = url.split('#')[1];
			self.scrollTo( self.anchor );
		}
		else {
			self.scrollTo( 0 );
		}

        self.nav.find('.forward').click(function () {
            self.scrollBy( -self.item_width );
			clearInterval( self.auto_scroll_interval );
			self.startAutoScroll();
        });

        self.nav.find('.back').click(function () {
            self.scrollBy( self.item_width );
            clearInterval( self.auto_scroll_interval );
            self.startAutoScroll();
        });

        /*
        self.nav.find('.box').each(function (i) {
            var pos = self.num_items - (i + 1);
            $(this).click(function () {
                self.scrollTo( pos );
                clearInterval( self.auto_scroll_interval );
                self.startAutoScroll();
            })
        });
        */

       $('.main_lightbox_menu_item').each(function () {
        	$(this).css("cursor", "pointer");
			$(this).click(function () {
				$('.main_lightbox_menu_item').removeClass('li_on');
				$('.main_lightbox_blurb_div').hide();
				$(this).addClass('li_on');
				$('#' + $(this).attr('id').replace('menu', 'content')).show();
			});
		});

		self.startAutoScroll();
    },

    startAutoScroll : function () {
		var self = this;
		self.auto_scroll_interval = setInterval(function () {
			if (self.current_scroll_index == self.max_scroll) {
				self.scrollTo( 0, 200 );
			}
			else {
				self.scrollTo( self.current_scroll_index + 1, 400 );
			}
		}, 5000);
    },

    scrollBy : function ( distance ) {
		var self = this;

        var current_left = parseInt( self.carousel.css('left') );
        var state        = Math.abs( current_left / self.item_width );

        var going_back    = distance > 0;
        var going_forward = distance < 0;

        if (state                    == 0              && going_back    == true) return;
        if (state + self.num_visible == self.num_items && going_forward == true) return;

		if (self.is_carousel_active == false) return;
		self.is_carousel_active = false;

        self.carousel.animate(
			{ left : current_left + distance },
			200,
			function () { self.is_carousel_active = true }
		);
        self.setNavState( going_back ? --state : ++state );
    },

    scrollTo : function ( index, speed ) {
        var self = this;

        if (self.is_carousel_active == false) return;
        self.is_carousel_active = false;

        var max_index = Math.min( index, self.max_scroll );
        self.carousel.animate(
			{ left : -( max_index * self.item_width) },
			speed || 200,
			function () { self.is_carousel_active = true }
		);
        self.setNavState( max_index );
    },

    setNavState : function ( start ) {
        var self = this;

        var $boxes = self.nav.find('.box');
        $boxes.removeClass('box_on');

        var real_start;

        if (start == null) {
            real_start = (self.num_items - 1);
        }
        else {
            real_start = (self.num_items - start) - 1;
        }

        for ( var i = 0; i < self.num_visible ; i++ ) {
            $boxes.eq( real_start-- ).addClass('box_on');
        }

        self.current_scroll_index = start;
    },

    showLightBoxBackUp: function ( index ) {
        var self = this;
        var $box = $('#main_carousel_lightbox_' + index);
        Viagra.Flash.embedFCP({
            "index"     : index,
            "container" : "#fcp" + index + "_flash_container"
        });
		$box.fadeIn( 200 , null );
		$box.find('.main_lightbox_menu_item:first').click();
		self.current_lightbox_index = index;
		clearInterval( self.auto_scroll_interval );
    },

    showLightBox: function( index, pagename, evar29 ) {
        var self = this;
        var $box = $('#main_carousel_lightbox_' + index);
        Viagra.Flash.embedFCP({
            "index": index,
            "container": "#fcp" + index + "_flash_container"
        });
        $box.fadeIn(200, null);
        $box.find('.main_lightbox_menu_item:first').click();
        self.current_lightbox_index = index;
        clearInterval(self.auto_scroll_interval);
        self.trackShowLightBox(pagename, evar29);
    },    
    trackShowLightBox: function( pagename, evar29 ) {
        s.pageName = s.prop3 = s.eVar3 = pagename;
        s.prop12 = 'lightbox:buttons';
        s.prop27 = s.eVar27 = 'routers';
        s.prop29 = s.eVar29 = evar29;
        s.prop1 = 'home';
        s.t();
    },
    hideLightBox: function () {
		var self = this;
		$('#main_carousel_lightbox_' + self.current_lightbox_index).fadeOut( 200 , null );
		$("#fcp" + self.current_lightbox_index + "_flash_container").html("");
		self.startAutoScroll();
    }
};







