/*
    All code written by Mark Hinch - Feb / March 2008
*/

var allTabs = new Array ();         // Holds all tabs' info and queries etc...
var selectedTabs = new Array ();    // Holds the tabNumber of the active tab of each tabset

function createTabSet (whichTabs) {
    selectedTabs[whichTabs] = new Array ();
    allTabs[whichTabs] = new Array ();
}

function defineTab (whichTabs, theHeading, theFunction, theQuery, theParams, theValues, theCount) {
    var tabNumber = allTabs[whichTabs].length;
    allTabs[whichTabs][tabNumber] = new Array ();
    allTabs[whichTabs][tabNumber]["theHeading"] = theHeading;
    allTabs[whichTabs][tabNumber]["theFunction"] = theFunction;
    allTabs[whichTabs][tabNumber]["theQuery"] = theQuery;
    allTabs[whichTabs][tabNumber]["theParams"] = theParams;
    allTabs[whichTabs][tabNumber]["theValues"] = theValues;
    allTabs[whichTabs][tabNumber]["theCount"] = theCount;
}

function redefineTab (whichTabs, tabNumber, theParams, theValues) {
    allTabs[whichTabs][tabNumber]["theParams"] = theParams;
    allTabs[whichTabs][tabNumber]["theValues"] = theValues;

    drawTabs (whichTabs, tabNumber);
}

function drawTabs (whichTabs, tabNumber) {
	var theTabs = '<table class="tabSectionHeading" cellpadding="0" cellspacing="0">';
	theTabs += '<tr>';
	theTabs += '<td class="tab_preheading"><img src="themes/client_default/segawest/images/tabs/tab_0.gif" /></td>';

	switch (tabNumber) {
		case 0 : theTabs += '<td class="tabedge"><img src="themes/client_default/segawest/images/tabs/tab_1.gif" /></td>'; break;
		default : theTabs += '<td class="tabedge"><img src="themes/client_default/segawest/images/tabs/tab_5.gif" /></td>'; break;
	}

	for (x = 0; x < allTabs[whichTabs].length; x++) {
		x == tabNumber ? whichClass = 'tab_on' : whichClass = 'tab_off';
		theTabs += '<td class="' + whichClass + '"><div>';
		theTabs += (x != tabNumber) ? '<a class="tabLink" href="javascript:drawTabs (\'' + whichTabs + '\', ' + x + ')">' : '';
		theTabs += removeSpaces (allTabs[whichTabs][x]["theHeading"]);
		theTabs += (x != tabNumber) ? '<\/a>' : '';
		theTabs += '</div></td>';

		if (x != (allTabs[whichTabs].length - 1)) {
			if (x != tabNumber) {
				if ((x + 1) != tabNumber) {
					theTabs += '<td class="tabedge"><img src="themes/client_default/segawest/images/tabs/tab_3.gif" /></td>';
				} else {
					theTabs += '<td class="tabedge"><img src="themes/client_default/segawest/images/tabs/tab_6.gif" /></td>';
				}
			} else {
				theTabs += '<td class="tabedge"><img src="themes/client_default/segawest/images/tabs/tab_2.gif" /></td>';
			}
		} else {
			if (x != tabNumber) {
				theTabs += '<td class="tabedgeEnd"><img src="themes/client_default/segawest/images/tabs/tab_4.gif" /></td>';
			} else {
				theTabs += '<td class="tabedgeEnd"><img src="themes/client_default/segawest/images/tabs/tab_7.gif" /></td>';
			}
		}
	}
	theTabs += '<td class="tab_repeat">&nbsp;</td>';
	theTabs += '<td class="tab_ending"><img src="themes/client_default/segawest/images/tabs/tab_8.gif" /></td>';
	theTabs += '</tr></table>';

	document.getElementById ('tabs_' + whichTabs).innerHTML = theTabs;

    selectedTabs[whichTabs] = tabNumber;
	populateTabs (whichTabs, tabNumber);
}

