/**
 * Author: Dhiraj Srivastava (dhiraj.srivastava@in.ibm.com)
 * 
 * this script will connects the links between LBE's
 */

var targetUrl = "";

// e.g. doNav('windowType:brand/id:1');
// #windowType:pLanding/id:3/Skin:true/SkinType:pillar/SkinID:home
// //windowType:closewindow/Skin:true/SkinType:brand/SkinID:1
// windowType:pLanding/id:home
function doNav(url) {
	//alert(" doNav " + url);
	var splitUrl = url.split("/");
	var windowType = splitUrl[0].split(":");
	var id = "";
	if (windowType[1] == "brand") {
		windowType = "brand";
		id = splitUrl[1].split(":")[1];
	} else if (windowType[1] == "pLanding") {
		windowType = "pillar";
		id = splitUrl[1].split(":")[1];
	} else if (windowType[1] == "cLanding") {
		windowType = "category";
		id = splitUrl[1].split(":")[1];
	} else if (windowType[1] == "closewindow") {
		windowType = splitUrl[2].split(":")[1];
		id = splitUrl[3].split(":")[1];
	}
	
	if (id!= 27){
		targetUrl = contextPath + '/showLBE.do?type=' + windowType + '&id='
		+ id + '';
	}else{
		targetUrl = contextPath + '/schoolsLanding.do';
	}
	// //alert("targetUrl in doNav() " + targetUrl);
	location.href = targetUrl;
}

function launchCatalog(object) {
	//alert(" launchCatalog " + object);
	// if isDetail is true it will call itemDetail.do else catalog.do
	if (object.isDetail) {
		if (object.schoolId != "") { // in case of school
			targetUrl = contextPath
					+ '/itemDetail.do?schoolId='
					+ object.schoolId
					+ '&schoolName='
					+ object.schoolName
					+ '&abbrivation='
					+ object.abbrivation
					+ '&skipToDonation=true&itemDetailType=schoolDonation&title='
					+ object.title + '';// +object.skipToDonation+'';
		} else {
			targetUrl = contextPath + '/itemDetail.do?itemId=' + object.itemId
					+ '';
		}
	} else {
		if (object.searchType == "featuredPrograms"
				|| object.searchType == "pillars") {
			targetUrl = contextPath
					+ '/catalog.do?searchType=pillar&filters.pillars='
					+ object.categoryID + '';
		} else if (object.searchType == "categories") {
			if (object.categoryID == 9) {
				// targetUrl =
				// contextPath+'/catalog.do?searchType=navigationCategory&filters.navigationCategories='+object.categoryID+'';
				targetUrl = contextPath + '/catalog.do?searchType=rewards&sorts=PA&itemsPerPage=12&pageNumber=1&filters.navigationCategories=1';
			} else {
				targetUrl = contextPath
						+ '/catalog.do?searchType=navigationCategory&filters.navigationCategories='
						+ object.categoryID + '';
			}
		}
	}
	location.href = targetUrl;
}

// windowType:catalog/searchType:categories/categoryID:27/isSchoolWishList:true/schoolId:2160849544
// e.g. navString=windowType:cLanding/id:27
// e.g
// windowType:catalog/iDetail:true/rewardID:8689/Skin:true/SkinType:pillar/SkinID:home
// // windowType:catalog/isDetail:true/rewardID:8780

// e.g windowType:catalog/searchType:keyword/categoryID:ghwtprizes
// #windowType:catalog/searchType:featuredPrograms/categoryID:3/Skin:true/SkinType:pillar/SkinID:3
// #windowType:catalog/searchType:keyword/categoryID:aiphysical/Skin:true/SkinType:pillar/SkinID:6
// windowType:catalog/searchType:categories/categoryID:4
// windowType:catalog/searchType:categories/categoryID:27/isSchoolWishList:true/schoolId:2160849544
// catalog.do?isSchoolWishList=true&schoolId=
// e.g windowType:pLanding/id:34/Skin:true/SkinType:pillar/SkinID:34
// windowType:pLanding/id:1020
// windowType:pBrands/Skin:true/SkinType:pillar/SkinID:10
// 
// #windowType:cLanding/id:27
// windowType:home
// windowType:pBrands/brandID:7
// windowType:register/wBack:true
// windowType:register
// windowType:cLanding/id:sweestakes/doTracking:Link.WhatsNext.Context
// windowType:brand/id:1/doTracking:Link.WhatsNext.CokeClassic
// windowType:pLanding/id:3/Skin:true/SkinType:pillar/SkinID:home
// windowType:closewindow/Skin:true/SkinType:brand/SkinID:1
// windowType:help/helpID:1/categoryId:1/project:MSR
// windowType:help/helpID:5
// windowType:pLanding/id:home
// windowType:pLanding/id:1020
// windowType:pBrands
// windowType:resetpsw

