

//////////////////////////////////////////////////////////
// Constants
//////////////////////////////////////////////////////////
// DOM Constants
var isDHTML = 0;
var isID = 0;
var isAll = 0;
var isLayers = 0;

// FontSize control constants
var _intCurrentFontSize = 0;
var FS_FONT_SIZE_BUTTON_1_ID = "lnkFontSize1";
var FS_FONT_SIZE_BUTTON_2_ID = "lnkFontSize2";
var FS_FONT_SIZE_BUTTON_3_ID = "lnkFontSize3";
var FS_AJAX_POST_URL = "/post/setTextSize.aspx?size=";

// Image toggle constants
var HIDE_IMAGES = false;


//////////////////////////////////////////////////////////
// Site
// The "Site" object should contain all of the code that adds behavior 
// to the various client side controls.
//////////////////////////////////////////////////////////
var Site = {
   
	start: function(){ 	
		
		// Add Font Size button behavior    
	    if ($(FS_FONT_SIZE_BUTTON_1_ID) && $(FS_FONT_SIZE_BUTTON_2_ID) && $(FS_FONT_SIZE_BUTTON_3_ID))  {
			Site.createFontSizer();
		}
		
		//Temporarily hide all the photos ULs and breakout DIVs, so they can be rearranged.
		if (HIDE_IMAGES) {
			var c = document.getElementById('content');
			var uls = c.getElementsByTagName('ul');
			var divs = c.getElementsByTagName('div');
			for (var i = 0; i < uls.length; i++) {
				if (uls[i].className == 'photos') {
					uls[i].style.display = 'none';
					uls[i].style.visibility = 'hidden';
				}
			}
			for (var i = 0; i < divs.length; i++) {
				if (divs[i].className == 'breakout') {
					divs[i].style.display = 'none';
					divs[i].style.visibility = 'hidden';
				}
			}
		}
	} // End start()
	,

	//Replace an html element with stylized image
	toggleTables: function(id, obj) {
		if (document.getElementById(id)) {
			var tbl = document.getElementById(id);
			var img = document.createElement('img');
			
			//Set image attributes
			img.src = obj.imgSrc;
			img.width = obj.imgWidth;
			img.height = obj.imgHeight;
			img.className = obj.imgClass;
			img.alt = obj.imgAlt;
			
			//Append the new image
			var parent = tbl.parentNode;
			parent.insertBefore(img, tbl);

			//Remove the html
			parent.removeChild(tbl);
		}
	} // End toggleTables()
	,

	changeFontSize: function(newSize) {
	
			var elemBody = $E('body');    
			
        	if(_intCurrentFontSize != newSize) {
				elemBody.removeClass('textsize1');
				elemBody.removeClass('textsize2');
				elemBody.removeClass('textsize3');
	        	elemBody.addClass('textsize' + newSize);
	        }
			
			// Set current size
			_intCurrentFontSize = newSize;
			Storage.createCookie('textsize',_intCurrentFontSize,'365');
		
			// Send a request to the server to store the TextSize in the cookie
	        var url = FS_AJAX_POST_URL + _intCurrentFontSize;
	        new Ajax(url, { method: 'get' }).request();
	        
	} // End changeFontSize()
	,
	
	// Add the behaviors to the font-size control
	createFontSizer: function() {  
	    
	    $(FS_FONT_SIZE_BUTTON_1_ID).addEvent('click', function(event) { Site.changeFontSize(1); });
	    $(FS_FONT_SIZE_BUTTON_2_ID).addEvent('click', function(event) { Site.changeFontSize(2); });
	    $(FS_FONT_SIZE_BUTTON_3_ID).addEvent('click', function(event) { Site.changeFontSize(3); });
	    var thisSize = Storage.readCookie('textsize');
	    
	    Site.changeFontSize(thisSize);
	    
	} // End createFontSizer()
	
};

//////////////////////////////////////////////////////////
// Common
// Handles common operations
//////////////////////////////////////////////////////////
var Common = {

	getDOMProps: function() {
		if (document.getElementById) {isID = 1; isDHTML = 1;}
		else {
			if (document.all) {isAll = 1; isDHTML = 1;}
			else {
				browserVersion = parseInt(navigator.appVersion);
				if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {isLayers = 1; isDHTML = 1;}
			}
		}
	} // End getDOMProps()
	,

	findDOM: function(objectID,withStyle) {
		if (withStyle == 1) {
			if (isID) { return (document.getElementById(objectID).style); }
			else { 
				if (isAll) { return (document.all[objectID].style); }
				else {
					if (isLayers) { return (document.layers[objectID]); }
				};
			}
		}
		else {
			if (isID) { return (document.getElementById(objectID)) ; }
			else { 
				if (isAll) { return (document.all[objectID]); }
				else {
					if (isLayers) { return (document.layers[objectID]); }
				};
			}
		}
	} // End findDOM()
	,
	
	bookmarkPage: function() {
		var sTitle = "AstraZeneca - " + window.document.title;
		var sUrl = window.document.location.href;
		if (window.sidebar)			// Mozilla Firefox Bookmark
			window.sidebar.addPanel(sTitle, sUrl, "");
		else if (window.external)	// IE Favorite
			window.external.AddFavorite(sUrl, sTitle);
		else if (navigator.appVersion.indexOf('Mac') != -1)
			alert('Press Apple+D to bookmark this site.');
		else
			alert('Press Control+D to bookmark this site.');
			
	} // End bookmarkPage()
	,
	
	printPage: function() {
		if (window.print)
			window.print();
		else
			alert('Select PRINT from the File menu.');
			
	} // End printPage()
	,
	
	urlDecode: function(str) {
		str=str.replace(new RegExp('\\+','g'),' ');
		return unescape(str);
		
	} // End urlDecode()
	, 

	urlEncode: function(str) {
		str=escape(str);
		str=str.replace(new RegExp('\\+','g'),'%2B');
		str=str.replace(new RegExp('-','g'),'%96');
		return str.replace(new RegExp('%20','g'),'+');
		
	} // End urlEncode()
	,

	refreshMe: function() {
		var url = document.location.href;
		Common.goTo(url);
	} // End refreshMe()
	,

	//Navigates to a URL
	goTo: function(strURL) {
		window.location.href = strURL;
	} // End goTo()
	,
	
	scrollToElement: function(elem){
		var selectedPosX = 0;
		var selectedPosY = 0;
		while(elem != null) {
			selectedPosX += elem.offsetLeft;
			selectedPosY += elem.offsetTop;
			elem = elem.offsetParent;
		}
		window.scrollTo(selectedPosX,selectedPosY);
	} // End scrollToElement()

};

