var mediaUploader = {
	noImg : '',
	yesImg : '',
	wpspinImg : '',
	tag:'',
	type:'',
	OptionUploaderFromForm : function(id,title,type,target){
		var win = window.dialogArguments || opener || parent || top;
		win.theme.option.uploader.getMedia(id,title,type,'form',target);
		win.tb_remove();
	},
	OptionUploaderFromUrl : function(target){
		var t = this;
		var f = document.forms[0];
		if ( '' == f.href.value || ''==t.type)
			return false;
		if ( '' == f.title.value)
			return false;
		src =  f.href.value;
		title = f.title.value;
		type = t.type;
		var win = window.dialogArguments || opener || parent || top;
		win.theme.option.uploader.getMedia(src,title,type,'url',target);
		win.tb_remove();
		return false;
	},
	themeGetMediaData :function (noImg,yesImg,wpspinImg){
		var t = mediaUploader;
		t.noImg = noImg;
		t.yesImg = yesImg;
		t.wpspinImg = wpspinImg;
		type = document.forms[0].mediaType.value;
		src = document.forms[0].href.value;
		if ( ! src ) {
			t.themeResetMediaData();
			return false;
		}
		document.getElementById('status_img').innerHTML = '<img src="'+t.wpspinImg+'" alt="" />';
		t.preloadImg = new Image();
		t.preloadImg.onload = t.themeUploadMediaData;
		t.preloadImg.onerror = t.themeResetImageData;
		t.preloadImg.src = src;
	},
	themeResetMediaData : function (){
		var t = mediaUploader;
		t.type = '';
		document.getElementById('go_button').style.color = '#bbb';
		if ( ! document.forms[0].href.value )
			document.getElementById('status_img').innerHTML = '*';
		else document.getElementById('status_img').innerHTML = '<img src="'+t.noImg+'" alt="" />';
	},
	themeResetImageData :function (){
		var t = mediaUploader;
		src = document.forms[0].href.value;
		var res=src.split('.');
		/*if(res[res.length-1]=='swf'){*/
		if(true){
			t.type = 'html5';
			document.getElementById('go_button').style.color = '#333';
			document.getElementById('status_img').innerHTML = '<img src="'+t.yesImg+'" alt="" />';
		}
		else{
		
			t.themeResetMediaData();
		}
	},
	themeUploadMediaData :function (){
		var t = mediaUploader;
		t.type = 'image';
		document.getElementById('go_button').style.color = '#333';
		document.getElementById('status_img').innerHTML = '<img src="'+t.yesImg+'" alt="" />';
	}
}
function setLoad(type){
	wpspinImg = jQuery('#loadImage').val();
	jQuery('#status_'+type).html('<img src="'+wpspinImg+'" alt="" />');
}
function setWrong(type){
	noImg = jQuery('#noImage').val();
	jQuery('#status_'+type).html('<img src="'+noImg+'" alt="" />');
	jQuery('#status_'+type).closest('tr').data('status','wrong');
	jQuery('#status_'+type).trigger('status_wrong');
}
function setEmpty(type){
	jQuery('#status_'+type).html('');
	jQuery('#status_'+type).closest('tr').data('status','wrong');
	jQuery('#status_'+type).trigger('status_wrong');
}
function setRight(type){
	yesImg = jQuery('#yesImage').val();
	jQuery('#status_'+type).html('<img src="'+yesImg+'" alt="" />');
	jQuery('#status_'+type).closest('tr').data('status','right');
	jQuery('#status_'+type).trigger('status_right');
}
jQuery(document).ready( function($) {
	if(location.search.indexOf('option_image_upload') != -1){
		jQuery('#media-upload #filter').append('<input type="hidden" value="1" name="option_image_upload">');
		jQuery('#media-upload #gallery-settings').remove();
	}
	if(jQuery('.symplex-media-type').length !=0){
		var selected = jQuery('.symplex-media-value:checked').val();
		jQuery('table.describe tr').hide();
		jQuery('table.describe').find('.'+selected).show();
		jQuery('table.describe').find('.all').show();
		jQuery('#mediaType').val(selected);
		jQuery('.symplex-media-value').change(function(){
			var selected = jQuery('.symplex-media-value:checked').val();
			jQuery('table.describe tr').hide();
			jQuery('table.describe').find('.'+selected).show();
			jQuery('table.describe').find('.all').show();
			jQuery('#mediaType').val(selected);
		});
	}
	jQuery('input.mediaText').bind('blur',function(e){
		$el = jQuery(e.target);		
		type=$el.attr('id');
		switch(type){
		case 'title':
			setLoad('title');
			if($el.val().length != 0)
				setRight('title');
			else
				setWrong('title');
			break;
		case 'image':
			setLoad('img');
			if($el.val().length == 0){
				setWrong('img');
				break;
			}
			preloadImg = new Image();
			preloadImg.onload = function(){
				setRight('img');
			};
			preloadImg.onerror = function(){
				setWrong('img');
			};
			preloadImg.src = $el.val();
			break;
		case 'ourl':
			setLoad('ogg');
			var str = $el.val();
			if($el.val().length != 0)
				setRight('ogg');
			else
				setEmpty('ogg');
			break;
		case 'wurl':
			setLoad('webm');
			if($el.val().length != 0)
				setRight('webm');
			else
				setEmpty('webm');
			break;
		case 'murl':
			setLoad('mp4');
			if($el.val().length != 0)
				setRight('mp4');
			else
				setEmpty('mp4');
			break;
		case 'vurl':
			setLoad('wmv');
			if($el.val().length != 0)
				setRight('wmv');
			else
				setEmpty('wmv');
			break;
		case 'furl':
			setLoad('flv');
			if($el.val().length != 0)
				setRight('flv');
			else
				setEmpty('flv');
			break;
		case 'flash':
			setLoad('swf');
			if($el.val().length != 0)
				setRight('swf');
			else
				setWrong('swf');
		
			break;
		case 'videoId':
			setLoad('videoid');
			if($el.val().length != 0)
				setRight('videoid');
			else
				setWrong('videoid');
			break;
		}	
	});
	jQuery('.describe tr').bind('status_right',function(e){
		var selected = jQuery('.symplex-media-value:checked').val();
		if(selected =='video'){
			var tmp = 0;
			var vtype = 0;
			jQuery('.describe tr:visible').each(function(){
				var stat = jQuery(this).data('status');	
				if(typeof stat !='undefined' && stat =='right'){
					tmp++;
					var type = jQuery(this).find('input').attr('id');
					if(type == 'ourl' || type == 'wurl' || type == 'murl'|| type == 'vurl' ||type == 'furl')
						vtype++;
				}
			});
			if(tmp > 1  && vtype>0 &&tmp!=vtype){
				jQuery('#go_button').addClass('aviable');
			}
		}else{
			var tmp = 0;
			jQuery('.describe tr:visible').each(function(){
				var stat = jQuery(this).data('status');	
				if(typeof stat !='undefined' && stat =='right')
					tmp++;
			});
			if(tmp == (jQuery('.describe tr:visible').length - 1) && ! jQuery('#go_button').hasClass('aviable')){
				jQuery('#go_button').addClass('aviable');
			}
		}
	});
	jQuery('.describe tr').bind('status_wrong',function(e){
		var selected = jQuery('.symplex-media-value:checked').val();
		if(selected =='video'){
			var tmp = 0;
			var vtype = 0;
			jQuery('.describe tr:visible').each(function(){
				var stat = jQuery(this).data('status');	
				if(typeof stat !='undefined' && stat =='right'){
					tmp++;
					var type = jQuery(this).find('input').attr('id');
					if(type == 'ourl' || type == 'wurl' || type == 'murl' || type == 'vurl' ||type == 'furl')
						vtype++;
				}
			});
			if(tmp < 2  || vtype<1 || tmp==vtype){
				jQuery('#go_button').removeClass('aviable');
			}
		}else{
			jQuery('#go_button').removeClass('aviable');
		}
	}); 
	jQuery('#go_button.aviable').live('click',function(e){
		if(jQuery('.symplex-media-type').length ==0)
			type = 'image';
		else
			type = jQuery('.symplex-media-value:checked').val();
		var title = jQuery('#title').val();
		var src = '';
		switch(type){
		case 'image':
			src = jQuery('#image').val();
			break;
		case 'youtube':
			src = jQuery('#videoId').val();
			break;
		case 'vimeo':
			src = jQuery('#videoId').val();
			break;
		case 'flash':
			src = jQuery('#flash').val();
			break;
		case 'video':
			var src = {};
			if(jQuery('#wurl').val()!='')
				src['wurl']=jQuery('#wurl').val();
			if(jQuery('#ourl').val()!='')
				src['ourl']=jQuery('#ourl').val();
			if(jQuery('#murl').val()!='')
				src['murl']=jQuery('#murl').val();
			if(jQuery('#furl').val()!='')
				src['furl']=jQuery('#furl').val();
			if(jQuery('#vurl').val()!='')
				src['vurl']=jQuery('#vurl').val();
			src = jQuery.param(src);
			break;
			
		}
		var target = jQuery('#target').val();
		var win = window.dialogArguments || opener || parent || top;
		win.theme.option.uploader.getMedia(src,title,type,'url',target);
		win.tb_remove();
	});
});


