//prod
//http://maps.seatics.com/maincontrol_qa_tn.js
//http://maps.seatics.com/maincontrol_tn.js

var curZoomLvl = 0;
var maxZoom = 4; //(400%)
var zoomSteps = 10; //11 including no zoom
var hasVFS = false;
var openedVFS = false;
$('#ssc_mapCell').height('525px');
var listAndMapParms = {
    mapShellURL: mapUrl, //"http://maps.seatics.com/mapshell_tn.swf",
    sectionSelectColor: 'FF8c00',
    sectionHiliteColor: 'AAAAff',
    sectionNoSeatsColor: 'FFFFFF',
    sectionInListColor: '6699CC',
    sectionNotInListColor: 'C9D6E2',
    showTGsInNotSelectedSections: false,
    showTGsNotOnMap: 'bottom',
    showListGroupHeaders: 0,
    vfsFilterAnimate: 'none',
    vfsImageAnimate: 'vertical-list',
    vfsMsecs: 800,
    dynMapMinTgMapHitPct: 60,
    tgMarks : ['<img src="http://content.ticketnetwork.com/images/red_checkmark.gif" />'],
    listType: 'scrolled',
    tgListFilterHTML: TNfilterHTML,
    tgListHdrHTML: TNlikeTGhdr,
    tgListRowHTML: TNlikeTGrow,
    tgListReady: TNsummaryHTML,
    listAndMapLoaded: TNlistAndMapLoaded,
    sortArrows: ['<a id="activeSrtDESC" class="tixSrtLink"></a>', "", '<a id="activeSrtASC" class="tixSrtLink"></a>'],
    //tgListFilterPricePoints: [0, 100, 250, 500, 1000],
    tgListFilterPricePoints: [0, 100, 250, 500, 1000, 999999999999999999999999],  //fix for bug
    tgMarkIX : 8, //had to change that coz default is 9,we don't grab the userseat 
    swfMapURL: flashMapURL,
    staticMapURL: staticMapURL,
    vfsEnable: 'hover',
    jsTrace: 0
};

if (navigator.userAgent.toLowerCase().indexOf("droid") > -1) {
    ssc.EH.customListRowClicked = function (ticketGroup) {
        document.getElementById('tg_button_' + ticketGroup.tg.tgTGcode).click();
    }
}

ssc.loadTgList(TGData, listAndMapParms);
ssc.setMapOptions('showSectionClickInvite=false');
//if(eventId == 1558921)
//{
//ssc.setOptions({showDynamicMap:0});
//}
ssc.sortTgList('price', 'asc');

TGData = "";

function TNlistAndMapLoaded(status) {
    //-3: static map (no flash map available)
    //1: static map loaded with flash
    //2: flash map (with or without vfs)

    //if the map loaded correctly, check if there is a vfs
    if (status >= 1) { //flash map loaded (may be showing static version if hit rate is low)
        var mapHeight = $('#ssc_mapCell').height();
        $('#ssc_contentDiv').height(mapHeight);
        if (status >= 2) { //interactive map
            if (hasVFS) {
                $('#map_info_VFS').show();
            }
            	if (showInstructions && !onSite) {
				$('#balloon').show();
			}
        }
    }
    //otherwise we need to add zoom in/zoom out capability to the map
    else {
        //set function to image load event if it hasn't yet loaded
        $('#ssc_staticMap').load(function () {
            AddZoomAndResizeTGList();
        });
        //necessary to have this too (in case image has already loaded - bound event won't trigger)
        $('#ssc_staticMap').each(function () {
            if (this.complete) {
                ssc.tt("Image already loaded, triggering manually");
                $(this).trigger("load");
            }
        });
    }

    $('#tgLoading').hide();
    CreateTGMbox(status);

}

ssc.EH.keyEvent = function(params) {
    if (params.code == "VM") {
        if (!openedVFS) {
            if ($('#mboxMarker-default-tn_general_mbox-0').length == 0)
                mboxDefine('dynamicElement', 'tn_general_mbox', 's_UsedSeatViews=true');
            mboxUpdate('tn_general_mbox', 's_UsedSeatViews=true');
        }
        openedVFS = true;
    }
}

function AddZoomAndResizeTGList() {
    //fix chrome drag and drop
    document.getElementById("ssc_staticMap").onmousedown = function () { return false; };
    AddZoomCapability();
    var mapHeight = $('#ssc_staticMap').height();
    $('#ssc_contentDiv').height(mapHeight);
    var colHdrHeight = $('#ssc_listColHdr').height();
    var filterHeight = $('#ssc_filterDiv').height();
    var tktGroupHeight = mapHeight - (colHdrHeight + filterHeight);
    $('#ssc_tktGroups').css('cssText', 'height: ' + tktGroupHeight + 'px'); //added for IE6 compatibility
}

