

//Functions by Jon
//Scriptaculous helpers

//vis_fx constants
BLIND = "blind";
SHRINK = "shrink";
FADE = "fade";
SLIDE = "slide";
DURATION = 0.5;

document.observe("dom:loaded", function() {
  // initially hide all containers for tab content
  
  setupHelpObservers();
  
});

function setupBubble(triggerClass, bubbleElemID)
{
	if ($(bubbleElemID) != null)
	  {
		$(bubbleElemID).setStyle({opacity:0});
		var timer = null;
	    var shown = false;
	    var beingShown = false;
		
	    $$('.' + triggerClass + ', #' + bubbleElemID).each(function(item) {
	
	          item.observe("mouseover", function(){
	      	      $(bubbleElemID).show();
				  
	      	      if (timer) clearTimeout(timer);
	      	      if (!beingShown && !shown)
	      	      {
	      	          beingShown = true;
					  new Effect.Parallel([
	      	          	new Effect.Opacity(bubbleElemID,{ sync: true, from: 0, to: 1 }),
						new Effect.Move(bubbleElemID, { sync: true, y: 10, mode: 'relative'})
					  ], {
					  	duration: 0.5, afterFinish: function(){
	      	              beingShown = false;
	      	              shown = true;
	      	          	}
					  });
	      	      }
	          });
	
	
	
	      	  item.observe("mouseout", function(){
	      	      if (timer) clearTimeout(timer);
	      	      timer = setTimeout(function(){
	      	          timer = null;
					  new Effect.Parallel([
	      	          	new Effect.Opacity(bubbleElemID,{ sync: true, from: 1, to: 0}),
						new Effect.Move(bubbleElemID, { sync: true, y: -10, mode:'relative'})
					  ],{
					  	duration: 0.5,
						afterFinish: function(){
	      	                $(bubbleElemID).hide();
	      	                shown = false;
	      	             }
					  })
	      	      }, 500)
	      	        
	          });
	      });
	  }	
}

function setupHelpObservers()
{
  if ($('show_help') != null && ($F('show_help') == "" || $F('show_help') == "true"))
  	{
	  $$('.help_tip').each(function(item) {
	  	item.observe("mouseover", show_help);
		item.observe("mouseout", hide_help);
		item.insert({ bottom: ' (<span class="question">?</span>)'});
	  })
	  $$('span.question').each(function(item) {
	  	item.observe("mouseover", show_help);
		item.observe("mouseout", hide_help);
	  })
	}
	else
	{
	  $$('.help_tip').each(function(item) {
	  	item.stopObserving("mouseover", show_help);
		item.stopObserving("mouseout", hide_help);
	  })
	  $$('span.question').each(function(item) {
	  	item.stopObserving("mouseover", show_help);
		item.stopObserving("mouseout", hide_help);
	  })
	}
}

function show_help(event){
	if (event.element().hasClassName('question')) {
		var info = event.element().parentNode.next();
		var elem = event.element().parentNode;
	}
	else {
		var info = event.element().next();
		var elem = event.element();
	}
	info.setStyle({position: "absolute",
					backgroundColor: "#333333",//"#D9E2E6",
					color: "#ffffff",
					border: "solid 4px #222222",//#94ACB5",
					padding: "3px 10px 10px 10px",
					width: "160px",
					fontSize: "10px",
					left: (elem.cumulativeOffset().left + (elem.getWidth() * 0.2 )) + "px",
					top: (elem.cumulativeOffset().top + elem.getHeight() + 5) + "px"});
	info.setOpacity(0.95)
	info.show();
}

function hide_help(event){
	if (event.element().classNames().member('question')) {
		event.element().parentNode.next().hide();
	}
	else {
		info = event.element().next().hide();
	}
}

function show_with_fx(elementName, value, effectType)
{
    switch (effectType) 
    {
        case BLIND:
        {
            value ? new Effect.BlindDown(elementName, {duration: DURATION}) : new Effect.BlindUp(elementName, {duration: DURATION});
            break;
        }
        case FADE:
        {
            value ? new Effect.Appear(elementName, {duration: DURATION}) : new Effect.Fade(elementName, {duration: DURATION}) ;
            break;
        }
        case SHRINK:
        {
            value ? new Effect.Grow(elementName, {duration: DURATION}) : new Effect.Shrink(elementName, {duration: DURATION});
            break;
        }
        case SLIDE:
        {
            value ? new Effect.SlideDown(elementName, {duration: DURATION}) : new Effect.SlideUp(elementName, {duration: DURATION});
            break;
        }
    }
}