function doNavigation(url) {
	//alert("doNavigation " + url);

	if (contextPath.charAt(contextPath.length - 1) == "/") {
		contextPath = contextPath.substring(0, contextPath.length - 1);
	}

	var splitUrl = url.split("/");
	// if(splitUrl.length > 5 || (splitUrl.length == 3 &&
	// splitUrl[2].split(":")[0]=="rewardID")){
	//alert("splitUrl.length ::" + splitUrl.length);
	//alert(splitUrl[1].split(":")[0]);
	//alert(splitUrl[0].split(":")[1]);

	if (splitUrl.length > 2 && (splitUrl[2].split(":")[0] == "rewardID" || splitUrl[1].split(":")[0] == "rewardType")) {
		var itemId = splitUrl[2].split(":");
		targetUrl = contextPath + '/itemDetail.do?itemId=' + itemId[1] + '';
	} else if (splitUrl.length > 2 && splitUrl[2].split(":")[0] == "doTracking") { // for
		// doTracking
		// method
		// e.g.
		// windowType:cLanding/id:sweepstakes/doTracking:Link.WhatsNext.Context
		// windowType:brand/id:1/doTracking:Link.WhatsNext.CokeClassic
		//alert("before checking for brand");
		if (splitUrl[0].split(":")[1] == "brand") {
			//alert("Calling doNav()");
			doNav(url);
		} else if (splitUrl[0].split(":")[1] == "cLanding"
				&& splitUrl[1].split(":")[1] == "sweepstakes") {
			targetUrl = contextPath + '/catalog.do?searchType=sweepstakes';
		} else if (splitUrl[0].split(":")[1] == "cLanding"
				&& splitUrl[1].split(":")[1] == "rewards") {
			targetUrl = contextPath + '/catalog.do?searchType=rewards';
		} else if (splitUrl[0].split(":")[1] == "cLanding"
				&& splitUrl[1].split(":")[1] == "instantwins") {
			targetUrl = contextPath + '/catalog.do?searchType=instantwins';
		} else if (splitUrl[0].split(":")[1] == "cLanding"
				&& splitUrl[1].split(":")[1] == "donations") {
			targetUrl = contextPath + '/catalog.do?searchType=goodcauses&filters.keywords=cause-';
		} else if (splitUrl[0].split(":")[1] == "cLanding"
				&& splitUrl[1].split(":")[1] == "schools") {
			//alert("before setting school in doTracking section");
			targetUrl = contextPath + '/catalog.do?searchType=schools';
		} else if (splitUrl[0].split(":")[1] == "pLanding"
				&& splitUrl[1].split(":")[1] == "pointplus") {
			targetUrl = contextPath + '/destinationRewards.do';
		} else {
			// doTracking(id); // sending link
		}
	} else if (splitUrl[0].split(":")[1] == "catalog" // //
			// windowType:catalog/searchType:categories/categoryID:4
			&& (splitUrl[1].split(":")[0] == "searchType" ||splitUrl[1].split("=")[0] == "searchType" )) {	// else if(splitUrl.length > 2 && splitUrl.length <5){
		//alert("Inside catalog section");
		var searchType = splitUrl[1].split(":");
		var categoryID = splitUrl[2].split(":");
		
		//windowType:catalog/searchType=keyword/filters.keywords=fanhancer
		//http://www.mycokerewards.com/catalog.do?searchType=keyword&filters.keywords=fanhancer
		
		
		if (searchType[1] == "featuredPrograms" || searchType[1] == "pillars") {
			targetUrl = contextPath
					+ '/catalog.do?searchType=pillar&filters.pillars='
					+ categoryID[1] + '';
		} else if (searchType[1] == "categories") {
			// For school rewards type we need to look for navigation category
			// id between[27-29]
			// windowType:catalog/searchType:categories/categoryID:27/isSchoolWishList:true/schoolId:2160849544/schoolName:XXX
			// catalog.do?isSchoolWishList=true&schoolId=2160849544
			//alert(" categoryID[1] ::" + categoryID[1]);

			if (categoryID[1] == "27" && url.indexOf("isSchoolWishlist") != -1) {
				//alert('inside school wishlist');
				// 
				targetUrl = contextPath
						+ '/catalog.do?searchType=wishlist&sorts=PD&pageNumber=1&id='
						+ splitUrl[4].split(":")[1];// +'&name='+
				// splitUrl[5].split(":")[1]

			} else if (categoryID[1] == "27") { // windowType:catalog/searchType:categories/categoryID:27
				//alert("before setting schools url");
				targetUrl = contextPath + '/catalog.do?searchType=schools';
			} else if (categoryID[1] == "4") {
				targetUrl = contextPath + '/catalog.do?searchType=rewards&sorts=PA&pageNumber=1&itemsPerPage=12';
			} else {
				targetUrl = contextPath
						+ '/catalog.do?searchType=navigationCategory&filters.navigationCategories='
						+ categoryID[1] + '';
			}
		} else if (categoryID=='filters.keywords=fanhancer'){
			//alert('inside categoryID');
			targetUrl = contextPath + '/catalog.do?searchType=keyword&filters.keywords=fanhancer';
		}
		
		else {
			targetUrl = contextPath + '/catalog.do?searchType=' + searchType[1]
					+ '&filters.keywords=' + categoryID[1] + '';
		}
	} else if (splitUrl[0].split(":")[1] == "pBrands") { // windowType:pBrands/brandID:7
		//alert('inside first pBrands');
		if (splitUrl.length > 1) {
			targetUrl = contextPath + '/participatingBrands.do?brandID='
					+ splitUrl[1].split(":")[1];
		} else {
			targetUrl = contextPath + '/participatingBrands.do';
		}// /index.jsp#windowType:pLanding/id:1020.
	} else if (splitUrl[0].split(":")[1] == "pLanding") { // windowType:pLanding/id:home
		//alert('inside pLanding');
		var id = splitUrl[1].split(":")[1]; // windowType:pLanding/id:pointPlus
		// //windowType:pLanding/id:1020
		if (id == 'home') {
			targetUrl = contextPath + '/home.do';
		} else {
			//alert('calling doNav()');
			doNav(url);
		}
	} else if (splitUrl[0].split(":")[1] == "myAccount") {
		targetUrl = contextPath + '/myAccount.do';
	} else if (splitUrl[0].split(":")[1] == "register") { // windowType:register/wBack:true
		//alert("inside register.do for splitUrl");
		targetUrl = httpsContextPath + '/register.do';
	} else if (url.split(":")[1] == "home") {
		targetUrl = contextPath + '/home.do';
	} else if (url.split(":")[1] == "register") { // windowType:register
		//alert("inside register.do");
		targetUrl = httpsContextPath + '/register.do';
	} else if (url.split(":")[1] == "resetpsw") { // windowType:resetpsw
		targetUrl = contextPath + '/home.do?resetPassword=true';
	} else if (splitUrl[0].split(":")[1] == "help") {
		//alert("inside help");
		if (splitUrl.length == 2) {// windowType:help/helpID:5
			helpID = splitUrl[1].split(":")[1];
			targetUrl = contextPath + '/help.do?helpID=' + helpID
		} else if (splitUrl.length > 2) {// windowType:help/helpID:1/categoryId:1/project:MSR
			helpID = splitUrl[1].split(":")[1];
			categoryID = splitUrl[2].split(":")[1];
			project = splitUrl[3].split(":")[1];
			targetUrl = contextPath + '/help.do?helpID=' + helpID
					+ '&categoryID=' + categoryID + '&project=' + project;
		} else {// windowType:help
			targetUrl = contextPath + '/help.do';
		}
	} else {
		//alert('inside doNav');
		doNav(url);
	}

	//alert(" targetUrl ::" + targetUrl);
	location.href = targetUrl;
}

