// Include some standard stuff that gets used site-wide...

var base = '/system/mtvan-3.0/';

$(function() {

	$('.rollover').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	}, function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
		});

	$('.q1-12-button-grey, .q1-12-button-green').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	}).mousedown(function(){
		$(this).addClass('down');
	}).mouseup(function(){
		$(this).removeClass('down');
	});
});

function show_signin(){
	jQuery("#sign_in_box").fadeIn("fast");
}

function hide_signin(){
	jQuery("#sign_in_box").fadeOut("fast");
}

function update_status(){

	if (document.getElementById('status_update').value){
	
		clearTimeout(news_feed_timeout);
		jQuery("#status_update_mask").fadeIn("fast");
		jQuery("#status_update_loader").fadeIn("fast");
		var query_string = 'status='+encodeURIComponent(document.getElementById('status_update').value);
		
		ajax_request('/system/mtvan-3.0/ajax/update_status.php', query_string, 'update_status_callback');
	}else{
		// nothing, alert maybe?
	}
}

function update_status_callback(response)
	{
	document.getElementById('status_update').value = '';
	jQuery("#status_update_mask").fadeOut("slow");
	jQuery("#status_update_loader").fadeOut("slow");
	
	news_update_method = 'update_status';
	load_news();
	}

function preload(image_array)
	{
    $(image_array).each(function()
    	{
        $('<img/>')[0].src = this;
        });
	}

// preload the images that are used all over the place
preload([
	'/system/mtvan-3.0/images/small_logo_hover.gif',
	'/system/mtvan-3.0/images/social/facebook_hover.png',
	'/system/mtvan-3.0/images/social/twitter_hover.png',
	'/system/mtvan-3.0/images/social/linkedin_hover.png',
	'/system/mtvan-3.0/images/social/digg_hover.png',
	'/system/mtvan-3.0/images/social/delicious_hover.png',
	'/system/mtvan-3.0/images/tick.gif',
	'/system/mtvan-3.0/images/cross.gif'
	]);


// fix checkboxes for IE
$(function() {
	if ($.browser.msie) {
		$('input:checkbox').click(function () {
			this.blur();
			this.focus();
			});
		}
	});


// Update Feedback
function update_feedback(success, success_message, fail_message)
	{
	if (success == 'Y')
		{
		$("#feedback_message").delay(500).fadeOut("fast", function(){
			$("#feedback_message").html('<img id="work_loader" src="/system/mtvan-3.0/images/tick.gif" class="icon"> '+success_message);
			$("#feedback_message").fadeIn("fast", function(){
				$("#mask").delay(1100).fadeOut("slow");
				$("#mask_box").delay(1100).fadeOut("slow");
				});
			});
		}
	else if (success == 'N')
		{
		$("#feedback_message").delay(500).fadeOut("fast", function(){
			$("#feedback_message").html('<img id="work_loader" src="/system/mtvan-3.0/images/cross.gif" class="icon"> '+fail_message);
			$("#feedback_message").fadeIn("fast", function(){
				$("#mask").delay(1100).fadeOut("slow");
				$("#mask_box").delay(1100).fadeOut("slow");
				});
			});
		}
	}

$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
  while(x<c.length){var m=r.exec(c.substr(x));
    if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
    }else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
    o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
  while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
  t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});

// Old AJAX function - use jQuery now, included for some old functions that use it.
function ajax_request(url,data,result_function){var http=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	// JScript gives us Conditional compilation, we can cope with old IE versions.
	// and security blocked creation of the objects.
	try {
		http = new ActiveXObject("Msxml2.XMLHTTP");
		}
	catch (e) {
		try {
			http = new ActiveXObject("Microsoft.XMLHTTP");
			}
		catch (E) {
			http = false;
		}}
	@end @*/
if(!http&&typeof XMLHttpRequest!="undefined"){try{http=new XMLHttpRequest()}catch(e){http=false}}if(!http&&window.createRequest){try{http=window.createRequest()}catch(e){http=false}}http.open("POST",url,true);http.setRequestHeader("Content-type","application/x-www-form-urlencoded");http.setRequestHeader("Content-length",data.length);http.setRequestHeader("Connection","close");http.onreadystatechange=function(){if(http.readyState==4&&http.status==200){var response=eval("("+http.responseText+")");window[result_function](response)}};http.send(data)};


