 // AJAX implementation

// GLOBAL VARIABLES

var rec_to_show = 8;			// holds the number of records to show per page
var active_page = 1;			// the first page to show ALWAYS 1
var container = 'container';	// the id of the thumbs container
var _loading = false; 
var sn= new Array();
var global_code = 'MS';                //Code to determine the Selected social networking
var wmode_change = false;         //code to change wmode for flash object
//-----------------------------


/*	This function initiates call to AJAX */
function call() {
	//if ajax is not supported then follow the normal link
	if (! IS_AJAX_ENABLED) {		
	    return true;
	}
	// if arguments has value then it is the active page number
	// otherwise this is the first time call	
	active_page = (arguments[0] != null && typeof(arguments[0]) == 'number') ? parseInt(arguments[0]) : 1 ;

	// get the active page path to call
	var page_name = getPageName();

	if (!page_name) {
	    return true;
	}

	//show loading message
	show_loading();

	// initiate call to AJAX
	var ajax_obj = new AJAX();
	ajax_obj.init('true','GET','XML',page_name,'',createContentBlock);
	ajax_obj = null;

	// return false
	return false;
}

/* returns page name */
function getPageName() {
	if (typeof(total_rec) != 'undefined') {			
		if (total_rec > 0) {			
			var num_pages = Math.ceil(total_rec/rec_to_show); // calculate the number of pages
			if (active_page <= num_pages) {
				return 'page' + active_page + '.xml'; // returns page name to read
			}else {				
				return;
			}
		}else {			
			return;
		}
	}else {		
	     return;
	}
}


/**
* This function creates the content blocks of the pages
*/
function createContentBlock(xml) {
	// get the responseXML
	// get the image collection for xml response
	// this is case-sensitive make sure the xml contains small case
	if (xml == null) {
	    return;
	}else {
		var imgcol = xml.getElementsByTagName('ecard');	
		
		// get page title and change it
		try {
			var pageTitle = xml.getElementsByTagName('pagetitle')[0].firstChild.nodeValue;
			document.title = pageTitle;
		}catch(e) {}
		
		// now get the image collection to get the attribute
		var output = buildTable(imgcol);
		var nav_bar = build_pagination_bar(active_page);	

		// change the innerHTML of the container	
		document.getElementById('container').innerHTML = '';	
		document.getElementById(container).innerHTML = output;		
		document.getElementById('pagin_head').innerHTML = nav_bar;
		if (nav_bar.length == 0) {
			// no records so set the bg of the cell to transparent
		    document.getElementById('pagin_head').style.backgroundColor = 'transparent';
			try {
				document.getElementById('pagin_foot').style.backgroundColor = 'transparent';   
			}
			catch (e){}				
		}
		try{
			document.getElementById('pagin_foot').innerHTML = nav_bar;
		}catch(e){}		
	}
	//hide the loading message
	hide_loading();
	// refresh adverts
	refreshAds();
}


