/**
	thirdparty-css-manager.js - o2online.ie
	
	Checks the vendorStyles array pairs for css and javascript files 
	to tweak layout.
	
 */
var	myurl = document.location.href;
	myurl = myurl.toLowerCase();
var csspath = "//www.o2online.ie/catalogue/partner/thirdparties/thirdparties/";
var imgpath = "//www.o2online.ie/catalogue/partner/thirdparties/images/";

vendorStyles = new Array(
//url test string, css/js file name,

"difference","thirdpartycss/3pv-bethedifference.css",
"difference","js/3pv-black-top-bottom.js",
"amazonaws","thirdpartycss/3pv-bethedifference.css",
"amazonaws","js/3pv-black-top-bottom.js",
"refer","thirdpartycss/3pv-bringafriend.css",
"sims","thirdpartycss/3pv-freesims.css",
"station","thirdpartycss/3pv-ideasstation.css",
"webtext","thirdpartycss/3pv-webtext.css",
"webtext","js/3pv-webtext.js",
"top","thirdpartycss/3pv-topup.css",
"treats/aim","thirdpartycss/3pv-treats-aim.css",
"mywidgets","thirdpartycss/3pv-widgets.css",
"feedhenry","thirdpartycss/3pv-widgets.css",
"myaccount","thirdpartycss/3pv-myaccount.css",
"careers","thirdpartycss/3pv-careers.css",
"blueroom","thirdpartycss/3pv-blueroom.css",
"blueroom","js/3pv-black-top-bottom.js",
"lithium","thirdpartycss/3pv-iip-lithium.css",
"help","thirdpartycss/3pv-iip-ngenera.css",
"kbp-o2dev","thirdpartycss/3pv-iip-ngenera.css"
);

/* How many divs required to close after main content div to fix nesting */

urlsArr = new Array(
	"community.o2online.ie",6,
	"difference",6,
	"amazonaws",6,
	"lithium",6,
	"specialtreats",1,
	"station",1
);

/** TH
 *	What lights up on the main menu?
 * 	"url test string",[numeric binary flag to show subnav (shop only at present)],[class name stub on top-level menu]. 
 *	e.g "bethedifference.o2online.ie",0,"rugby"
 */

navArr = new Array(
	"treats","treats",
	"difference","rugby",
	"help","help",
	"blueroom","theo2"
);

/**
 *	Switch out special image caps to make our body top look just right
 *	---
 *	if we want to test for multiple patterns in the same string, such as
 *	in help/search as opposed to help/kb, put a "|" between the test
 *	patterns, this will test that all patterns are there
 */

var imagecaparr = new Array(
	"help.o2online.ie|&search=1","search_top.png",
	"help.o2online.ie","help and support_top.png",
	"kbp-o2dev.kb.net","help and support_top.png",
	"kbp-o2dev.kb.net|&search=1","help and support_top.png",	
	"refer","bring-a-friend-top-ssb.png"
);


var linkTagStub = '<link rel="stylesheet" href="';
var linkTagEnd = '" type="text/css" />';
var scriptTagStub = '<script src="';
var scriptTagEnd = '" type="text/javascript"></script>';

function writeTag() {
	url = unescape(myurl);

	var requestsMade = 0;
	for (x=0;x<vendorStyles.length;x+=2){

		if (url.indexOf(vendorStyles[x])!=-1){

			if (vendorStyles[x+1].indexOf(".js")!=-1) {
				$("head").append(scriptTagStub+csspath+vendorStyles[x+1]+scriptTagEnd);
				requestsMade++;
			}
			if (vendorStyles[x+1].indexOf(".css")!=-1) {
				$("head").append(linkTagStub+csspath+vendorStyles[x+1]+linkTagEnd);
				requestsMade++;
			}
			if (requestsMade>=2) x = vendorStyles.length;	// Keep HTTP requests down - only one css and js per document
		}
	}
}

/*	closeDivs	*/

function closeDivs() {
	divStr = "";
	for (f=0;f<urlsArr.length;f=f+2) {	
		if(myurl.indexOf(urlsArr[f])!=-1 ) {
			for(g=1;g<=urlsArr[f+1];g++) {
				divStr += "</div>";
			}
			document.write(divStr);
		}
	}
}

function swapimg() {
	var urlmatch = false;
	imgtoswapin = "";
	for(o=0;o<imagecaparr.length;o++)
	{
		if (imagecaparr[o].indexOf("|")!=-1)
		{
			imagecaparr[o] = imagecaparr[o].split("|");
			for(d=0;d<imagecaparr[o].length;d++) {
				if (myurl.indexOf(imagecaparr[o][d])!=-1) {
					urlmatch=true;
				}
				else
				{
					urlmatch=false;
				}
				
			}
			if (urlmatch)
			{
				imgtoswapin = imagecaparr[o+1];
				o = imagecaparr[o].length;
			}
		}
		
		else
		{
			for(d=0;d<imagecaparr.length;d++)
			{
				if (myurl.indexOf(imagecaparr[d])!=-1)
				{
					imgtoswapin = imagecaparr[d+1];
					urlmatch=true;
				}
			}	
		}
		if(urlmatch) o = imagecaparr.length;
	}
	if(imgtoswapin!="")
	{
		$(".page_top_img img").attr("src",imgpath+imgtoswapin);
	}
}

function configSubnav() {
	myurl = myurl.toLowerCase();
	for (f=0;f<navArr.length;f=f+2) {	
		if(myurl.indexOf(navArr[f])!=-1) {
			if(navArr[f+1]!=""){
				$("."+navArr[f+1]+"_over").removeClass(this).addClass(navArr[f+1]+"_selected");
			}
		}
	}
}

function choosesubnav() {
	var thesubnav = "";
	myurl = myurl.toLowerCase();
	
	if (myurl.indexOf("blueroom")!=-1)
	{
		thesubnav = '<ul><li><a href="/">Blueroom Home</a></li><li><a href="/The-O2.aspx">The O2</a></li><li><a href="/Gig-and-Ticket-Guide.aspx">Gig and Ticket Guide</a></li><li><a href="/Forums/Default.aspx">Forums</a></li><li><a href="/Competition.aspx">Competition</a></li></ul>';
	}

	$("#sub-nav").html(thesubnav);
}


$(document).ready(function() {
	writeTag();
	configSubnav();
	swapimg();
	choosesubnav();
});