function html_encode(text) 
{
    var textnew = text.replace(/&/g,"&amp;");
    textnew = textnew.replace(/</g,"&lt;");
    textnew = textnew.replace(/>/g,"&gt;");
    textnew = textnew.replace(/\r\n/g,"<br>");
    textnew = textnew.replace(/\n/g,"<br>");
    textnew = textnew.replace(/\r/g,"<br>");
    return(textnew);
}

function encode_boolean(boolVal)
{
    return boolVal == true ? 1 : 0;
}

/*---------------------------
Registration page functions
----------------------------*/

function highlight(id)
{
    $$('.' + id).each(function(item){ item.addClassName('highlight') })
}

function clear_highlight(id)
{
    $$('.' + id).each(function(item){ item.removeClassName('highlight') })
}
/*----------------------------
Site specific: Event Tickets
----------------------------*/

function add_ticket_row()
{
    var index = $("ticket_rows").value;
    index++;
    $("ticket_rows").value = index;
    new Insertion.Bottom("ticket_table", '<tr id="ticket_row_' + index + '"><td><input id="ticket_name_' + index + '" name="ticket[][name]" type="text" size="10"></td><td><input id="ticket_no_available_' + index + '" name="ticket[][no_available]" type="text" size="5"></td><td><input id="ticket_price_' + index + '" name="ticket[][price]" type="text"  size="5"></td><td><a href="javascript:void(0)" onclick="del_ticket_row(' + index + ')">Remove</a><input type="hidden" id="ticket_id_' + index + '" name="ticket[][id]" value=""></td></tr>');
}

function del_ticket_row(index)
{
    new Element.remove("ticket_row_" + index);
}

/*----------------------------
Site specific: Event Listings
----------------------------*/

function show_actions(id)
{
	$('row_bottom_' + id).show();
	$('actions_ph').hide();
	$('row_bottom_' + id).addClassName('highlight');
	$('row_top_' + id).addClassName('highlight_noborder');
}

function hide_actions(id)
{
	$('row_bottom_' + id).hide();
	$('actions_ph').show();
	$('row_bottom_' + id).removeClassName('highlight');
	$('row_top_' + id).removeClassName('highlight_noborder');
}

function show_hint(id)
{
	$('note').hide();
	$(id).show();
}

function hide_hint(id)
{
	$('note').show();
	$(id).hide();
}

/*----------------------------
Site specific: Widget Preview
----------------------------*/

function update_colour() {
    // Get R G B values from slider textboxes;
    var decR = $('slider_red').value;
    var decG = $('slider_green').value;
    var decB = $('slider_blue').value;

    // converted to hex
    var hexR = parseInt(decR).toString(16);
    var hexG = parseInt(decG).toString(16); 
    var hexB = parseInt(decB).toString(16);

    // pad with 0 if needed
    hexR.length < 2 ? hexR = "0" + hexR : hexR;
    hexG.length < 2 ? hexG = "0" + hexG : hexG;
    hexB.length < 2 ? hexB = "0" + hexB : hexB;

    // Set text box value
    $('widget_colour').value = hexR + hexG + hexB;

    // Set div preview colour
    $('colour_preview').style.backgroundColor = '#' + hexR + hexG + hexB;
};
    
function text_update_colour(hex)
{
    var regex = /[a-fA-F0-9]{6}/;
    if (regex.test(hex) == true)
    {
        $('colour_preview').style.backgroundColor = '#' + hex;
        //converted hex to n
        red_slider.setValue(parseInt("0x" + hex.substring(0,2)));
        green_slider.setValue(parseInt("0x" + hex.substring(2,4)));
        blue_slider.setValue(parseInt("0x" + hex.substring(4,6)));
    }
};