/**
* Builds the table structure of the thumb container
*
*/
function buildTable(imgcol) {	
	var tableHTML = new Array();
	tableHTML[tableHTML.length] = '<table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td colspan=4>&nbsp;</td></tr><tr>';	
	var row_ctr = 0; // row counter creates new row/<tr> if exceeds 2
	var temp;
	var thumb_link = thumb_src = redline = type = greenline = newimage = bigimage = floader = flashDetails = image_type = '';
	var strWrite = ''; // holds either card type or number of records
	var thumb_image_path = 'http://img.123greetings.com/thumbs/'; // thumb image source path
	sn.length=0;
	for (var i=0; i < imgcol.length; i++ ) {		
		if (row_ctr == 2) {
		    tableHTML[tableHTML.length] = '</tr><tr><td colspan=4>&nbsp;</td></tr><tr>';
			row_ctr = 0;
		}
		// get the image elements array
		node = imgcol[i];
		try
		{
			floader = unescape(node.getElementsByTagName("flloader")[0].firstChild.nodeValue);
			floader = floader.split("%3F")[0];
		}
		catch (ex)
		{
			floader = '';
		}
		
		try
		{
			flashDetails = node.getElementsByTagName("flash_details")[0].firstChild.nodeValue;
		}
		catch (ex)
		{
			flashDetails='';
		}

		
		thumb_link = node.getElementsByTagName("thumblink")[0].firstChild.nodeValue;
		thumb_src = thumb_image_path + node.getElementsByTagName("image")[0].firstChild.nodeValue;
		redline = node.getElementsByTagName("redline")[0].firstChild.nodeValue;
		greenline = node.getElementsByTagName("greenline")[0].firstChild.nodeValue;
		// big image path
		bigimage = node.getElementsByTagName("image")[0].firstChild.nodeValue; 
		//get image type
		//image_type = node.getElementsByTagName("image_type")[0].firstChild.nodeValue;
		
		temp = flashDetails.split('#');

		try {
			newimage = node.getElementsByTagName("newimagepath")[0].firstChild.nodeValue;
		}catch(e) {
			newimage = '';
		}


		// if label is blank then look for no_cards tag
		try {
		    type = node.getElementsByTagName("label")[0].firstChild.nodeValue;
		}
		catch (e){
		    type = '';
		}

		if (type == '') {
			no_of_cards = node.getElementsByTagName("no_cards")[0].firstChild.nodeValue;
			strWrite = (no_of_cards > 0) ? "[ " + no_of_cards + " cards ]" : "";
		}else {
			type = getType(type);
			strWrite = "[ " + type + " ]";
		}

		
		
		// fill up images array for preloading
		// call preloader
		Preload(new Array(thumb_src));
		
		tableHTML[tableHTML.length] = '<td align="center" valign="middle"><table width=140 height=140 border=0 cellspacing=1 cellpadding=1 > <tr><td valign="middle" background="http://img.123greetings.com/images/imgback_thumb.gif" align="center" style="background-repeat: no-repeat;border-color: transparent"><a href=' + thumb_link + '><img border="0" src=' + thumb_src + ' alt="' + (redline + ' ' + greenline) + '" height="115" width="115" title="' + (redline + ' ' + greenline) + '" align="absmiddle"></a></td></tr></table></td>';
		
		tableHTML[tableHTML.length] = '<td valign="middle" width="21%"><a href="' + thumb_link + '" title="' + (redline + ' ' + greenline) + '"><FONT face="MS SANS SERIF" color=#cc0000 size=2><B>' + redline + '</b></font></a><BR><a href="' + thumb_link + '" title="' + (redline + ' ' + greenline) + '"><FONT face="MS SANS SERIF" color=#006600 size=1>' + greenline + '</font></a><br><a href="' + thumb_link + '" title="' + (redline + ' ' + greenline) + '"><FONT face="MS SANS SERIF" color=#006600 size="1"><b>' + strWrite + '</font></b></a></font>';
		
		//show my_space logo only if type is not blank
		//alert(type);
	    var image_path = '';
		if (newimage == '') {
			image_path = 'http://img.123greetings.com/eventsnew/' + bigimage;
		}else{
			image_path = newimage + bigimage;
		}

		
		if (type != '') {
			if (type == 'Flash') {					
				image_path = image_path.replace(/eventsnew/,'thumbs');
				sn[i]={'thumb' : image_path,'path' : thumb_link,'type' : "F",'flash_url':floader+"#"+temp[1]+","+temp[2]};				
			}else if (type == 'Interactive') {
				// if interactive card
				/*if (image_type == 'F') {
				    image_path = image_path.replace(/eventsnew/, 'thumbs');
				}
				var loader_path = '';
				if(floader != '') {
					loader_path = floader+"#"+temp[1]+","+temp[2];
				}else {
				    loader_path = flashDetails;
				}
			    sn[i] = {
					'thumb': image_path, 
					'path' : thumb_link, 
					'type' : image_type,
					'flash_url' : loader_path
				};*/
				var is_flash = (flashDetails.indexOf('.swf') > -1) ? true : false;
				
				if (is_flash) {
				    image_path = image_path.replace(/eventsnew/, 'thumbs');
				}

				var loader_path = '';
				if(floader != '') {
					loader_path = floader+"#"+temp[1]+","+temp[2];
				}else {
				    loader_path = flashDetails;
				}
				//image type
				var im_type = is_flash ? "F" : "A";

				sn[i] = {
					'thumb': image_path, 
					'path' : thumb_link, 
					'type' : im_type,
					'flash_url' : loader_path
				};
			}else{				
				sn[i]={'thumb': image_path,'path': thumb_link,'type': "A",'flash_url': ''};				
			}
			var jsFunc = 'dhtml.showBox1(event,'+ (i+1) + ')';
			tableHTML[tableHTML.length] = '<BR>&nbsp;<BR><IMG SRC="'+ window.config[window.code].img_url.split('#')[0] +'"  BORDER="0" ALT=""  SNBUTTON=1 onClick="' + jsFunc + '" style="cursor:pointer"></td>';
		}else {
			tableHTML[tableHTML.length] = '<BR>&nbsp;<BR><IMG SRC="'+ window.config[window.code].img_url.split('#')[0] +'" BORDER="0" ALT="" SNBUTTON=1 onClick="dhtml.showBox1(event,' + (i+1) + ')" style="cursor:pointer"></td>';
		}		
		
		row_ctr++; // increase counter
	
		
	}
	tableHTML[tableHTML.length] = '<tr><td colspan=4>&nbsp;</td></tr></table>';	
	return tableHTML.join('');	
}