//========================================================
function TNsummaryHTML(criteria, listStats) {
}

function MinMaxChangedParse(value) {
    var priceRange = value.split("-");
    ssc.criteriaChanged('MinPrice=' + priceRange[0] + '&MaxPrice=' + priceRange[1]);
}

function TNfilterHTML(criteria) {
    //build and replace html for summaryblurb
    ssc.tt("Filter: TN start.");
    var selList = '<table class="ssc_filterTbl" border="0" cellpadding="0" cellspacing="0"><tr><td NOWRAP colspan="2">',
       i,
       px,
       qtyWidth,
       qtyFollow;

    // Build price range links
    selList += '<div class="ssc_tn_filter ssc_tn_filter_price"><h3>Price Range:&nbsp;&nbsp;';
    selList += '<select id="ssc_tn_price_select" onchange="MinMaxChangedParse(this.value)">';
    selList += '<option value="0-0"'
    if (criteria.minPrice == 0 && criteria.maxPrice == 0)
        selList += ' class="tn_active_filter"';
    selList += '>All</option>';

    for (px = 1; px < criteria.okPricePoints.length; px++) {
        if (px != criteria.okPricePoints.length - 1) { //bug fix
            selList += '<option value="' + criteria.okPricePoints[px - 1] + '-' + criteria.okPricePoints[px] + '"';
            if (criteria.minPrice == criteria.okPricePoints[px - 1] && criteria.maxPrice == criteria.okPricePoints[px])
                selList += ' class="tn_active_filter" selected';
            if (!(criteria.loPricePoints[px - 1] || criteria.hiPricePoints[px]))
                selList += ' class="tn_empty_filter" disabled="disabled"';
            selList += '>';
            selList += ((criteria.okPricePoints[px - 1] == 0) ? 'Less than ' : criteria.currencySymbol + criteria.okPricePoints[px - 1] + '-') + criteria.currencySymbol + criteria.okPricePoints[px];
            selList += '</option>';

            //if this is the last price point, make a 'Greater than' select
            //if (px == criteria.okPricePoints.length - 1) {
            if (px == criteria.okPricePoints.length - 2) { //bug fix
                selList += '<option value="' + criteria.okPricePoints[px] + '-0"';
                if (criteria.minPrice == criteria.okPricePoints[px - 1] && criteria.maxPrice == 0)
                    selList += ' class="tn_active_filter" selected';
                if (!criteria.loPricePoints[px])
                    selList += ' class="tn_empty_filter" disabled="disabled"';
                selList += '>More than ' + criteria.currencySymbol + criteria.okPricePoints[px] + '</option>';
            }
        }  //bug fix
    }

    selList += '</select></h3></div>';

    // build qty combo
    selList += '<div class="ssc_tn_filter ssc_tn_filter_quant"><h3>Number of Tickets:&nbsp;&nbsp;';
    selList += '<select id="ssc_tn_quantity_select" onchange="ssc.criteriaChanged(\'ReqQty=\'+this.value)">';
    selList += '<option value="0"';
    if (criteria.reqQty == 0)
        selList += ' class="tn_active_filter" selected';
    selList += '>Any</option>';

    for (i = 1; i <= criteria.hiQty; i++) {
        if (i == criteria.hiQty - 1 && i != criteria.hiQtyNxt) continue;
        selList += '<option value="' + i + '" class="ssc_qtyValues' + ((criteria.reqQty == i) ? ' tn_active_filter" selected' : '"') + '>' + i + '</option>'
    }

    selList += '</select></h3></div>';

    //View all
    selList += '<div class="ssc_tn_filter_submit"><input type="submit" value="View All" onClick="ssc.criteriaChanged(\'MaxPrice=0&MinPrice=0&ReqQty=0\');ssc.setMapOptions(\'clearSectionSelect=*\');return false;" /></div>';
    selList += '</td></tr><tr><td NOWRAP>';

    ssc.tt("Filter: TN end.");
    return '<table class="ssc_filterTbl" cellpadding="0" cellspacing="0" border="0"><tr><td>' + selList + '</td></tr></table>';
}