//////////////////////////////////////////////////////////
// Storage
// The storage object handles cookie operations
//////////////////////////////////////////////////////////
var Storage = {

	readCookie: 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;
	} // End readCookie()
	,
	
	createCookie: function(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = '; expires=' + date.toGMTString();
		}
		else {
			var expires = '';
		}
		var ck = name + '=' + value + expires + '; path=/';
		//	if (days != -1) alert('Cookie\n' + ck + '\ncreated');
		document.cookie = ck;
	} // End createCookie()
	,
	
	expireCookie: function(name) {
		if (name) {
			Storage.createCookie(name,'',-1);
		}
	} // End expireCookie()
	,
	
	cookieQsParam: function(key) {
		key = key.toLowerCase();
		var qs = window.location.search.substring(1);
		var pairs = qs.split('&');
		for (var i = 0; i < pairs.length; i++) {
			var name = pairs[i].split('=')[0];
			var val = pairs[i].split('=')[1];
			if (name.toLowerCase() == key) {
				if (Common.isNumeric(val)) {
					Storage.createCookie(key, val, null);
				}
			}
		}
	} //End cookieQsParam()
	
};


//////////////////////////////////////////////////////////
// Linking
// The tracking object handles sitewide link wiring
//////////////////////////////////////////////////////////
var Linking = {

	isExternalNonAzLink: function(hostname, obj) {
		var href = obj.href.toLowerCase();
		var bExternalNonAz = ((href.indexOf("http://")!= -1 || href.indexOf("https://")!= -1) && href.indexOf(hostname) == -1 && href.indexOf('astrazeneca') == -1) ? true : false;	
		return bExternalNonAz;
	}
	,

	wireLinks: function() {
		var hostname = window.location.hostname.replace('www.','').toLowerCase();
		var header = document.getElementById('ancHeader');
		var anchors = document.getElementsByTagName('a');
		for (var i = 0; i < anchors.length; i++) {
			if (Linking.isExternalNonAzLink(hostname,anchors[i])) {
				anchors[i].href = Linking.appendSource(anchors[i].href);
			}
		}
	} // End wireLinks()
	,
	
	appendSource: function(uri) {
		var joinChar = (uri.indexOf("?")>=0) ? "&" : "?";
		var sourceVal = Storage.readCookie("source");
		if (sourceVal > 0)
			return uri + joinChar + "source=" + sourceVal;
		return uri;
	} // End appendSource()
	
};


//////////////////////////////////////////////////////////
// Tracking
// The tracking object handles client analytics
//////////////////////////////////////////////////////////
var Tracking = {

	SEOSourceTagging: function() {
	    var engineList = [  "google",
                            "yahoo",
                            "msn",
                            "aol",
                            "aol",
                            "lycos",
                            "ask",
                            "altavista",
                            "netscape",
                            "cnn",
                            "looksmart",
                            "about",
                            "mamma",
                            "alltheweb",
                            "gigablast",
                            "voila",
                            "virgilio",
                            "live",
                            "baidu",
                            "alice",
                            "yandex",
                            "najdi",
                            "aol",
                            "club-internet",
                            "mama",
                            "seznam",
                            "search",
                            "szukaj",
                            "szukaj",
                            "netsprint",
                            "google.interia",
                            "szukacz",
                            "yam",
                            "pchome"];
        // AZCommon Code defines the source code in the HTTP Response Header
        if (Storage.readCookie("source") == 0) {
            for (var i = 0; i < engineList.length; i++) {
            
            }
        }
	} // End SEOSourceTagging()
	,
	
	atdmt_switch: function(atdmt_switch_url, redirect_url) {
	
		//var atdmt_switch_url = "http://switch.atdmt.com/action/SQL20080902_Questions_For_Doctor_PDF";
		var io = new Image();
		io.src = atdmt_switch_url;
		window.location = redirect_url;
		return false;
    
	} // End atdmt_switch()
	
};


//////////////////////////////////////////////////////////
// Init Events
//////////////////////////////////////////////////////////
Common.getDOMProps();


//////////////////////////////////////////////////////////
// Dom Ready Events
//////////////////////////////////////////////////////////
window.addEvent('domready', function(){

	//Wire site links
   Linking.wireLinks();
   
});


//////////////////////////////////////////////////////////
// Load Events
//////////////////////////////////////////////////////////
window.addEvent('load', function() {

	//Call the Site initialization function when the page loads
	Site.start();
	
});


