
String.prototype.parseJSON = function () {
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                this.replace(/"(\\.|[^"\\])*"/g, ''))) &&
            eval('(' + this + ')');
    } catch (e) {
        alert('message: ' + e.message + ', description: ' + e.description + ', contstructor: ' +  + e.constructor);
        return false;
    }
};

function menuPop(status,id){
	m = $(id)
	if (status == "true"){
		m.style.display = "block";
	}
	else if(status = "false"){
		m.style.display = "none";
	}
}

function rollUp(id){
	l = $(id)
	Effect.SlideUp(l, {duration: 1})
}
function resizeShort(id){
	s = $(id)
	new Effect.Scale(id, 150, {scaleX:false, scaleY:true, scaleContent:false});
}	

var cB = "container-balloon";
var bC = "balloon-content";
var mouseover = "false";
var myV;
var myAction;
var myCaller;

function spawnB(caller,e,v,action){
	if( v == "show" ){
		var IE = document.all ? true : false; 
		if (IE) {
			xCord = event.clientX + document.documentElement.scrollLeft;
			yCord = event.clientY + document.documentElement.scrollTop;
		}
		else {	
			xCord = e.pageX;
			yCord = e.pageY;
		}
		myV = v;
		myAction = action;
		myCaller = caller;

		t = setTimeout(toggleB, 800);
	}
	else {
		clearTimeout(t);
		b = $(cB)
		b.style.display = "none";
	}
}
function toggleB() {
	if( myV = "show" ) {		
		getArtistBio( myAction, myCaller );
	}
}
function writeB(){
	// write balloon
	var b = $(cB)
	b.style.display = "block";
	b.style.left = (xCord - 248) + "px";
	b.style.top = (yCord - 25) + "px";
	mouseover = false;
}




Effect.MoveAndResizeTo = Class.create();
Object.extend(
	Object.extend(
		Effect.MoveAndResizeTo.prototype, 
		Effect.Base.prototype
	), 
	{
  			initialize: function(element, toTop, toLeft, toWidth, toHeight) 
		{
			this.element      = $(element);
			this.toTop        = toTop;
			this.toLeft       = toLeft;
			this.toWidth      = toWidth;
			this.toHeight     = toHeight;
			this.originalTop  = parseFloat(Element.getStyle(this.element,'top')  || 0);
			this.originalLeft = parseFloat(Element.getStyle(this.element,'left') || 0);
			this.originalWidth  = parseFloat(Element.getStyle(this.element,'width')  || 0);
			this.originalHeight = parseFloat(Element.getStyle(this.element,'height') || 0);
			this.effectiveTop = this.toTop;
			this.effectiveLeft = this.toLeft;
			this.effectiveWidth = this.toWidth;
			this.effectiveHeight = this.toHeight;
                    - parseFloat(Element.getStyle(this.element,'margin-top') || 0) 
                    - parseFloat(Element.getStyle(this.element,'margin-bottom') || 0) 
                    - (document.compatMode == 'BackCompat' ? 0 : // height includes padding & border in IE BackCompat mode
                        parseFloat(Element.getStyle(this.element,'padding-top') || 0) 
                        + parseFloat(Element.getStyle(this.element,'padding-bottom') || 0) 
                        + parseFloat(Element.getStyle(this.element,'border-top-width') || 0)
                        + parseFloat(Element.getStyle(this.element,'border-bottom-width') || 0));

			this.options = arguments[5] || {};

			if (this.effectiveWidth < 0) this.effectiveWidth = 0;
			if (this.effectiveHeight < 0) this.effectiveHeight = 0;

			if (this.originalTop == this.effectiveTop &&
    		this.originalLeft == this.effectiveLeft &&
    		this.originalWidth == this.effectiveWidth &&
    		this.originalHeight == this.effectiveHeight) 
			{

  				// no need to start!
 		 		return;
			}
			this.start(this.options);

		},
		update: function(position) 
		{
			topd  = this.effectiveTop * (position) + this.originalTop * (1 - position);
			leftd = this.effectiveLeft * (position) + this.originalLeft * (1 - position);
			widthd  = this.effectiveWidth * (position) + this.originalWidth * (1 - position);
			heightd = this.effectiveHeight * (position) + this.originalHeight * (1 - position);
			this.setPosition(topd, leftd, widthd, heightd);
		},
		setPosition: function(topd, leftd, widthd, heightd) 
		{
			this.element.style.top = topd+'px';
			this.element.style.left = leftd+'px';
			this.element.style.width = widthd+'px';
			this.element.style.height = heightd+'px';
		}
	}
);

