/* combine : /javascript/common/changeTitle.js*/
$(document).ready(function() {
	if ((typeof(jDisplayChangeTitleOverlay) != "undefined")
			&& (jDisplayChangeTitleOverlay == true)) {
		$.get('/content/changetitle/changeTitle.html', function(data) {
			displayChangeTitleOverlay(data);
		});
	}
});

function displayChangeTitleOverlay(data) {
	openModalJQ(data, null, null, null);
	populateOverlayData();
	}

function populateOverlayData() {
	$('#modal_control, #modal_header').remove();
	$('#modal_container').css({'backgroundColor' : 'transparent', 'border' : '0px', 'height' : '382px', 'overflow' : 'hidden'});
	if (jDisplayChangeTitleOverlaySignedInMessage == true) {
		$('#modal_container').find('#titleOverlayTradingTitle').html(jAccountTitleName);
	} else {
		$('#modal_container').find('#signedInCopy').hide();
		$('#modal_container').find('#modalParam').attr('height', 325);
		$('#changetitleContainer').css({'height' : 255});
	}
	var buttonLabel = 'Go back to ' + jBackRegName;
	var goBackLink = $('#modal_container').find('#titleOverlayGoBackLink');
	goBackLink.html(buttonLabel);
	goBackLink.attr('title', buttonLabel);
	goBackLink.attr('alt', buttonLabel);
	goBackLink.attr('href', 'javascript:returnToPreviousSite();');
	var goShoppingLink = $('#modal_container').find('#titleOverlayGoShoppingLink');
	goShoppingLink.attr('href', 'javascript:goShopping();');
	var welcomeImage = $('#modal_container').find('#titleWelcome');
	welcomeImage.css('backgroundImage', 'url(' +  IMG_SERVER + '/content/images/changetitle/welcometo.png' + ')');
	welcomeImage.attr('title', 'Welcome');
	welcomeImage.attr('alt', 'Welcome');
	var titleImage = $('#modal_container').find('#titleImage');
	titleImage.css('backgroundImage', 'url(' +  $('#brandLogo-2 img').attr('src') + ')');
	titleImage.attr('title', $('#brandLogo-2 a').attr('title'));
	titleImage.attr('alt', $('#brandLogo-2 a').attr('alt'));
}

function returnToPreviousSite() {
	closeModalJQ();
	imageClick(newUrl, jBackUrl);
}

function goShopping() {
	closeModalJQ();
	MANUAL_LINK_CLICK('Go Shopping', jTitle);
}

function imageClick(toURL, destinationServer, destinationPage, coremetricsTag, elementSlot, pageFrom) {
	var splitUrl = toURL.split('newtitle=');
	var titleId = splitUrl[1];
	imageClickChangeTitle(titleId,elementSlot);
}

function changeTitleFreeFormat(titleId, ffpid, cm) {
	$.ajax({
		url: "/shop/changetitle/ajax/ChangeTitleLaunch.action?newtitle=" + titleId,
		type: "GET",
		cache: false,
		dataType: "json",
		success: function(data, status) {
			if(data.successful) {
				setTimeout(function() {performChangeTitleRedirect(data.urlto, data.lastSessionId, "coremetrics="+cm, null, 
						ffpid, null);}, 300);
				
			} else if (data.bagShareNotAllowed) {
				bagShareNotAllowed(data);
			}
		}
	});
}

function ChangeTitleHierarchy(titleId,cm,hierarchyPath) {
	$.ajax({
		url: "/shop/changetitle/ajax/ChangeTitleLaunch.action?newtitle=" + titleId+ "&elementSlot=XSS",
		type: "GET",
		cache: false,
		dataType: "json",
		success: function(data, status) {
			if(data.successful) {
				setTimeout(function() {performChangeTitleRedirect(data.urlto, data.lastSessionId, null, 
						hierarchyPath,null);}, 300);
				
			} else if (data.bagShareNotAllowed) {
				bagShareNotAllowed(data);
			}
		}
	});
}

function ChangeTitleProductDetails(titleId,cm,productDetailsPath) {
	$.ajax({
		url: "/shop/changetitle/ajax/ChangeTitleLaunch.action?newtitle=" + titleId,
		type: "GET",
		cache: false,
		dataType: "json",
		success: function(data, status) {
			if(data.successful) {
				setTimeout(function() {performChangeTitleRedirect(data.urlto, data.lastSessionId, "coremetrics="+cm, 
						null,null,null,productDetailsPath);}, 300);
				
			} else if (data.bagShareNotAllowed) {
				bagShareNotAllowed(data);
			}
		}
	});
}

function changeTitleSuperSites(titleId) {
	$.ajax({
		url: "/shop/changetitle/ajax/ChangeTitleLaunch.action?newtitle=" + titleId + "&elementSlot=SUPERSITE",
		type: "GET",
		cache: false,
		dataType: "json",
		success: function(data, status) {
			if(data.successful) {
				setTimeout(function() {performChangeTitleRedirect(data.urlto, data.lastSessionId, null, 
						null, null, true, null);}, 300);
				
			} else if (data.bagShareNotAllowed) {
				bagShareNotAllowed(data);
			}
		}
	});
}

function imageClickChangeTitle(titleId,elementSlot) {
	$.ajax({
		url: "/shop/changetitle/ajax/ChangeTitleLaunch.action?newtitle=" + titleId + "&elementSlot=S"+elementSlot,
		type: "GET",
		cache: false,
		dataType: "json",
		success: function(data, status) {
			if(data.successful) {
				setTimeout(function() {performChangeTitleRedirect(data.urlto, data.lastSessionId, data.coremetricsTag);}, 300);
			} else if (data.bagShareNotAllowed) {
				bagShareNotAllowed(data);
			}
		}
	});
}

function bagShareNotAllowed(data) {
	$('#proceed_to_link').html('Proceed to ' + data.newTitleName);

	ctNewtitle = data.newtitle;
	ctUrlto = data.urlto;
	ctForm = theForm;

	$('#TITLE_CHANGE_BAG-popup').css({'visibility' : 'visible',
		'display' : 'inline',
		'top' : '400',
		'left' : '100'});
}

function performChangeTitleRedirect(destinationServer, lastSessionId, coremetricsTag, hiararchy,
		ffpid, superSites,productDetailsPath) {
	
	var redirectUrl = destinationServer;
	
	// Add the session id if it is defined
	if (lastSessionId) {
		
		if (redirectUrl.indexOf("?") > -1) {
			redirectUrl = destinationServer + "&lastSessionId=" + lastSessionId;
		} else {
			redirectUrl = destinationServer + "?lastSessionId=" + lastSessionId;
		}
		
		// Add optional elements if they are defined
		if (coremetricsTag) {
			redirectUrl = redirectUrl + "&" + coremetricsTag;
		}
		
		// take to the product details page
		if (productDetailsPath) {
			redirectUrl = redirectUrl + "&productDetailsPath=" + productDetailsPath;
		}

		// Add hierarchy path if the change title will end up in a navigarion page rather than home
		if (hiararchy) {
			redirectUrl = redirectUrl + "&hierarchy=" + hiararchy;
		}
		
		// Add the free format page id 
		if (ffpid) {
			redirectUrl = redirectUrl + "&ffpid=" + ffpid;
		}
		
		// Add supersites flag to allow the action to return SuperSites String
		if (superSites) {
			redirectUrl = redirectUrl + "&superSites=" + superSites;
		}
		
		// Perform redirect
		window.location = redirectUrl;
	}
}

/* complete : /javascript/common/changeTitle.js*/


