/* Ableton Denver Javascript ::: http://datamafia.com */

// set the left column to default state
function left_col_default(){
	// if this class is used slideUp the parent and slideDown the parents previous sibling
	$(".left_column_close").filter(function(){
		$(this).parent().slideUp(300);
		$(this).parent().prev().slideDown(200);
	})
}
// open the target left column item first returning all other items to default state
function left_col_open(a){ // a=new 
	left_col_default(); // return to the default state
	$(a).slideUp(function(){ // callback
		$(a).next().slideDown(200)
	})
}

// DOC RED
$(document).ready(function(){
	$(".left_column_close").click(function(event){
		left_col_default();
	})
	$("#left_login").click(function(event){
		left_col_open(this);
	})
	$("#left_search").click(function(event){
		left_col_open(this);
	})
	$("#left_links").click(function(event){
		left_col_open(this);
	})
	$(".click_to_play").fadeTo(1, 0.7); // init val hack
	$(".click_to_play").hover( // for the player init hover
		function () {
			$(this).fadeTo(150, 1);
		}, 
		function () {
			$(this).fadeTo(150, 0.7);
		}
	);
	$("#call_player").click( // for the player init hover
		function () {
			$("#inline_set_browser").animate( { height:"300px"}, 1000 );
			ajaxCallGet('popout=inline', '/player/', 'inline_set_browser');
			}
		);
	$("#call_popout_left").click( // for the player init hover
		function () {
			$("#inline_set_browser").animate( { height:"0px"}, 1000 );
			$("#inline_set_browser").text('');
			}
		);
	$("#popout_player").click( // for the player init hover
		function () {
			$().animate( { height:"0px"}, 1000 );
			$("#inline_set_browser").text('');
			}
		);
});

// close the inline player function (DOM solution)
function close_inline_player() {
	$("#inline_set_browser").animate( { height:"0px"}, 1000 );
	$("#inline_set_browser").text('');
	}

/* Validate the contact form input */
function validate_required(field,alerttxt) {
	with (field){
		if (value==null||value=="") {
			alert(alerttxt);
			return false;
		}else{
			return true;
		}
	}
}
// more form validation...
function validate_form(thisform){
	with (thisform){
		if (validate_required(email,"Email must be filled out!")==false)
			{email.focus();return false;}
		if (validate_required(name,"Name must be filled out!")==false)
			{name.focus();return false;}
	}
	return ''; 
}
// yeah, I should have written this more generic...
function file_upload(thisform){
	with (thisform){
		if (validate_required(description,"Description must be filled out!")==false)
			{description.focus();return false;}
		/*if (validate_required(terms,"Terms must be accepted!")==false)
			{terms.focus();return false;}*/
		if (terms.checked != 1){
			alert('You must accept the terms to upload a file.');
			return false;
		}
	}
	return '';
}

// Same but different - checks for the desc on the file edit
function file_edit(thisform){
	with (thisform){
		if (validate_required(description,"Description must be filled out!")==false)
			{description.focus();return false;}
	}
	return '';
}
// Functionc for CL viewing. Note the next().next() - it works and should be stable for a while...
function toggle_next_cl(siht){
	$(siht).next().toggle();
	$(siht).hide();
	$(siht).prev().show();
} // next function reverses the previous' work
function toggle_prev_cl(siht){
	$(siht).next().toggle();
	$(siht).next().next().toggle();
	$(siht).hide();
}
// See verbose naming stragegy
function displayNext(siht){ // used for confirm delete button
	$(siht).next().show();
	$(siht).hide();
}

function serialAjaxCallGet(s, t, i){ 
	var d = $("#"+s).serialize(); // serialize form id
	$.ajax({ 
		asynch: true,
		type: "GET", 
		url: t, 
		data: d,
		dataType: "text",
		success: function(data){
			$("#"+i).html(data)
		}
	});
}
	
// this is used in pagination of the returned search results
function ajaxCallGet(s, p, i){ 
	$.ajax({ 
		asynch: true,
		type: "GET", 
		url: p, 
		data: s,
		dataType: "text",
		success: function(data){
			$("#"+i).html(data)
		}
	});
}
// generate player as popup window
function generaic_popup(mylink, windowname, width, height){
    if (! window.focus)return true;
    var href;
    if (typeof(mylink) == 'string')
        href=mylink;
    else
        href=mylink.href;
        window.open(href, windowname, 'width='+width+',height='+height+',scrollbars=yes');
    return false;
}	
	
// generate player as popup window
function player_popup(mylink, windowname){
    if (! window.focus)return true;
    var href;
    if (typeof(mylink) == 'string')
        href=mylink;
    else
        href=mylink.href;
        window.open(href, windowname, 'width=510,height=400,scrollbars=yes');
    return false;
}
// check to see if checkboc is checked and chuck wood.
function isChecked(ckbox_name, fail_msg) {
		var ckbx = document.getElementsByName(ckbox_name);
		if (ckbx[0].checked==true) {
			return true;
		}else{
			alert(fail_msg);
			return false;
		}
}