function centerImg(num) {
	prev_img = center_img;
	drawCarousel(num);
	//First image or image clicked twice
	if (center_img == num) {
		new Effect.MoveAndResizeTo('thumb_' + num, 20, 0, 120, 67, {duration: .2});
		$('thumb_' + num).className = 'cnt';
		$('caption_' + num).className = 'cnt_text';
        if( $('thumb_' + num).name == 'add_item' ) {
            populateAddAlert();
        }
        else {
            getAlertInfo( $('thumb_' + num).name );
        }
    	updateAlertCount(num);
	}
	// A different image is in the center
	if (num != center_img) {
		var tmp = center_img;
		center_img = num;
		new Effect.Parallel
		(
			[
				new Effect.MoveAndResizeTo(	'thumb_' + tmp, 20, 67, 65, 36, {sync: true}),
				new Effect.MoveAndResizeTo('thumb_' + num, 16, 180, 120, 67, {sync: true	}), 
				new Effect.MoveBy('carousel', 0, dis, {sync: true})
			],
			{
				duration: .5,
				queue: {position: 'end', scope: 'carousel'},
				beforeStart: function(obj)
				{
					gonext = false;
					goback = false;
					$('thumb_'+tmp).className = 'reg';
					$('caption_'+tmp).className = 'reg_text';
					$('thumb_'+num).className = 'cnt';
					$('caption_'+num).className = 'cnt_text';
                    if( $('thumb_' + num).name == 'add_item' ) {
                        populateAddAlert();
                    }
                    else {
                        getAlertInfo( $('thumb_' + num).name );
                    }
                	updateAlertCount(num);
				}, 
				afterFinish: function(obj)
				{
					gonext = true;
					goback = true;
				}
			}
		);
	}
}

function drawCarousel(num) {
	// Get the <tr> element in which we'll be pasting the carousel
	var carousel_row = $('carousel_row');
	// Calculate the starting point padding with 7 images on each side
	var startfrom = parseInt(num) - 7;
	if (startfrom < 0) {
		startfrom = img_total + startfrom;
	}
	var i = startfrom;
	removeChildrenFromNode(carousel_row);
	for( var k = 0; k < 14; k++ ) {
		var td = document.createElement("td");
		var img = document.createElement("img");
		var div = document.createElement("div");
		td.appendChild(img);
		td.appendChild(div);
		carousel_row.appendChild(td);
		td.id = 'td_' + i;
		td.style.verticalAlign = 'top';
		div.id = 'caption_' + i;
		div.className = 'reg_text';
		div.innerHTML = images.imgs[i].caption;
		img.className = 'reg';
		img.style.width = '65px';
		img.style.height = '36px';
		img.id = 'thumb_' + i;
		img.name = images.imgs[i].name;
		img.src = images.imgs[i].file;
		img.onclick = function() {
			var tmp = this.id.substring(parseInt(this.id.indexOf('_')) + 1);
			individualClick(tmp);
		};
		if (i == prev_img) {
			var px = ((k-2) * 65) + ((k-2) * 20);
			$('carousel').style.left = '-' + px + 'px';
			img.style.width = '120px';
			img.style.height = '67px';
			img.className = 'cnt';
			div.className = 'cnt_text';
		}
		++i;
		if (i == img_total) {
			i = 0;
		}
	}
}

function removeChildrenFromNode(node) {
	if (node !== undefined && node === null) {
		return;
   	}
   	var len = node.childNodes.length;
   	while (node.hasChildNodes()) {
	  node.removeChild(node.firstChild);
	}
}

function decideWebTrend() {
    switch( myAlert['type'] ) {
        case 'performer':
            return 'myperf';
        break;
        case 'team':
            return 'myteam';
        break;
        case 'venue':
            return 'myvenue';
        break;
    }
}

function individualClick(num) {
	var tmp = parseInt(center_img) - parseInt(num);
	if (tmp <= 2 && tmp >= -2) {
		dis = (tmp * 65) + (tmp * 20);
	}
	else {
		if (tmp > 2) {
			if (center_img > img_total - 3) {
				tmp = - (img_total - tmp);
			}
			else 
				tmp = img_total - tmp;
		}
		else
			tmp = img_total + tmp;
	}
	dis = (tmp * 65) + (tmp * 20);
	webTrends('mytm_' + decideWebTrend() + '_tile');
	centerImg(num);
}

function goBack()
{
	if (goback) {
		var img = parseInt(center_img) - 4;
		if (img < 0) {
			img = img_total + img;
		}
		dis = (4 * 65) + (4 * 20);
		centerImg(img);
		webTrends('mytm_' + decideWebTrend() + '_rewind');
	}
}
function goNext()
{
	if (gonext) {
		var last_img = img_total - 1;
		var last_center = img_total - 2;
		var img = parseInt(center_img) + 4;
		if (img > last_img) {
			img = img - img_total;
		}
		dis = (-4 * 65) + (-4 * 20);
		webTrends('mytm_' + decideWebTrend() + '_ff');
		centerImg(img);
	}
}
function goBackOne()
{
	if (goback) {
		var img = parseInt(center_img) - 1;
		if (img < 0) {
				img = img_total + img;
		}
		dis = (1 * 65) + (1 * 20);
		webTrends('mytm_' + decideWebTrend() + '_prev');
		centerImg(img);
	}
}
function goNextOne()
{
	if (gonext) {
		var img = parseInt(center_img) + 1;
		if (img > img_total - 1) {
				img = img - img_total;
		}
		dis = (-1 * 65) + (-1 * 20);
		webTrends('mytm_' + decideWebTrend() + '_next');
		centerImg(img);
	}
}

	