// ======================================== determine what to show in the TG row
function TNlikeTGhdr(criteria) {
    return '<table class="ssc_lhTable">' +
	'<tr valign="bottom" >' + ((criteria.tgMarks.length > 0 && highlightTix && isHilitedTixPresent) ? '<td class="ssc_hMark"></td>' : "") +
    '<td class="ssc_lhSec resultsHeadingTD" onclick="ssc.sortTgList(\'secrow\');"><div class="colLblContainer"><a>Section</a></div><div class="tixSrtLinksContainer">' + criteria.columnSortArrows['SECROW'] + '</div></td>' +
    '<td class="ssc_lhRow resultsHeadingTD" onclick="ssc.sortTgList(\'row\');"><div class="colLblContainer"><a>Row</a></div><div class="tixSrtLinksContainer">' + criteria.columnSortArrows['ROW'] + '</div></td>' +
	'<td class="ssc_lhQty resultsHeadingTD" onclick="ssc.sortTgList(\'qty\')"><div class="colLblContainer"><a>Quantity</a></div><div class="tixSrtLinksContainer">' + criteria.columnSortArrows['QTY'] + '</div></td>' +
	'<td class="ssc_lhPri resultsHeadingTD" onclick="ssc.sortTgList(\'price\')"><div class="colLblContainer"><a>Price' +
    ' ea' +
    '</a></div><div class="tixSrtLinksContainer">' + criteria.columnSortArrows['PRICE'] + '</div></td>' +
	'<td class="ssc_lhBuy resultsHeadingTD">' +
    'Prices in USD' +
    '</td></tr>' +
	'</table>';
}

function TNlikeTGrow(criteria, tgIndex, tg) {
    if (tg.tgHasVFS)
        hasVFS = true;
    var dispQty;
    var requestTicketNum = undefined;

    dispQty = tg.tgQty + ' left';
    if (2 != tg.tgQty - 1)
        requestTicketNum = 2;
    else
        requestTicketNum = 1;

    if (criteria.reqQty > 0)
        requestTicketNum = criteria.reqQty;

    var showNotes = tg.tgNotes.trim() != "";
    var notesDiv = "";
    if (showNotes) {
        notesDiv = "<div class='tixDetailsBox'><b>Details:</b>: " + tg.tgNotes + "</div>"
    }

    var starDiv = "";
    if (tg.tgStar != "") {
        starDiv = "<div class='tixStar'></div>";
    }

    var r = "<table border='0' class='ssc_lrTable' cellpadding='0' cellspacing='0'>" +
                "<tr>" +
((criteria.tgMarks.length > 0 && highlightTix && isHilitedTixPresent)? '<td class="ssc_lrMark">' + ((tg.tgMark) ? (criteria.tgMarks[Number(tg.tgMark)] || criteria.tgMarks[1] || criteria.tgMarks[0]) : '') + '</td>' :'') +
		        "<td class='ssc_lrSec'>" + starDiv +
		        "<span>" + tg.tgUserSec + "</span></td>" +
			    "<td class='ssc_lrRow'>" + tg.tgUserRow + "</td>" +
			    "<td class='ssc_lrQty'>" + dispQty + "</td>" +
			    "<td class='ssc_lrPri'><span class='tixPriceLine'>" + criteria.currencySymbol + tg.tgPrice + "</span>" +
                '</td>' +
			    "<td class='ssc_lrBuy' rowspan='" + (showNotes ? 2 : 1) + "'>" +
			    "<input type='button' id='tg_button_" + tg.tgTGcode + "' class='RTSubmitLink' onclick=\"$('#ssc_TG_" + tgIndex + "').attr('class', 'ssc_lrHilite');$(this).addClass('RTSubmitLinkHover');SubmitPurchaseLink('qtySel" + tgIndex + "', '" + checkoutLinkBase + "&tgid=" + tg.tgTGcode + "&price=" + tg.tgPrice + "&h="+ (tg.tgMark?1:0) + "', " + requestTicketNum + ");\" />" +
			   	((showNotes) ? "<tr>" + ((highlightTix&&isHilitedTixPresent)?"<td></td>":"") + "<td colspan=4 class='ssc_lrNotes'><b>Note</b>: " + tg.tgNotes + "</td></tr>" : "") +
                "</tr>";

    return r += "</table>";
}