/* function to determine the card type like Postcard, Flash, Animated etc. */
function getType(type) {
	var card_type = '';
	switch (type.toUpperCase()) {
		case 'F':
			card_type = 'Flash';
			break;
		case 'I':
			card_type = 'Interactive';
			break;
		case 'A':
			card_type = 'Animated';
			break;
		case 'P':
			card_type = 'Postcard';
			break;
		case 'O':
			card_type = 'Clickcard';
			break;
	}

	return card_type;
}

/* copies the top navigation bar to footer */
function copy_header() {
	if (document.getElementById('pagin_head').innerHTML != '') {	
		document.getElementById('pagin_foot').innerHTML = document.getElementById('pagin_head').innerHTML;
	}else {
		// no records so set the bg of the cell to transparent
	    document.getElementById('pagin_foot').bgColor = 'transparent';
	}
}

/**
* Builds the pagination/navigation bar of the thumbpage
* 
*/
function build_pagination_bar(page) {

	var nav_str = new Array();
	var num_pages = Math.ceil(total_rec/rec_to_show);
	
	if (total_rec > 0) {	// check whether total_rec that holds number of records is > 0
		
		if (total_rec > 8) {		
			
			if (num_pages > 0) {	
				
				nav_str[nav_str.length] = '<TABLE cellpadding=1 cellspacing=0 width="100%" border=0 ><TBODY><TR bgColor=#efefef>';
				
				// determines the placement of Separator or Vertical Bar '|'
				if (page == 1) {
					nav_str[nav_str.length] = '<TD align=left width="30%"><FONT face=Verdana color=#00058a size=1>&nbsp;</FONT></TD>';    
				}else {
					nav_str[nav_str.length] = '<TD align=left width="30%"><FONT face=Verdana color=#00058a size=1>&nbsp;<a href="javascript:void(0)" onmouseover="self.status=\'\';return true;" onClick="prev_records('+(page-1)+');"><B>&lt;Back</B></A></FONT></TD>';
				}
				
				
				nav_str[nav_str.length] = '<TD align="center" width="40%" valign="absmiddle" style="padding-bottom: 2px">';

				
				for (var i = 1; i <= num_pages; i++ ) {
					// determines the placement of Separator or Vertical Bar '|'
					if (i==page) {
						if (i==num_pages) {
							nav_str[nav_str.length] = '<FONT face="verdana" color="gray" size="1"><B>'+i+'</b></font>';    
						}else {
							nav_str[nav_str.length] = '<FONT face="verdana" color="gray" size="1"><B>'+i+'</b></font><FONT face=verdana color="#00058a" size="1"> | </font>';
						}			    
					}else {
						// determines the current page being viewed
						if (i == num_pages) {
							nav_str[nav_str.length] = '<FONT face=Verdana color=#00058a size=1><A href="javascript:void(0)" onmouseover="self.status=\'\';return true;" onClick="call('+(i)+')"><B>'+i+'</B></A></font>';    
						}else {
							nav_str[nav_str.length] = '<FONT face=Verdana color=#00058a size=1><A href="javascript:void(0)" onmouseover="self.status=\'\';return true;" onClick="call('+(i)+')"><B>'+i+'</B></A></font><FONT face=verdana color=#00058a size=1> | </font>';
						}				
					}
				}

				if (page == num_pages) {
					nav_str[nav_str.length] = '</TD><TD align=right width="30%"><FONT face=Verdana color=#00058a size=1>&nbsp;</FONT></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>';    
				}else {
					nav_str[nav_str.length] = '</TD><TD align=right width="30%"><FONT face=Verdana color=#00058a size=1><A href="javascript:void(0)" onmouseover="self.status=\'\';return true;" onClick="next_records('+(page+1)+');"><B>Next&gt;</B></A>&nbsp;</FONT></TD></TR></TBODY></TABLE>';
				}		
			}
		}else {
		     nav_str[nav_str.length] = '';
		}
	}else {
		// total records can't be empty if is then return null
	    return;
	}
	return nav_str.join('');
}

/**
* Navigates to next set of records
*/
function next_records(page) {	
	num_pages = Math.ceil(total_rec/rec_to_show);
	if (page <= num_pages) {		
		active_page = page;		
	    return call(page);
	}
}

/**
* Navigates to previous set of records
*/
function prev_records(page) {	
	if (page >= 1) {
		active_page = page;
		return call(page);
	}
}

/******************************** IMAGE PRELOADING ****************************/
var aImages=new Array();
function Preload() {
	var temp = Preload.arguments;
	var tempLen = temp.length;
	for(x=0; x < tempLen; x++) {
		aImages[x]=new Image();
		aImages[x].src=Preload.arguments[x];
	}
}

/*************************** show loading ********************/
/*
* Function when called displays a dynamically created div with loading message.
*/
var image = new Image();
image.src = 'http://img.123greetings.com/images/loading.gif';
image.align = 'absmiddle';

