/* myAjax actions */

function replaceYoutubePreview( hash, div ) {
    get( div ).innerHTML = '<img src="/graph/loading.gif" alt="Trwa ładowanie video"/>';
    var ajax = new myAjax();
    ajax.action = 'replaceYoutubePreview';
    ajax.post( 'hash='+hash, div );
}

function swapHashCode() {
	var h		= get( 'hashcode' );
	h.value = 'foto';
}

function try_sendPhotoZlot() {
	swapHashCode();
	var ph 	= get( 'photo' );
	if( ph.value.match( '(.jpg|.jpeg|.JPG|.JPEG)$' ) ) {
		hide( 'div_send_photo' );
		document.forms[ 'sendPhoto' ].submit();
	} else {
		alert( 'Wybierz zdjęcie formatu JPG' );
	}
}


function try_sendPhoto() {
	swapHashCode();
	var ph 	= get( 'photo' );
	var file 	= get( 'photo' );
	var file_path = get( 'file_path' );
	if( ph.value.match( '(.jpg|.jpeg|.JPG|.JPEG)$' ) ) {
		file_path.value = file.value;
		hide( 'div_send_photo' );
		document.forms[ 'sendPhoto' ].submit();
	} else {
		alert( 'Wybierz zdjęcie formatu JPG' );
	}
}

function try_contact() {
	var h		= get( 'hashcode' );	
	var e 	= getValue( 'email' );
	var l		= getValue( 'login' );
	var t		= getValue( 'title' );
	var b 	= getValue( 'body' );
	if( ( e == '' ) || ( b == '' ) ) {
		alert( 'Wprowadź swój adres e-mail i wpisz wiadomość' );
	} else {
		h.value = 'foto';
		var ajax	= new myAjax();
		hide( 'div_contact' );
		ajax.action = 'try_contact';
		ajax.post( 'email=' + e + '&body=' + b + '&login=' + l + '&title=' + t + '&hashcode='+h.value, 'contact_message' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				show( 'contact_message_send' );
			} else {
				show( 'div_contact' );
			}
		}
	}
}

function addThread( group ) {
	var t = getValue( 'title' );
	var b = getValue( 'body' );
	if( ( t == '' ) || ( b == '' ) ) {
		alert( 'Musisz wpisać tytuł i treść' );
	} else {
		var ajax = new myAjax();
		ajax.action = 'addThread';
		ajax.post( 'group=' + group + '&title=' + t + '&body=' + b );
		hide( 'addThreadForm' );
		hide( 'addThreadLink' );		
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				document.location.reload();
			} else {
				show( 'addThreadForm' );
				show( 'addThreadLink' );		
				alert( this.response );
			}
		}
	} 
}

function addPost( id, group ) {
	var b = getValue( 'body' );
	if( b == '' ) {
		alert( 'Musisz wpisać treść' );
	} else {
		var ajax = new myAjax();
		ajax.action = 'addPost';
		ajax.post( 'thread=' + id + '&group=' + group + '&body=' + b );
		hide( 'addPostForm' );
		hide( 'addPostLink' );		
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				document.location.reload();
			} else {
				show( 'addPostForm' );
				show( 'addPostLink' );		
				alert( this.response );
			}
		}
	} 

}

function try_remind() {
	var e 	= get( 'email' );
	e.value = trim( e.value );	
	if( e.value == '' ) {
		alert( 'Wprowadź swój adres e-mail' );
	} else {
		var ajax	= new myAjax();
		hide( 'div_reminder' );
		ajax.action = 'try_remind';
		ajax.post( 'email=' + e.value, 'reminder_message' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				document.location.href = "/Haslo_zostalo_wyslane";
			} else {
				show( 'div_reminder' );
			}
		}
	}
}

function try_register() {
	var lq 		= get( 'login' );
	var p 	= get( 'password' );
	var pr 	= get( 'password_repeat' );
	var n 	= get( 'nick' );
	var e 	= get( 'email' );
	var h		= get( 'hashcode' );
	lq.value = trim( lq.value );
	p.value = trim( p.value );
	pr.value = trim( pr.value );
	n.value = trim( n.value );
	e.value = trim( e.value );	
	if( ( lq.value == '' ) || ( p.value == '' ) || ( pr.value == '' ) || ( e.value == '' ) || ( n.value == '' ) ) {
		alert( 'Musisz wypełnić wszystkie pola oznaczone gwiazdką' );
	} else if ( p.value != pr.value ) {
		alert( 'Hasło nie zgadza się z powtórzeniem hasła' );
	} else if( p.value == lq.value ) {
		alert( 'Hasło nie może być takie same jak login' );
	} else {
		var ajax	= new myAjax();
		hide( 'div_register' );
		ajax.action = 'try_register';
		ajax.post( 'nick=' + n.value + '&email=' + e.value + '&password='+p.value, 'reg_message' );
		ajax.onLoad = function() {
			if( this.response == 'ok' ) {
				h.value = 'foto';				
				document.forms[ 'register' ].submit();
			} else {
				show( 'div_register' );
			}
		}
	}
}

function actp( id ) {
	var ajax = new myAjax();
	ajax.action = 'add_catalogue_to_promoted';
	ajax.post( 'id=' + id );
	ajax.onLoad = function() {
		alert( this.response );
	}
}


