/*
 * Courage Classic Team & Supporter page data update script (Button Hijack)
 * Lasantha Gamage
 * lasantha@zurigroup.com
 * 02/03/2010
 * 
 * Jquery 1.2.x or above required
 */

var win;
var popUpSubmitButton;
var reTryAttempt = false;
var externalPostUrl = "http://71.6.174.174/CourageClassic/Post.aspx";
var flashGatewyFile = "/atf/cf/{87e66499-4099-49f9-8648-f7c16ea08835}/GATEWAY.SWF";


$(document).ready( function(){
	
	currentPageUrl = window.location.href;
	if (currentPageUrl.indexOf('PageEditor.aspx', currentPageUrl) <= 0){
		return;
	}
	
	// replace default button action and add cross domain flash gateway to post data
	targetAnchor = $("#why_i_ride a")[1];
	$(targetAnchor).attr("onClick","editWhyIRideText (); return false;");
	$(document.body).append('<div><object width="400" height="100" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" id="crossDomainGatewayObject" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0"> <param value="'+flashGatewyFile+'" name="movie"> <param value="transparent" name="wmode"> <param value="high" name="quality"> <embed width="1" height="1" wmode="transparent" type="application/x-shockwave-flash" id="crossDomainGateway" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash"  quality="high" src="'+flashGatewyFile+'"></object></div>');
	
});

// initialize child window
function editWhyIRideText (){
	
	var strTarget = "../personalpage/TemplateItemEditor.aspx?c=" + c_Index + "&b=" + b_Index + "&ed=" + iID + "&pt=P&ft=TEXT&fd=WHY_I_RIDE";

	//win = window.open("http://localhost/Curragelassic/test/child.html","Testing","menubar=1,resizable=1,width=350,height=250");
	win = window.open(strTarget, 'template_item_editor', 'resizable=yes,scrollbars=yes,width=670,height=450,left=' + (screen.availWidth - 670)/2 + ',top=' + (screen.availHeight - 450)/2);

	// for FF
	win.onload = function (){
		popUpSubmitButton = win.document.getElementById("Item_Editor_Button_Update");
		$(popUpSubmitButton).attr("onClick", "return window.opener.updateText()");
		//$(popUpSubmitButton).click(window.opener.updateText());
	}
	// for IE
	$(win.document).ready(
		function (){
			popUpSubmitButton = win.document.getElementById("Item_Editor_Button_Update");
			$(popUpSubmitButton).attr("onClick", "return window.opener.updateText()");		
		}
	);
}

// Child window Callback
function updateText (){
	// Initialize crossdomain data post
	descriptionField = win.document.getElementById("Item_Editor_Text_Editor");
	updatedDescription = $(descriptionField).val();
	
	// get teamId, team name, supporter id  and supporter name
	tid = sid = 0;	
	sourceSting = $("param[name=flashVars]").val();
	sourceArray = sourceSting.split("&");
	
	for (i = 0; i < sourceArray.length; i++){
		pm = sourceArray[i].split("=");
		
		if (pm[0] == 'tid'){
			tid = pm[1];	
		}
		
		if (pm[0] == 'supid'){
			sid = pm[1];	
		}
	}
	
	NameWithlink = $("#stats_teaminfo h5 span a");
	if (NameWithlink[0]){
		myTeamName = $("#stats_teaminfo h5 span a").html();
	}
	else {
		myTeamName = $("#stats_teaminfo h5 span").html();
	}
	myUrl = $($("#stats_teaminfo span")[1]).html();
	myRiderId = $($("#stats_ridernum h5 span")[2]).html();
	myTitle = $("#rider_heading").html();
	
	cxGate = document.getElementById("crossDomainGatewayObject");
	try {
		cxGate.postbackdata (myTitle,  updatedDescription, myTeamName, tid, myUrl, myRiderId, sid, externalPostUrl );
	}
	catch (e){
		cxGate = document.getElementById("crossDomainGateway");
		cxGate.postbackdata (myTitle,  updatedDescription, myTeamName, tid, myUrl, myRiderId, sid, externalPostUrl );
	}
	
	return false;
}

//Flash ExternalInterface Callback
function postAccepted (){
	//disable child window save button
	$(popUpSubmitButton).val("Updating...").attr("disabled", true);
}

//Flash ExternalInterface Callback
function postSuccess (){
	// post child window 	
	$(popUpSubmitButton).attr("disabled", false).val("Save").removeAttr("onClick");
	win.document.forms[0].submit();
}

//Flash ExternalInterface Callback
function postError(){
	// crossdomain data post failed
	if (!reTryAttempt){
		// retrying for save dat in externl database
		reTryAttempt = true;
		$(popUpSubmitButton).val("Retrying...");
		updateText ();
	}
	else {
		// 2nd attempt filed. submit form without saving data in external database
		$(popUpSubmitButton).val("Save").removeAttr("onClick");
		
		win.document.forms[0].submit();
	}
}