function show_loading(){	
	//show loading message text
	if (!_loading) {
		var oDiv = document.createElement('div');
		oDiv.setAttribute('id','loading');
		oDiv.id = 'loading';
		oDiv.style.position = 'absolute';
		oDiv.style.zIndex = '50';		
		oDiv.style.top = findPosY(document.getElementById('pagin_head')) - 10; // add top scroll amount		
		oDiv.style.left = (parseInt(getWinSize() / 2)- parseInt(200/2));
		oDiv.style.height = 50;
		oDiv.style.width = 200;
		oDiv.style.paddingTop = 15;
		oDiv.style.backgroundColor = '#FFFFFF';//'#00A8F9';
		oDiv.style.opacity = .95;
		oDiv.style.filter = 'alpha(opacity=' + 95 + ')';
		oDiv.style.textAlign = 'center';
		oDiv.style.fontFamily = 'verdana, Geneva, Helvetica';
		oDiv.style.fontWeight = 'bold';
		oDiv.style.fontSize = '10';
		oDiv.style.color = '#000000';
		oDiv.style.borderStyle = 'ridge';		
		oDiv.style.borderColor = '#808080';
		oDiv.style.borderWidth = 1;
		oDiv.style.visibility = 'visible';
		//create image
		oDiv.appendChild(image);
		oDiv.appendChild(document.createTextNode("   Loading...."));		
		document.body.insertBefore(oDiv, document.body.childNodes[0]);
		_loading = true;
	}
}
	

/*
*
* Function used to hide the loading message
*
*/
function hide_loading() {		
	// code to hide loading message	
	if (_loading) {	
		document.body.removeChild(document.getElementById('loading'));
		_loading = false;
	}	
}

function getWinSize() {
	var winW;
	// get window size adjusted for scrollbars
	if (window.innerWidth) {
		winW = window.innerWidth-16;
	}
	if (document.body.offsetWidth) {
		winW = document.body.offsetWidth-20;
	}
	return winW;
}

/* Find a Top position of an element in the page */
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

/* This function refreshes ads */
function refreshAds() {
	var adPos = OAS_listpos.split(',');
	var iframeSrc = 'http://img.123greetings.com/banner_js/showad.html';
	var strAdvertBanner = '<IFRAME name="bannerFrame" id="bannerFrame" marginheight="0" marginwidth="0" align="middle" height="HE" width="WI" frameborder="0" allowtransparency="true" src="SRC" scrolling=no></iframe>';
	//change top banner 728 x 90
	if (document.getElementById('top_banner')) {
		var strBanner = strAdvertBanner.replace(/HE/,'90');
		strBanner = strBanner.replace(/WI/,'748');
		strBanner = strBanner.replace(/SRC/, iframeSrc + '#' + adPos[0] + '|' + OAS_listpos + '|' + OAS_sitepage + '|' + OAS_query);
		document.getElementById('top_banner').innerHTML = strBanner;
	}
	// change right banner 128 x 600
	if (document.getElementById('right_banner')) {
		var strBanner = strAdvertBanner.replace(/HE/,'620');
		strBanner = strBanner.replace(/WI/,'128');
		strBanner = strBanner.replace(/SRC/, iframeSrc + '#' + adPos[1] + '|' + OAS_listpos + '|' + OAS_sitepage + '|' + OAS_query);
		document.getElementById('right_banner').innerHTML = strBanner;
	}
	//change middle banner 300 x 250
	if (document.getElementById('middle_banner')) {
		var strBanner = strAdvertBanner.replace(/HE/,'258');
		strBanner = strBanner.replace(/WI/,'320');
		strBanner = strBanner.replace(/SRC/, iframeSrc + '#' + adPos[2] + '|' + OAS_listpos + '|' + OAS_sitepage + '|' + OAS_query);
		document.getElementById('middle_banner').innerHTML = strBanner;
	}
}


/*********Function to load Javascript Libararies asynchronously **********/
var scr
function load_js(script_src,callback){
var t;
var script_loaded = false;
var temp = document.getElementsByTagName('script');
for(i=0;i<temp.length;i++)
	{
		if(temp[i].src == script_src){
			script_loaded = true;
		}
	}
	if (!script_loaded)
	{
		scr = document.createElement('script');
		var temp_id = script_src.split("/");
		scr.id = temp_id[temp_id.length -1].split(".")[0];
		scr.type = "text/javascript";
		scr.src = script_src;
		scr.onload = callback;//firefox only
		scr.onreadystatechange = function() {if(scr.readyState=='complete' || scr.readyState=='loaded')callback(); } //IE only
		document.getElementsByTagName('head')[0].appendChild(scr);
		return false;
	}else{
		callback();
	}
}