function AddZoomCapability() {
    var map = $('#ssc_staticMap');
    var mapwrapper = $('#ssc_mapCell center');
    var cell = $('#ssc_mapCell');
    //first of all, set the map to a width/height that is visible in the original square (aka, 475px wide)
    var origHeight = map.height();
    var origWidth = map.width();
    var optimumWidth = cell.width();
    map.width(optimumWidth + 'px');
    map.height(optimumWidth * origHeight / origWidth + 'px');
    //Set ssc_mapCell height to the new optimized height of the static Map
    cell.height(optimumWidth * origHeight / origWidth + 'px');

    //store optimum height/width of map (aka, best fit to viewing pane)
    map.attr('optimumHeight', map.height());
    map.attr('optimumWidth', map.width());

    //map wrapper needs to be twice height and width of map (won't allow map to move more than half-way out of the viewing range)
    mapwrapper.width(map.width() * 2);
    mapwrapper.height(map.height() * 2);

    cell.unbind("mousewheel DOMMouseScroll", ssc.mouseWheelMoved);
    cell.bind("mousewheel DOMMouseScroll", StaticMouseWheel);

    map.draggable({ containment: 'parent' });

    //center map wrapper in the viewing pane
    mapwrapper.css('left', -1 * (mapwrapper.width() / 2 - cell.width() / 2));
    mapwrapper.css('top', -1 * (mapwrapper.height() / 2 - cell.height() / 2));

    //center map within the map wrapper
    map.css('left', mapwrapper.width() / 2 - map.width() / 2);
    map.css('top', mapwrapper.height() / 2 - map.height() / 2)
}

function StaticMouseWheel(e) {
    var dir = e.wheelDelta || -e.detail;
    //Firefox: up: 3 down: -3
    //Every other browser: up: 120 down: -120
    //if zoom in
    if (dir > 0) {
        ZoomIn(1);
    }
    //if zoom out
    else {
        ZoomOut(1);
    }
    return false;
}

function SSCMap_Reset() {
    var map = $('#ssc_staticMap');
    ZoomToLevel(0);
    map.css('left', '0');
    map.css('top', '0');
}

function ZoomToLevel(zoomLevel) {
    var map = $('#ssc_staticMap');
    var mapwrapper = $('#ssc_mapCell center');
    var cell = $('#ssc_mapCell');
    var originalHeight = map.height();
    var originalWidth = map.width();

    //calculate and set the new height/width
    var optHeight = parseInt(map.attr('optimumHeight'));
    var optWidth = parseInt(map.attr('optimumWidth'));
    var optHeightwrapper = optHeight * 2;
    var optWidthwrapper = optWidth * 2;
    var stepMultiplier = Math.pow(maxZoom, zoomLevel / zoomSteps);

    //adjust the map size to the proper zoom level
    map.height(optHeight * stepMultiplier);
    map.width(optWidth * stepMultiplier);

    //adjust the map wrapper to the proper zoom level
    mapwrapper.height(optHeightwrapper * stepMultiplier);
    mapwrapper.width(optWidthwrapper * stepMultiplier);

    //re-center the map wrapper in the viewing pane
    mapwrapper.css('left', -1 * (mapwrapper.width() / 2 - cell.width() / 2));
    mapwrapper.css('top', -1 * (mapwrapper.height() / 2 - cell.height() / 2));

    //scale the map left and top positions to match the new height of the map
    map.css('left', map.width() / originalWidth * map.position().left);
    map.css('top', map.height() / originalHeight * map.position().top);

    curZoomLvl = zoomLevel;
    $('#ssc_map_zoom_slider').slider('option', 'value', curZoomLvl);
}

function ZoomIn(zoomAmount) {
    //if we can zoom in that amount, do so
    if (curZoomLvl + zoomAmount <= zoomSteps) {
        ZoomToLevel(curZoomLvl + zoomAmount);
    }
    //otherwise zoom in as much as we can (if we're not already zoomed in all the way)
    else if (curZoomLvl != zoomSteps) {
        ZoomToLevel(zoomSteps);
    }
}

function ZoomOut(zoomAmount) {
    //if we can zoom out that amount, do so
    if (curZoomLvl - zoomAmount >= 0) {
        ZoomToLevel(curZoomLvl - zoomAmount);
    }
    //otherwise zoom in as much as we can (if we're not already zoomed in all the way)
    else if (curZoomLvl != 0) {
        ZoomToLevel(0);
    }
}

function MoveMap(direction) {
    var map = $('#ssc_staticMap');
    var cell = $('#ssc_mapCell');
    var moveBase = 1 / 4; //fraction of the window that the map will move (ex. 1/4 = map will move 1/4th of the window size)
    var pMSign = (direction == 'down' || direction == 'right') ? 1 : -1;

    switch (direction) {
        case 'up':
        case 'down':
            map.stop().animate(
                { top: '+=' + (pMSign * moveBase * cell.height()) }, 500, 'swing'
            );
            break;
        case 'left':
        case 'right':
            map.stop().animate(
                { left: '+=' + (pMSign * moveBase * cell.width()) }, 500, 'swing'
            );
            break;
    }
}

