function _resizeCMS()
{
	h = $('#amp_studio').height();
	window.parent.resizeCMS(h);
}

function checkBox(chbx_id)
{
	$(function()
	{
		$("#" + chbx_id ).each(function(i){ 
		
			this.checked=true;

		});

	});			
}	

function tieCheckBoxesToAsyncCall(chk_class,call,multiple,type)
{
	
	$("."+ chk_class).click(function () {
	        
	       	flag = $(this).attr('checked') ? '1' : 0;
	      	action =  $(this).attr('value');
	      	
	      	if (multiple != true)
	      	{
		      	//clear all checkboxes
		      	$("."+ chk_class).attr('checked',false);
		      	
		      	//set my flag
		      	$(this).attr('checked',flag);
	      	}

		$.post(call  + action + '/' + flag + '/' + type,'',
		
		 function(data){ });
		
	})	
}	


function showSongList( compilationId ) {

	  var comp = '#songlist-' + compilationId;
	  $( comp ).show();
	  
	  
	  var showbutton = '#show-songs-' + compilationId;
	  var hidebutton = '#hide-songs-' + compilationId;
	  
	  //show 'hide', hide 'show'
	  $( showbutton ).hide();
	  $( hidebutton ).show();
	  
	 _resizeCMS();
}

function hideSongList( compilationId ) {

	  var comp = '#songlist-' + compilationId;
	  $( comp ).hide();

	  var showbutton = '#show-songs-' + compilationId;
	  var hidebutton = '#hide-songs-' + compilationId;

	  //show 'show', hide 'hide'
	  $( hidebutton ).hide();
	  $( showbutton ).show();
	  
	  _resizeCMS();
}



/* */