function update_tool() {
    var tktit_col = $('widget_colour').value.toLowerCase() == "da001d" ? "" : "tktit_col = \"" + $('widget_colour').value.toLowerCase() + "\";";
    var tktit_txt = $('widget_title').value.toLowerCase() == "events" ? "" : "tktit_txt = \"" + $('widget_title').value.replace(/"/gi,"\\\"") + "\";";
    var tktit_loc = $('widget_show_location').checked ? "" : "tktit_loc = 0;";
    var tktit_date = $('widget_show_date').checked ? "tktit_date = 1;" : "";
    var tktit_max = $('widget_max_events').value == 5 ? "" : "tktit_max = " + $('widget_max_events').value + ";";
    $('code_no_js').innerHTML = "http://www.stubmatic.com/events/widget/" + $('uid').value + "?&t=" + escape($('widget_title').value) + "&c=" + $('widget_colour').value + "&l=" + encode_boolean($('widget_show_location').checked) + "&d=" + encode_boolean($('widget_show_date').checked) + "&m=" + $('widget_max_events').value;
	$('preview_frame').src = "/events/preview_widget/" + $('uid').value + "?&t=" + escape($('widget_title').value) + "&c=" + $('widget_colour').value + "&l=" + encode_boolean($('widget_show_location').checked) + "&d=" + encode_boolean($('widget_show_date').checked) + "&m=" + $('widget_max_events').value;
    $('code').innerHTML = html_encode("<!-- Start Stubmatic.com Widget  -->\n<script type=\"text/javascript\">tktit_id = " + $('uid').value + ";" + tktit_txt + tktit_col + tktit_loc + tktit_date + tktit_max + "</script>\n<script src=\"http://www.stubmatic.com/javascripts/widget.js\" type=\"text/javascript\"></script>\n<!-- End Stubmatic.com Widget  -->");
	new Effect.Highlight('code_no_js')
	new Effect.Highlight('code')
};

function reset_widget() {
    $('widget_options').reset();
    red_slider.setValue(218);
    green_slider.setValue(0);
    blue_slider.setValue(29);
    $('colour_preview').style.backgroundColor = '#da001d'
    update_tool();
}

/*----------------------------
Site specific: Flash Widget Preview
----------------------------*/
    

function update_flw() {
    var tktit_col = $('widget_colour').value.toLowerCase() == "444444" ? "" : "tktit_col = \"" + $('widget_colour').value.toLowerCase() + "\";";
    var tktit_txt = $('widget_title').value.toLowerCase() == "" ? "" : "tktit_txt = \"" + $('widget_title').value.replace(/"/gi,"\\\"") + "\";";
    var tktit_max = $('widget_max_events').value == 5 ? "" : "tktit_max = " + $('widget_max_events').value + ";";
	var c = $('widget_colour').value.toLowerCase() == "444444" ? "" : "&widget_colour=" + $('widget_colour').value.toLowerCase();
	var tx = $('widget_title').value.toLowerCase() == "" ? "" : "&widget_title=" + html_encode($('widget_title').value.replace(/"/gi,"\\\""));
	var m = $('widget_max_events').value == 5 ? "" : "&widget_no=" + $('widget_max_events').value;
    $('code_no_js').innerHTML = html_encode("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' id='widget' width='249' height='227' codebase='http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab'><param name='movie' value='http://tktit.com/flash/widget.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#444444' /><param name='allowScriptAccess' value='sameDomain' /><param name='flashVar' value=\"widget_id=" + $('uid').value + c + tx + m + "\" /><embed src='http://tktit.com/flash/widget.swf' quality='high' bgcolor='#444444' width='249' height='227' name='Test' align='middle' play='true' loop='false' quality='high' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' flashVars=\"widget_id=" + $('uid').value + c + tx + m + "\" pluginspage='http://www.adobe.com/go/getflashplayer'></embed></object>");
    $('preview').innerHTML = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' id='widget' width='249' height='227' codebase='http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab'><param name='movie' value='/flash/widget.swf' /><param name='quality' value='high' /><param name='bgcolor' value='#444444' /><param name='allowScriptAccess' value='sameDomain' /><param name='flashVar' value=\"widget_id=" + $('uid').value + c + tx + m + "\" /><embed src='/flash/widget.swf' quality='high' bgcolor='#444444' width='249' height='227' name='Test' align='middle' play='true' loop='false' quality='high' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' flashVars=\"widget_id=" + $('uid').value + c + tx + m + "\" pluginspage='http://www.adobe.com/go/getflashplayer'></embed></object>";
	$('code').innerHTML = html_encode("<!-- Start Stubmatic.com Widget  -->\n<script type=\"text/javascript\">tktit_id = " + $('uid').value + ";" + tktit_txt + tktit_col + tktit_max + "</script>\n<script src=\"http://tktit.com/javascripts/widget_fl.js\" type=\"text/javascript\"></script>\n<!-- End Stubmatic.com Widget  -->");
	new Effect.Highlight('code_no_js')
	new Effect.Highlight('code')
};

function reset_flw() {
    $('widget_options').reset();
    red_slider.setValue(68);
    green_slider.setValue(68);
    blue_slider.setValue(68);
    $('colour_preview').style.backgroundColor = '#444444'
    update_flw();
}