function pfc( id, author ) {
	var ajax = new myAjax();
	hide( 'link_catalogue_' + id );
	ajax.action = 'photos_from_catalogue';
	ajax.post( 'id=' + id + '&author=' + author, 'cat_' + id.toString() );
}

function moderate( id, obj, act ) {
	if( confirm( 'Jesteś pewien ?' ) ) {
		var ajax = new myAjax();
		ajax.action = act;
		ajax.post( 'id=' + id + '&obj=' + obj );
		ajax.onLoad = function() {
			if( act == 'remove' ) {
				alert( this.response );
				document.location.reload();
			} else {
				alert( this.response );
			}
		}
	} 
}

function addCommentForm( id, parent ) {
	if( parent == 'Catalogue' ) {
		for( i in document.catalogues ) {
			var n = document.catalogues[ i ];
			var name = 'c_f_'+parent+'_'+n;
			var spaner = 'c_j_'+parent+'_'+n;
			if( n == id ) {
				hide( spaner );
			} else {
				show( spaner );
				if( get( name ) != undefined ) {
					get( name ).innerHTML = '';
				}
			}
		}
	}
	var ajax = new myAjax();
	ajax.action = 'addCommentForm';
	ajax.post( 'id=' + id + '&parent=' + parent, 'comments_'+parent+'_'+id );

}

function addComment( id, parent, target, geter ) {
	if( geter == undefined ) { 	geter = 'comment';}
	var ajax	= new myAjax();
	var c 	= getValue( geter );
	c = c.split( '&' ).join( 'X_AMPERSTAND_X' );
	c = c.split( '+' ).join( 'X_PLUS_X' );
	if( c != '' ) {
		hide( 'submiter' );
		ajax.action = 'addComment';
		ajax.post( 'id=' + id + '&parent=' + parent + '&comment=' + c, target );
		get( geter ).value = '';
		ajax.onLoad = function() {
			show( 'submiter' );
		}
	} else {
		alert( 'Wpisz treść komentarza' );
	}
}

function coms( id, parent, target ) {
	var ajax = new myAjax();
	ajax.action = 'getComs';
	ajax.post( 'id=' + id + '&parent=' + parent, target );
	ajax.onLoad = function() {
		show( 'submiter' );					
	}
}

function threads( id, page, tpp, target ) {
	
}

function showFaq( id ) {
	var span = 'faq_' + id.toString();
	var f = get( span );
	for( i in document.faqs ) {
		hide( 'faq_' + document.faqs[ i ].toString() );
	}
	if( f.innerHTML == '' ) {
		var ajax = new myAjax();
		ajax.post( 'action=faq&id='+id.toString(), span );
	} else {
		show( f );
	}
}

function newsletterSubscribe() {
	var t = get( 'newsletter_message' );
	var email 	= getValue( 'newsletter_mail' );
	var type 	= getValue( 'newsletter_type' );
	var ajax 	= new myAjax();
	t.innerHTML = '';
	ajax.post( 'action=newsletter_subscribe&newsletter_mail='+email+'&newsletter_type='+type );
	ajax.onLoad = function() {
		show( 'newsletter_message' );
//		t.innerHTML = this.response;
		switch( trim( this.response ) ) {
			case 'ok':
				hide( 'newsletter' );
				show( 'newsletter_blank' );
				t.innerHTML += '<span style="color:#ffffff; ">Dziękujemy<br/>Twój adres e-mail został zapisany.</span>';	
				break;
			case 'bad_email':
				t.innerHTML += '<span style="color:#ffffff; ">Wpisano błędny adres email</span>';	
				break;
			default:
				break;
		}
	}
}

function newsletterUnsubscribe() {
	var t = get( 'newsletter_message' );
	var email 	= getValue( 'newsletter_mail' );
	var ajax 	= new myAjax();
	t.innerHTML = '';
	ajax.post( 'action=newsletter_unsubscribe&newsletter_mail='+email );
	ajax.onLoad = function() {
		show( 'newsletter_message' );
//		t.innerHTML = this.response;
		switch( trim( this.response ) ) {
			case 'ok':
				hide( 'newsletter' );
				show( 'newsletter_blank' );
				t.innerHTML += '<span style="color:#ffffff; ">Twój adres e-mail został skasowany.</span>';	
				break;
			case 'bad_email':
				t.innerHTML += '<span style="color:#ffffff; ">Wpisano błędny adres email</span>';	
				break;
			default:
				break;
		}
	}
}

function updateAjaxIcon( target, sel ) {
	var s = getValue( sel );
	if( document.buffer_icon == undefined ) { document.buffer_icon = s; }
	if( document.buffer_icon != s ) {
		document.buffer_icon = s;
		var ajax = new myAjax();
		ajax.action = 'iconPreview';
		ajax.post( 'icon='+s, target );
	}
	setTimeout( 'updateAjaxIcon( \''+target+'\', \''+sel+'\' );', 1500 );
}

function niceUrl( str, target ) {
		var string = getValue( str );
		string = string.split( '&' ).join( '&amp;' );
		var ajax = new myAjax();
		ajax.action = 'nice_url';
		ajax.post( 'string='+string );
		ajax.onLoad = function() {
			var t = get( target );
			t.value = this.response;
		}
//		setTimeout( 'niceUrl( \''+str+'\', \''+target+'\' );', 1000 );
}