function populateTabs (whichTabs, tabNumber) {
    theQuery = allTabs[whichTabs][tabNumber]["theQuery"];
    theParams = allTabs[whichTabs][tabNumber]["theParams"];
    theValues = allTabs[whichTabs][tabNumber]["theValues"];

    // Sort it out for the relevant browser
    var xmlhttp = false;
    try {xmlhttp = new ActiveXObject ('Msxml2.XMLHTTP');} catch (e) {
        try {xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');} catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') xmlhttp = new XMLHttpRequest ();

    // Build up the parameters string from passed in parameters and values
    var paramString = '';
    for (z = 0; z < theParams.length; z++) paramString += '&' + theParams[z] + '=' + theValues[z];

    // Perform the query
    xmlhttp.open ('GET', '../ajax/ajax-query.php?query=' + theQuery + paramString, true);

    // 0 - Not initialised, 1 - Loading data, 2 - Loaded data, 3 - Data available, 4 - Initialised
    xmlhttp.onreadystatechange = function () {
        switch (xmlhttp.readyState) {
            case 0 : case 1 : case 2 : case 3 : displayLoading (whichTabs); break;
            case 4 :
                // Now display the results
                displayResults (whichTabs, xmlhttp.responseXML);
            break;
        }
    }

    xmlhttp.send (null);
    return;
}

// FUNCTION TO DISPLAY CUSTOM LOADING ICON FOR AJAX-RETRIEVED CONTENT IN TABS
function displayLoading (whichTabs) {
    whichTabs = document.getElementById ('content_' + whichTabs);
    whichTabs.innerHTML = '<center><img src="themes/client_default/segawest/images/icons/ajax-loader.gif" /><\/center>';
}

// FUNCTION TO DISPLAY RESULTS FOR AJAX-RETRIEVED CONTENT IN TABS
function displayResults (whichTabs, resultXML) {
    // CHECK THE COUNT (FOR PAGINATION PURPOSES)
    result = resultXML.getElementsByTagName ("result");
    allTabs[whichTabs][selectedTabs[whichTabs]]["theCount"] = result[0].getAttribute("count");
    theFunction = allTabs[whichTabs][selectedTabs[whichTabs]]["theFunction"];

    itemArray = resultXML.getElementsByTagName ('item');
    resultHTML = '';

    if (result[0].getAttribute("count") == 0) {
        resultHTML += '<div class="errorItem">' + itemArray[0].firstChild.nodeValue + '<\/div>';
        if (document.getElementById ('main_' + whichTabs)) {
            document.getElementById ('main_' + whichTabs).style.display = "none";
        }
    } else {
        switch (theFunction) {
            case "news" :
                for (j = 0; j < itemArray.length; j++) {
                    if (itemArray[j].getAttribute("image") != "") {
                        image = itemArray[j].getAttribute("image");
                    } else {
                        image = 'themes/client_default/segawest/images/icons/sega_icon.jpg';
                    }

                    resultHTML += '<div class="newsItem">';
                    resultHTML += '<div class="image"><a href="../news/?n=' + itemArray[j].getAttribute("id") + '"><img src="' + image + '" /><\/a><\/div>';
                    resultHTML += '<a href="../news/?n=' + itemArray[j].getAttribute("id") + '">' + itemArray[j].getAttribute("heading") + '<\/a><br \/>';
                    resultHTML += '<span class="date">' + itemArray[j].getAttribute("date") + '<\/span>';
                    resultHTML += '<\/div>';
                }
            break;
            case "downloads" :
                for (j = 0; j < itemArray.length; j++) {
                    resultHTML += '<div class="downloadItem">';
                    resultHTML += '<div class="date">' + itemArray[j].getAttribute("date") + '<\/div>';
                    resultHTML += '<a href="' + itemArray[j].getAttribute("link") + '">' + itemArray[j].getAttribute("title") + '<\/a>';
                    resultHTML += '<span class="size"> (' + itemArray[j].getAttribute("size") + ' Mb)<\/span><br \/>';
                    resultHTML += '<span class="text">' + itemArray[j].firstChild.nodeValue + '<\/span>';
                    resultHTML += '<\/div>';
                }
            break;
            case "games" :
                for (j = 0; j < itemArray.length; j++) {
                    if (itemArray[j].getAttribute("packshot") != "") {
                        image = itemArray[j].getAttribute("packshot");
                    } else {
                        image = 'images/icons/sega_icon.jpg';
                    }

                    resultHTML += '<div class="gameItem">';
                    resultHTML += '<div class="image"><a href="' + itemArray[j].getAttribute("link") + '"><img src="' + image + '" /><\/a><\/div>';
                    resultHTML += '<div class="textarea">';
                    resultHTML += '<a href="' + itemArray[j].getAttribute("link") + '">' + itemArray[j].getAttribute("name") + '<\/a> - ';
                    resultHTML += '<span class="text"> ' + itemArray[j].getAttribute("platforms") + ' <\/span>';
    //              resultHTML += '<span class="text">' + itemArray[j].firstChild.nodeValue + '<\/span>';
                    resultHTML += '<span class="date">' + itemArray[j].getAttribute("releasedate") + '<\/span>';
    //              resultHTML += '<span class="text">' + itemArray[j].getAttribute("summary") + '<\/span>';
                    resultHTML += '<\/div>';
                    resultHTML += '<\/div>';
                    resultHTML += '<div class="clearingDiv" style="height: 0px">&nbsp;<\/div>';
                }
            break;
            case "media" :
                for (j = 0; j < itemArray.length; j++) {
                    resultHTML += '<div class="mediaItem">';
                    resultHTML += '<a href="' + itemArray[j].getAttribute("src") + '" rel="lightbox[]" title="' + itemArray[j].getAttribute("game") + ': ' + itemArray[j].getAttribute("title") + '" >';
                    resultHTML += '<div style="background: url(' + itemArray[j].getAttribute("thumb") + ') no-repeat center" class="image"><div class="icon" onmouseover="javascript: imageChange (\'themes/client_default/segawest/images/buttons/ss_1.gif\', \'button_' + whichTabs + '_' + j + '\');" onmouseout="javascript: imageChange (\'themes/client_default/segawest/images/buttons/ss_0.gif\', \'button_' + whichTabs + '_' + j + '\');"><img src="themes/client_default/segawest/images/buttons/ss_0.gif" id="button_' + whichTabs + '_' + j + '" \/><\/div><\/div>';
                    resultHTML += '<div class="text">' + itemArray[j].getAttribute("game") + '<\/div>';
                    resultHTML += '<\/a>';
                    resultHTML += '<\/div>';
                }
            break;
            case "screenshots" :
                for (j = 0; j < itemArray.length; j++) {
                    resultHTML += '<div class="screenshotItem">';
                    resultHTML += '<a href="' + itemArray[j].getAttribute("src") + '" rel="lightbox[]" title="' + itemArray[j].getAttribute("game") + ': ' + itemArray[j].getAttribute("title") + '" >';
                    resultHTML += '<div style="background: url(' + itemArray[j].getAttribute("thumb") + ') no-repeat center" class="image"><div class="icon" onmouseover="javascript: imageChange (\'themes/client_default/segawest/images/buttons/ss_1.gif\', \'button_' + whichTabs + '_' + j + '\');" onmouseout="javascript: imageChange (\'themes/client_default/segawest/images/buttons/ss_0.gif\', \'button_' + whichTabs + '_' + j + '\');"><img src="themes/client_default/segawest/images/buttons/ss_0.gif" id="button_' + whichTabs + '_' + j + '" \/><\/div><\/div>';
                    resultHTML += '<\/a>';
                    resultHTML += '<\/div>';
                }
            break;
            case "videos1" :
                for (j = 0; j < itemArray.length; j++) {
                    resultHTML += '<div class="videoItem">';
                    resultHTML += '<a href="../games/?g=' + itemArray[j].getAttribute("gameid") + '&v=' + itemArray[j].getAttribute("id") + '" title="' + itemArray[j].getAttribute("gamename") + ': ' + itemArray[j].getAttribute("title") + '" >';
                    resultHTML += '<div style="background: url(' + itemArray[j].getAttribute("thumb") + ') no-repeat center" class="image"><div class="icon" onmouseover="javascript: imageChange (\'themes/client_default/segawest/images/buttons/ss_1.gif\', \'button_' + whichTabs + '_' + j + '\');" onmouseout="javascript: imageChange (\'themes/client_default/segawest/images/buttons/ss_0.gif\', \'button_' + whichTabs + '_' + j + '\');"><img src="themes/client_default/segawest/images/buttons/ss_0.gif" id="button_' + whichTabs + '_' + j + '" \/><\/div><\/div>';
                    resultHTML += '<div class="text">' + itemArray[j].getAttribute("gamename") + '<\/div>';
                    resultHTML += '<\/a>';
                    resultHTML += '<\/div>';
                }
            break;
            case "videos2" :
                for (j = 0; j < itemArray.length; j++) {
                    resultHTML += '<div class="videoItem">';
                    resultHTML += '<a href="javascript:void(0)" onclick="javascript:loadFLV(\'' + itemArray[j].getAttribute("src") + '\', \'true\')" title="' + itemArray[j].getAttribute("title") + '" class="VideoTips">';
                    resultHTML += '<div style="background: url(' + itemArray[j].getAttribute("thumb") + ') no-repeat center" class="image"><div class="icon" onmouseover="javascript: imageChange (\'themes/client_default/segawest/images/buttons/ss_1.gif\', \'button_' + whichTabs + '_' + j + '\');" onmouseout="javascript: imageChange (\'themes/client_default/segawest/images/buttons/ss_0.gif\', \'button_' + whichTabs + '_' + j + '\');"><img src="themes/client_default/segawest/images/buttons/ss_0.gif" id="button_' + whichTabs + '_' + j + '" \/><\/div><\/div>';
                    resultHTML += '<\/a>';
                    resultHTML += '<\/div>';
                }
            break;
            case "content" :
                for (j = 0; j < itemArray.length; j++) {
                    resultHTML += '<div class="contentItem">';
                    resultHTML += '<div class="text">' + itemArray[j].firstChild.nodeValue + '<\/div>';
                    resultHTML += '<\/div>';
                }
            break;
            case "reviews" :
                resultHTML += '<div class="reviewItems">';
                for (j = 0; j < itemArray.length; j++) {
                    if (itemArray[j].getAttribute("reviewerlogo") != "") {
                        image = itemArray[j].getAttribute("reviewerlogo");
                    } else {
                        image = 'images/icons/sega_icon.jpg';
                    }
    //                resultHTML += '<h3 class="toggler atStart"><a class="button1_on" href="javascript:void(0)" style="float: left; width: 360px; text-align: left"><span>' + itemArray[j].getAttribute("heading") + ' - ' + itemArray[j].getAttribute("reviewername") + '</span></a></h3>';
                    resultHTML += '<div class="reviewItem">';
                    resultHTML += '<div class="heading"><a class="heading" href="' + (itemArray[j].getAttribute("reviewwebsite") != '' ? itemArray[j].getAttribute("reviewwebsite") : itemArray[j].getAttribute("reviewerwebsite")) + '" target="_blank">' + itemArray[j].getAttribute("heading") + '</a></div>';
                    resultHTML += '<div class="reviewer"><a href="' + itemArray[j].getAttribute("reviewerwebsite") + '" target="_blank">' + itemArray[j].getAttribute("reviewername") + '</a></div>';
                    resultHTML += (itemArray[j].getAttribute("score") > 0) ? '<div class="score">' + itemArray[j].getAttribute("score") + '%</div>' : '';
                    //resultHTML += '<img src="' + image + '" class="image" \/>';
                    //resultHTML += '<div class="text">' + itemArray[j].firstChild.nodeValue + '</div>';
                    resultHTML += '</div>';
                }
                resultHTML += '</div>';
            break;
            case "blank" :
                resultHTML = '';
            break;
        }
    }

    whichTabs = document.getElementById ('content_' + whichTabs);
    if (resultHTML != '') {
        whichTabs.innerHTML = resultHTML;
    } else {
        whichTabs.innerHTML = "";
    }

    if (result[0].getAttribute("count") > 0) {
        // IF IT IS ONE THAT NEEDS SLIMBOX
        if ((theFunction == "media") || (theFunction == "screenshots")) {
            Lightbox.init (Lightbox);
        }

        // IF IT IS ONE THAT NEEDS TOOLTIPS
        if (theFunction == "videos2") {
            var VideoTips = new Tips($$('.VideoTips'), {className: 'videotip'});
        }

        // IF IT IS ONE THAT NEEDS THE MOO TOOLS ACCORDION
        if (theFunction == "reviews") {
    		var accordion = new Accordion('h3.atStart', 'div.atStart', {}, $('accordion'));
    		var newTog = new MooElement('h3', {'class': 'toggler'});
    		var newEl = new MooElement('div', {'class': 'element'});
    		accordion.addSection(newTog, newEl, 0);
        }
    }
}

function skipPage (whichTabs, whichWay) {
    tabNumber = selectedTabs[whichTabs];
    theQuery = allTabs[whichTabs][tabNumber]["theQuery"];
    theParams = allTabs[whichTabs][tabNumber]["theParams"];
    theValues = allTabs[whichTabs][tabNumber]["theValues"];
    theCount = allTabs[whichTabs][tabNumber]["theCount"];

    for (x = 0; x < theParams.length; x++) {
        if (theParams[x] == "start") start = x;
        if (theParams[x] == "limit") limit = x;
    }

    if (whichWay == 'next') {
        theValues[start] = 1 * (theValues[start] * 1) + (theValues[limit] * 1);
    } else if (whichWay == 'prev') {
        theValues[start] = 1 * (theValues[start] * 1) - (theValues[limit] * 1);
    }

    if (((theValues[start] * 1) >= 0) && ((theValues[start] * 1) < theCount)) {
        allTabs[whichTabs][tabNumber]["theValues"] = theValues;
        drawTabs (whichTabs, tabNumber);
    } else {
        if (whichWay == 'next') {
            theValues[start] = 1 * (theValues[start] * 1) - (theValues[limit] * 1);
        } else if (whichWay == 'prev') {
            theValues[start] = 1 * (theValues[start] * 1) + (theValues[limit] * 1);
        }
    }
}

function removeSpaces (theString) {
	var result = '';
	theString = '' + theString;
	splitString = theString.split (" ");
	for (i = 0; i < splitString.length; i++) result += splitString[i] + "&nbsp;";
	return result;
}

/* PRIMARILY USED FOR SCREENSHOTS THUMBNAILS */
function imageChange (theURL, theOne) {
    document.getElementById (theOne).src = theURL;
}

/* This is a function that goes through all the buttons in a range (theButtons), and unhighlights them, whilst highlighting the chosen one (theButton) */
function doButtons (theButtons, theButton) {
    found = true;
    for (x = 0; x < 100; x++) {
        theItem = document.getElementById (theButtons + x);
        if (theItem) {
            if (x == theButton) {
                theItem.setAttribute('class', 'button4_on');
            } else {
                theItem.setAttribute('class', 'button4');
            }
        } else {
            x = 100;
        }
    }
}