function doPost()
{
	
	var alerts = new Array();
	
	$(function()
	{
		//file type check
		var data_errors = false;
		var file_upload = false;
		
		$("input[@type=file]").each(function() 
		{			
			var filename = $(this).attr("value");
			var mediatype =  $(this).attr("mediatype");
			
			if (filename && mediatype)
			{
				file_upload = true;
				
				var ext = filename.split('.').slice(-1); 
				
				filename = filename.split('\\').slice(-1); 
				
				ext = String(ext);
				
				ext = ext.toLowerCase();
				
				switch (mediatype)
				{
					case "audio":
					
						if (ext != 'mp3')
						{
							alerts.push(filename + ' is not a supported media file. The player accepts MP3 audio only.');
							data_errors = true;
						}	
						
					break;
					
					case "picture":
					
						if (ext != 'jpg' && ext != 'jpeg' )
						{
							alerts.push(filename + '\n is not a JPG file, please resave.');
							data_errors = true;
						}	
						
					break;			
	
					case "video":
					
						if (
							ext != 'mov' && 
							ext != 'avi' && 
							ext != 'mpg' && 
							ext != 'mp4' && 
							ext != 'm4v' && 
							ext != 'mpeg' && 
							ext != 'wmv' && 
							ext != 'flv' && 
							ext != 'wmv' && 
							ext != '3g2' && 
							ext != '3gp' && 
							ext != '3gp2' && 
							ext != '3gpp' 
						 )
						{
							alerts.push(filename + '\n is not a supported video file, please resave.');
							data_errors = true;
						}
						else
						{						
							$('#fe_upload_screen .upload_screen_note').show();	
						}	
						
					break;			
	
					case "allmedia":
					
						if (
							ext != 'jpg' && 
							ext != 'jpeg' &&
							ext != 'mp3' &&  
							ext != 'mp4' && 
							ext != 'mov' && 
							ext != 'avi' && 
							ext != 'mpg' && 
							ext != 'mpeg' && 
							ext != 'wmv' && 
							ext != 'flv' && 
							ext != 'wmv' && 
							ext != '3g2' && 
							ext != '3gp' && 
							ext != '3gp2' && 
							ext != '3gpp' 
						 )
						{
							alerts.push(filename + '\n is not a supported media file, please resave.');
							data_errors = true;
						}
						else
						{						
							$('#fe_upload_screen .upload_screen_note').show();	
						}	
						
					break;	
					
				}
			}	

		}
		);	

		//required files
		$("input[@type=text]").each(function() 
		{			
			var required =  $(this).attr("required");
			var requiredMsg = $(this).attr( "txtmsg" );
			var strValidChars =  $(this).attr( "valid_chars" );
			var strString=$(this).val();

			if ( required == 'true' && strString == '' )
			{
				if( requiredMsg == '' ) {
					requiredMsg = 'Please insert title';
				}

				alerts.push( requiredMsg );
				data_errors = true;				
			}	
			else if ( required == 'true' && strValidChars != undefined )
			{
				//chars validation
				
				var blnResult = true;
				
				for (i = 0; i < strString.length && blnResult == true; i++)
				{
					strChar = strString.charAt(i);
					if (strValidChars.indexOf(strChar) == -1)
					{
						blnResult = false;
					}
				  }

				if(blnResult == false) {
					alerts.push( requiredMsg + " \n" + strString + " is not a valid value");
					data_errors = true;	
				}		
			}	
			
		}
		);	

		//required files
		$("input[@type=file]").each(function() 
		{			
			var required =  $(this).attr("required");
			var requiredMsg = $(this).attr( "ipmsg" );

			if ( required == 'true' && $(this).val() == '' )
			{
				if( requiredMsg == '' ) {
					requiredMsg = 'Please upload file';
				}

				alerts.push( requiredMsg );
				data_errors = true;				
			}	

		}
		);	

		//required checkboxes
		$("input[@type=checkbox]").each(function() 
		{			
			var required =  $(this).attr("required");
			var requiredMsg = $(this).attr( "cbmsg" );
			if ( required == 'true' && !$(this).is(':checked')  )
			{
				if( requiredMsg == '' ) {
					requiredMsg = 'Please check checkbox';
				}

				alerts.push( requiredMsg );
				data_errors = true;				
			}	

		}
		);	

				
		//no errors? let's submit
		if (!data_errors)
		{

			if (file_upload)
			{
				var t='Upload in progress.... ';
				var a='#TB_inline?height=170&width=320&modal=true&inlineId=amp_upload_screen';
				var g='';
			
				window.top.tb_show(t,a,g);
			}	
		
			window.document.forms[0].submit();
			return false;
			
		}
		else
		{
			alert_msg = alerts.join('\n');
			alert(alert_msg);	
		}		
		
	});

}	



function doPostAndReboot()
{
	$(function()
	{
		$("#reboot").val('1');
	});
	
	doPost();
}	

function checkFormats() {

	var checkedCt = $("#formatList :checked").size();

	if ( checkedCt > 0 ) {
		doPost();
	}
	else {
		alert("Please select at least one format");
	}
}

function closeMe()
{
	window.top.tb_remove();	
}	



/* utilities */

//add thickbox to href & area elements that have a class of .thickbox
function tie_thickbox(domChunk){
	
	$(domChunk).click(function(){
	var t = this.title || this.name || null;
	var a = this.href || this.alt;
	var g = this.rel || false;
	
	window.top.tb_show(t,a,g);
	this.blur();
	return false;
	});
}

function limitChars(textid, limit, infodiv)
{
		var text = $('#'+textid).val();
		var textlength = text.length;
		if(textlength > limit)
		{
			$('#' + infodiv).html('Text must be '+limit+' characters or less.');
			$('#'+textid).val(text.substr(0,limit));
			return false;
		}
		else
		{
			$('#' + infodiv).html('You have '+ (limit - textlength) +' characters left.');
			return true;
		}
}

function jumpToAMP()
{
	$('html, body').animate({scrollTop:0}, function() {});
}

function focusCMS()
{
	$('html, body').animate({scrollTop:650}, function() {});
}


function str_replace(search, replace, subject, count) {
    // http://kevin.vanzonneveld.net
 
    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }
 
    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
}
