(function(jQuery) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  jQuery.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function validateStayConnected(){

	jQuery('#sc-firstname').validate({
            expression: 'if (VAL) return true; else return false;',
            message: 'Please enter a first name'
        });
        jQuery('#sc-lastname').validate({
            expression: 'if (VAL) return true; else return false;',
            message: 'Please enter a last name'
        });
	jQuery('#sc-email').validate({
                    expression: 'if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;',
                    message: 'Please enter a valid e-mail address'
                });
	jQuery('#submit-sc').submit();

}

function validateMoreInfo(){

	jQuery('#rc-firstname').validate({
            expression: 'if (VAL) return true; else return false;',
            message: 'Please enter a first name'
            
        });
        jQuery('#rc-lastname').validate({
            expression: 'if (VAL) return true; else return false;',
            message: 'Please enter a last name'
        });
	//jQuery('#rc-phone').validate({
                  //  expression: 'if (VAL.match(/^[0-9][0-9]{9}$/)) return true; else return false;',
                 //   message: 'Please enter a phone number'
               // });
	jQuery('#submit-rc').submit();
	
}

jQuery(document).ready(function(){

    if (jQuery('body.home').length) { // && /stg/.test(window.location.hostname)

        var html_build = '<div><span class="close">close</span><h1>Welcome to Entereg.com</h1><p>In December 2011, Adolor Corporation was acquired by<br>Cubist Pharmaceuticals.<p><a href="http://www.cubist.com/">Click here</a> for more information or close to go to Entereg.com.</p><p id="link-cubist"><a href="http://www.cubist.com/">Cubist</a></p></div>';

        jQuery.fn.khmodal({ id: 'popup-welcome', html_build: html_build, auto_load: true, bg_close: false, valign: 100 });
        var $display = jQuery('#popup-welcome');
        var $popup_bg = jQuery('#popup-bg');

        jQuery('#popup-welcome .close').click(function () {
            $display.fadeOut(200);
            $popup_bg.fadeOut(200);
            setTimeout(function () { $popup_bg.remove(); $display.remove(); }, 1000);

        });



   //alert('jquery common working');
   /*
   //Preload images-Homepage Slide 1
   jQuery.preLoadImages("Info_on_Entereg_Box.png", "/images/Message_1/Info_on_Entereg_Box.png");
   jQuery.preLoadImages("Download_Brochure_Line.png", "/images/Message_1/Download_Brochure_Line.png");
   jQuery.preLoadImages("Dosing_and_Admin_Line.png", "/images/Message_1/Dosing_and_Admin_Line.png");
   jQuery.preLoadImages("Bowel_Res_Order_Sets_Line.png", "/images/Message_1/Bowel_Res_Order_Sets_Line.png");
   
   //Preload images-Homepage Slide 2
   jQuery.preLoadImages("Entereg_Economic_Persp_Box.png", "/images/Message_2/Entereg_Economic_Persp_Box.png");
   jQuery.preLoadImages("Read_Now_Button.png", "/images/Message_2/Read_Now_Button.png");
   
   //Preload images-Homepage Slide 3
   jQuery.preLoadImages("See_What_HCPs_Say_Box.png", "/images/Message_3/See_What_HCPs_Say_Box.png");
   jQuery.preLoadImages("Watch_Now_Button.png", "/images/Message_3/Watch_Now_Button.png");
   
   //Preload images-Homepage Slide 4
   jQuery.preLoadImages("Entereg_Stay_Connected_Box.png", "/images/Message_4/Entereg_Stay_Connected_Box.png");
   jQuery.preLoadImages("Receive_Updates_Button.png", "/images/Message_4/Receive_Updates_Button.png");
   
   //Preload images-Homepage Slide 5
   jQuery.preLoadImages("Entereg_Contact_Rep_Box.png", "/images/Message_5/Entereg_Contact_Rep_Box.png");
   jQuery.preLoadImages("Request_More_Info_Button.png", "/images/Message_5/Request_More_Info_Button.png");
   */
    
    //slideshow
    jQuery('#messages').cycle({
    	speed: 'slow', // default animation transition speed
	interval: 8000 // default interval between image change
    });
   
  
      //lightbox-external links        
        jQuery('a[href^="http://"], a[href^="https://"], body.order_sets a#orderset, body.order_sets a#orderset2').click(function(e) {
         var extUrl = (this.href);
          jQuery('#external-link').lightbox_me({
           centered: true,
           closeSelector:".close",
           onLoad: function() {
           jQuery('#link-go').click(function() {
		  //alert(extUrl);
		  window.open(extUrl);
		});
           jQuery('#external-link').find('input:first').focus()
            }
        });
    e.preventDefault();
});  



    //lightbox-receive updates  
        jQuery('#stay, #btn-updates').click(function(e) {
          jQuery('#connected').lightbox_me({
           centered: true,
           closeSelector:".close",
           onLoad: function() {
            jQuery('#submit-sc').click(function() {
		  //alert('clicked');
		validateStayConnected();
		});
            jQuery('#connected').find('input:first').focus()
            }
        });
    e.preventDefault();
});

    //lightbox-more info  
        jQuery('#request, #btn-support').click(function(e) {
          jQuery('#more-info').lightbox_me({
           centered: true,
           closeSelector:".close",
           onLoad: function() {
           jQuery('#submit-rc').click(function() {
		  //alert('clicked');
		validateMoreInfo();
		});
            jQuery('#more-info').find('input:first').focus()
            }
        });
    e.preventDefault();
});






} 
//Form Vaidation
//Stay Connected


//More Info



});//END: doc.ready

(function($){
	$.fn.khmodal = function( options ){
		var defaults = {
			'auto_load' : false,
			'id' : 'modal',
			'html_build' : '<p class="load">loading...</p>',
			'bg_close' : true,
			'bg_opacity' : 0.7,
			'fade_in_speed' : 'slow',
			'fade_out_speed' : 'slow',
			'valign':'center', // if number is set, it will align that specifc # of pixels from top
			'halign':'center' // if number is set, it will align that specifc # of pixels from left
		};
		var options = $.extend( defaults, options );
		
		var popup_status = 0;
		
		//autoload? or do onclick
		if(options.auto_load){
			popup_build();
		}else{
			this.click(function(){
				popup_build();
			});
		}	
		
		function popup_build(){
			$('body').append('<div id="'+options.id+'" class="popup"><span id="submit"></span>'+((options.bg_close)?'<span class="close" title="Close Popup">Cancel</span>':'')+'<div class="d"></div></div><div id="popup-bg"></div>');
			$display = $('#'+options.id); //build after creation
			$popup_bg = $("#popup-bg"); //build after creation
		
			popup_load($display,$popup_bg);
			$('.d',$display).html(options.html_build);
		
			if(options.bg_close){
				$popup_bg.add($('.close',$display)).click(function(){popup_disable($display);});
				$(document).keypress(function(e){ if(e.keyCode==27 && popup_status==1){ popup_disable($display); } });
			}
			return $display;
		}
		function popup_load($display,$popup_bg){
			//loads popup only if it is disabled
			if(popup_status==0){
				$popup_bg.css({"opacity": options.bg_opacity});
				$popup_bg.fadeIn(options.fade_in_speed);
				$display.show();
				popup_status = 1;
		
				//center popup
				var windowWidth = document.documentElement.clientWidth;
				var windowHeight = document.documentElement.clientHeight;
				var popupHeight = $display.height();
				var popupWidth = $display.width();
				//centering
				$display.css({"left": options.halign=='center'?windowWidth/2-popupWidth/2:options.halign,"top": (options.valign == 'center')?(windowHeight - popupHeight)/2:getYX('Y')+options.valign+'px'});
				//only need force for IE6
				//$popup_bg.css({"height": windowHeight});
			}
		}
		function popup_disable($display){
			//disables popup only if it is enabled
			if(popup_status==1){
				$display.fadeOut(options.fade_out_speed);
				$('#popup-bg').fadeOut(options.fade_out_speed);
				setTimeout(function () { $('#popup-bg').remove();$display.remove(); }, 1000);
				popup_status = 0;
			}
		}
	}
	
})(jQuery);

eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('k g(a){f b=0,3=0;4(i(9.c)==\'e\'){3=9.c;b=9.h}8 4(1.5&&(1.5.6||1.5.7)){3=1.5.7;b=1.5.6}8 4(1.2&&(1.2.6||1.2.7)){3=1.2.7;b=1.2.6}4(a=="j"){d b}8 4(a=="l"){d 3}}',22,22,'|document|documentElement|scrOfY|if|body|scrollLeft|scrollTop|else|window|||pageYOffset|return|number|var|getYX|pageXOffset|typeof|X|function|Y'.split('|'),0,{}))