function optionImageUploadSuccess(fileObj, serverData) {
	// if async-upload returned an error message, place it in the media item div and return
	if ( serverData.match('media-upload-error') ) {
		jQuery('#media-item-' + fileObj.id).html(serverData);
		return;
	}

	optionImagePrepareMediaItem(fileObj, serverData);
	updateMediaForm();

	// Increment the counter.
	if ( jQuery('#media-item-' + fileObj.id).hasClass('child-of-' + post_id) )
		jQuery('#attachments-count').text(1 * jQuery('#attachments-count').text() + 1);
}

function optionImagePrepareMediaItem(fileObj, serverData) {
	var f = ( typeof shortform == 'undefined' ) ? 1 : 2, item = jQuery('#media-item-' + fileObj.id);
	// Move the progress bar to 100%
	jQuery('.bar', item).remove();
	jQuery('.progress', item).hide();

	try {
		if ( typeof topWin.tb_remove != 'undefined' )
			topWin.jQuery('#TB_overlay').click(topWin.tb_remove);
	} catch(e){}

	// Old style: Append the HTML returned by the server -- thumbnail and form inputs
	if ( isNaN(serverData) || !serverData ) {
		item.append(serverData);
		prepareMediaItemInit(fileObj);
	}
	// New style: server data is just the attachment ID, fetch the thumbnail and form html from the server
	else {
		if(typeof swfu.settings.post_params.target !== 'undefined'){
			item.load('async-upload.php', {attachment_id:serverData, fetch:f,option_image_upload:1,target:swfu.settings.post_params.target}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
		}else{
			item.load('async-upload.php', {attachment_id:serverData, fetch:f,option_image_upload:1}, function(){prepareMediaItemInit(fileObj);updateMediaForm()});
		}
	}
}
