/*
this js is made to fix the execution of p7 tabs when used with BC webapps
the following conditions need to be met:
1) must be inserted into the template where the webapp is set to run
2) dependent on cookies.js(handles cookies)
3) dependent on jquery.js(handles events)
*/


$(document).ready(function(){

var currentSubMenu = -1;

	if (window.location.toString().indexOf("_webapp_") != -1 || window.location.toString().indexOf("CustomContentRetrieve.aspx") != -1)
	{


	// stores the current url and retrieves currentSubMenu value from the cookie stored
		webappURL = window.location.toString();
		currentSubMenu = parseInt(readCookie("currentSubMenu"));
        //alert(currentSubMenu)
        
        if (parseInt(currentSubMenu) == -1)
        {
        	alert("this error has somthing to do with click events and ajax in firefox.");
            return false;
        }

		var org_webappURL  = document.getElementById("nav_301159").getElementsByTagName("a")[currentSubMenu].href.toString();

	//swaps current URL(href) of the submenu with webapp URL so P7 can initiallize it
		document.getElementById("nav_301159").getElementsByTagName("a")[currentSubMenu].href = webappURL;
	
	// EVENT:CLICK handles opening of original submenu link
		$("#nav_301159 a").click(function(event) {
			if (this.href == webappURL) {
				event. preventDefault();
				window.location = org_webappURL;
			}
		});
   	// reinitiallizes P7
		P7_initTBM(0,2,1,1,200,1);
	}

//removes cookie if not in the details view. this is to ensure smooth operation of script
	else
	{
		if (checkCookie("currentSubMenu") != false)
			delCookie("currentSubMenu");
     	else
     		currentSubmenu = parseInt(readCookie("currentSubMenu"));
	}



//EVENT:CLICK this handles the creation of cookies and passing of current submenu index
    
	$(".webapptable tbody tr td img, .webapptable tbody tr td div div div img").click(function(event){

	//alert("entered function"); 
		var cat=""; // cat will store the category that the webapp item is assigned to
            
	//makes sure that the cookie used to store the submenu is removed before any creation is made
        if (checkCookie("currentSubMenu") != false)
			delCookie("currentSubMenu");



//placed for testing purposes. should not be needed if the event will fire off as expected

        if (this.tagName == "IMG")
			cat = this.parentNode.nextSibling.innerHTML.toString();
		else
			cat = this.nextSibling.innerHTML.toString();


// this part is parses the category of the webapp item to be stored in [cat] variable
		cat = cat.substring(0,cat.indexOf(" ")).toLowerCase();
        
// create cookie based on the which submenu is supposed to be active
        if (parseInt(get_thumb_subMenu(cat)) > -1 && parseInt(get_thumb_subMenu(cat)) < document.getElementById("nav_301159").getElementsByTagName("a").length)
        {
        	createCookie("currentSubMenu",get_thumb_subMenu(cat).toString(),7);
        }
        
        else if (parseInt(get_thumb_subMenu(cat)) == document.getElementById("nav_301159").getElementsByTagName("a").length)
        {
        	createCookie("currentSubMenu",currentSubMenu.toString(),7);
        }
        
	});
	
});

// this function finds which submenu is active. it will return 1 of the 3 possible values
// a) -1 if it cannot locate the submenu that is supposed to be active
// b) a value equal the number of submenus if it was able to located the active submenu but the active submenu was modified for P7
// c) a value < the sumber of submenus but > than -1 if it sucessfully found the active submenu
function get_thumb_subMenu(cat)
{
	
	for (var x=0; x < document.getElementById("nav_301159").getElementsByTagName("a").length; x++)
    {
     	if ( document.getElementById("nav_301159").getElementsByTagName("a")[x].href.indexOf(cat) > -1)
        {
           	return x;
        }	
    }
    //alert(x+","+document.getElementById("nav_301159").getElementsByTagName("a").length)
    if ( x == document.getElementById("nav_301159").getElementsByTagName("a").length)
    	return x;

    return -1;
}
