var name = "#admin_menu";  
var menuYloc = null;
var menuXloc = null;
var feedback_id = null;
var place = '#temp_holder';
var current_page = "";
$(document).ready(function() {
uploaders();
$(".uploader").mouseover(function(){
    $("#upload_type").val($(this).attr("id"));
});
$(".delete_img").button().click(function(){
    var d = $(this).attr("id").split("|");
	$.post("fixes/delete.php", {id: d[0], type: d[1]});
    $("#img_holder_" + d[1] + d[0]).hide('slow',function(){
	   $("#" + d[1] + d[0]).fadeIn('slow');
	});
});
$(".delete_guitar").button().click(function(){
    var b = $(this).attr("id");
    $("#guitar" + b).hide("slow");
	$.post("fixes/delete.php", {id: b, type: "", check: "true"});
});
$(".position_submit").button().click(function(){
    var id = $(this).attr("id");
    var position = $("#position" + id).val();
    $.post("add_guitar.php", {id: id, position: position}, function(){
        window.location.reload();
    });
});
$(".comment_submit").button().click(function(){
    var id = $(this).attr("id");
    var element = $("#comment"+id).val(), splitt = element.split("");
    var leng = element.length, key_code = "", complete_text = "";
    for(var i = 0; i <= leng - 1; i++){
        key_code = element.charCodeAt(i);
        if(key_code > 122){
            complete_text = complete_text + "&#" + key_code + ";";
        }else{
            complete_text = complete_text + splitt[i];
        }
    }
    $.post("comment_save.php", { id: id, comment : complete_text, lang : $("#current_lang").val() },function(data){
			$("#spin" + id).attr("src","images/spinner.gif").fadeIn("slow").delay(1000).fadeOut('fast', function(){
							$("#spin" + id).attr("src","images/Check-icon.png").fadeIn();
							$("#spin" + id).delay(600).fadeOut('slow');
						});
		});
});
$(".visibility").click(function(){
    var id = $(this).attr("id");
    var value = $(this).val();
    $.post("guitar_visibility.php", { id : id, value: value });
});
var lang = $("#current_lang").val();
    function lava_lamp(){
        $("#1").lavaLamp({
            fx: "backout", 
            speed: 700,
            click: function(event, menuItem) {
                if($("#user_name").val() != ""){
                    return true;
                }else{
                    current_page = $(this).attr("id");
                    var link_page = $(this).find("a").attr('id');
                    $("#current_page").val(link_page);
                    if(link_page == "fixedguitars"){
                        $("#title_name").html("gitar&#371; remontas");
                        /*Remont gitar Guitar fixing*/
                        document.title = 'Gitaru remontas';
                    }else{
                        $("#title_name").html("gitaros pamokos");
                        /*Uroki gitari Guitar lessons*/
                        document.title = 'Gitaros pamokos';
                    }
                    if(link_page != "feedback"){
                        $.post("draw_feedback.php", { lang : lang, page : link_page }).success(function(data){
                            $("#temp_content_holder").delay(500).html(data);
                        });
                        $("#table_spinner").remove();
                        $("#content_holder").fadeOut("slow", function(){
                                $("#content_holder").after("<center id='table_spinner'><table style='height:400px'><tr><td height='100%'><img id='spinner_lang' src='images/spinner_lang.gif' style='display: none;'></td></tr></table></center>");
                                $("#spinner_lang").fadeIn("slow", function(){
                                    $("#spinner_lang").delay(400).fadeOut("slow", function(){
                                        $("#table_spinner").remove();
                                        $("#content_holder").css("display", "none");
                                        $("#content_holder").html(" ").html($("#get_content").val()).fadeIn("slow");
                                        $('.lightbox').lightBox();
                                    });
                                });
                            });
                    }else{
                        $.post("draw_feedback.php", { lang : lang, page : link_page }).success(function(data){
                            $("#table_spinner").remove();
                            $("#content_holder").fadeOut("slow", function(){
                                $("#content_holder").after("<center id='table_spinner'><table style='height:400px'><tr><td height='100%'><img id='spinner_lang' src='images/spinner_lang.gif' style='display: none;'></td></tr></table></center>");
                                $("#spinner_lang").fadeIn("slow", function(){
                                    $("#spinner_lang").delay(400).fadeOut("slow", function(){
                                        $("#table_spinner").remove();
                                        $("#content_holder").css("display", "none");
                                        $("#content_holder").html(" ").delay(400).html(data).fadeIn("slow");
                                        $('.lightbox').lightBox();
                                    });
                                });
                            });
                        });
                    }
                    return false;
                }
            }
        });
    }
    lava_lamp();
    jQuery.fn.exists = function(){return jQuery(this).length}
	$.post("getimg.php", {},function(data){
		feedback_id = data;
		});
 	menuXloc = parseInt($(name).css("left").substring(0,$(name).css("left").indexOf("px")))
    $("#admin_menu").bind("enter", function(){
        var offset = menuXloc+200+"px";
        $(name).animate({"left":offset},{duration:1000,queue:false,easing:"bounceout"});
    });
    menuXloc = parseInt($(name).css("left").substring(0,$(name).css("left").indexOf("px")))
    $("#admin_menu").bind("leave", function(){
        var offset = menuXloc+"px";
        $(name).animate({"left":offset},{duration:1000,queue:false,easing:"backout"});
    });
	menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))
    $(window).scroll(function(){
        var offset = menuYloc+$(document).scrollTop()+"px";
        $(name).animate({"top":offset},{duration:"slow",queue:false,easing:"backout"});
    });
    		$('textarea.editor').each( function() {
			if($(this).attr('id') == 'editor0'){
                CKEDITOR.replace($(this).attr('id'),{width:"620",toolbar: 'MyToolbar'});
                CKEDITOR.instances['editor0'].setData($("#editor0").val());
			}else{
				if($(this).attr('id') != 'editorzero'){
					CKEDITOR.replace($(this).attr('id'),{width:"400px",toolbar: 'feedback'});
				}
			}
		});
$('#add_feedback').click(function(){
		$.post("getimg.php", { },function(data){
			$.post("getimg.php", { type: 'text', id: data, lang: lang },function(data){
				if(data == ""){
					alert("You can't create another feedback, before filling previous.");
				}else{
					feedback_id++;
					draw_feedback();
				}
			});
		});
	});
$('#add_guitar').click(function(){
            window.location.href = "add_guitar.php?type=new";
	});
	$("#SubmitLink").button().click(function() { submitForm(); return false; } );
	$('#dialog').dialog({
	autoOpen: false,
	width: 400,
	resizable: false,
	open: function(){
		$('.ui-widget-overlay').hide().fadeIn();
	},
	show: "fade",
	hide: "fade",
	buttons: {
		"Ok": function() {
				var id = $("#img_to_delete").val();
				$.post("images/people/delete.php", {del_id: id});
				$("#img_holder"+id).hide('slow',function(){
				$("#uploader"+id).fadeIn('slow');
				});
				$("#img_edit"+id).css({ 'top': '-10px'})
				$("#img_delete"+id).css({ 'top': '-10px'})
				$(this).dialog("close");
			}, 
		"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	$('#dialog2').dialog({
	autoOpen: false,
	width: 400,
	resizable: false,
	open: function(){
		$('.ui-widget-overlay').hide().fadeIn();
	},
	show: "fade",
	hide: "fade",
	buttons: {
		"Ok": function() {
				var id = $("#img_to_delete").val();
				$.post("images/people/delete.php", {del_id: id});
				$.post("delete_feedback.php", {id: id});
				$('#hr'+id).fadeOut('slow',function(){
					$('#hr'+id).remove();
				});
				$("."+id).fadeOut('slow');
				$(this).dialog("close");
			}, 
		"Cancel": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
    $('.language').click(function(){
        var old_lang = $("#current_lang").val();
        if(current_page == ""){
            current_page = "main" + old_lang;
        }
        lang = $(this).attr("id");
        $.post("preview.php", {lang : lang});
        if($("#user_name").val() == ""){
            $("#__ru, #__lt, #__en").fadeOut("slow").children().removeClass("current");
            var new_page = current_page.replace(old_lang, lang);
            $("#" + new_page).addClass("current");
            $("#" + new_page).attr("class", "current");
            /*var left_lava = $("li.current").offset();
            left_lava = left_lava.left;
            alert(left_lava);
            var lava_width = $("li.current").width();
            alert(lava_width);
            $(".back").css({ "left": left_lava+"px", "width": lava_width+"px" });*/
            $("#_" + lang).delay(1000).fadeIn("slow");
            if($("#current_page").val() != "feedback"){
                $.post("draw_feedback.php", { lang : lang, page : $("#current_page").val() }).success(function(data){
                    $("#temp_content_holder").delay(500).html(data);
                });
                $("#table_spinner").remove();
                $("#content_holder").fadeOut("slow", function(){
                        $("#content_holder").after("<center id='table_spinner'><table style='height:400px'><tr><td height='100%'><img id='spinner_lang' src='images/spinner_lang.gif' style='display: none;'></td></tr></table></center>");
                        $("#spinner_lang").fadeIn("slow", function(){
                            $("#spinner_lang").delay(400).fadeOut("slow", function(){
                                $("#table_spinner").remove();
                                $("#content_holder").css("display", "none");
                                $("#content_holder").html(" ").html($("#get_content").val()).fadeIn("slow");
                                $('.lightbox').lightBox();
                            });
                        });
                    });
            }else{
                $.post("draw_feedback.php", { lang : lang, page : $("#current_page").val() }).success(function(data){
                    $("#table_spinner").remove();
                    $("#content_holder").fadeOut("slow", function(){
                        $("#content_holder").after("<center id='table_spinner'><table style='height:400px'><tr><td height='100%'><img id='spinner_lang' src='images/spinner_lang.gif' style='display: none;'></td></tr></table></center>");
                        $("#spinner_lang").fadeIn("slow", function(){
                            $("#spinner_lang").delay(400).fadeOut("slow", function(){
                                $("#table_spinner").remove();
                                $("#content_holder").css("display", "none");
                                $("#content_holder").html(" ").delay(400).html(data).fadeIn("slow");
                                $('.lightbox').lightBox();
                            });
                        });
                    });
                });
            }
        }else{
            var page;
            if(jQuery.url.param("page") == "undefined"){
                page = "main";
            }else{
                page = jQuery.url.param("page");
            }
            window.location.replace("?page=" + page + "&language=" + lang);
        }
        $("#current_lang").val(lang);
    });
    $('#mode').click(function(){
	data_transmit("preview","editor0");
	if(jQuery("#ed").is(":hidden")){
		$("#ed").delay(600).show("slow");
	}else{
		$("#ed").hide("slow");
	}
	if(jQuery("#preview").is(":hidden")){
		$("#preview").delay(600).fadeIn("slow");
	}else{
		$("#preview").fadeOut("slow");
	}
	if($("#mode").html() == "Preview mode"){
					$("#mode").fadeOut("slow", function(){
    					$("#mode").html("Editing mode").fadeIn("slow");
    					$.post("preview.php", {username: $("#mode").html()});
					});
				}else{
					$("#mode").fadeOut("slow", function(){
    					$("#mode").html("Preview mode").fadeIn("slow");
    					$.post("preview.php", {username: $("#mode").html()});
					});
				}	
    });
});

function data_transmit(elem,editor){
	var element = CKEDITOR.instances[editor].getData();
	if(element != "" || $("#current_page").val() != "feedback"){
		$("#"+elem).html(element);
	}else{
		$("#"+elem).html("Click here to add text.");
	}
}
function draw_feedback(){
					for(var i = 1;i<=feedback_id-1;i++){
						if(jQuery("#feedback"+i).exists()){
							if(!jQuery("#feedback"+i).is(":hidden")){
								feedback_submit(i);
							}
						}
					}
					$.post("feedback_save.php", {type : 'new', id : feedback_id});
                    window.location.href = "?page=feedback#bottom";
                    window.location.reload();
				}
function submitForm() {
	var element = CKEDITOR.instances["editor0"].getData();
	$("#editor0").val(element);
	$.post("ck_save.php", { page: $("#current_page").val(), editor1: $("#editor0").val(), lang : $("#current_lang").val() },
		function(data){
			$("#spinner").attr("src","images/spinner.gif").fadeIn("slow").delay(1000).fadeOut('fast', function(){		 
							$("#spinner").attr("src","images/Check-icon.png").fadeIn();
							$("#spinner").delay(600).fadeOut('slow');
						});
		});
	}
function feedback_submit(id) {
	var editor_val = CKEDITOR.instances["editor" + id].getData();
	if(editor_val != ""){
		$("#editor" + id).val(editor_val);
	}else{
		$("#editor" + id).val("Click here to add text.");
	}
	var element = $("#title"+id).val(), splitt = element.split("");
    var leng = element.length, key_code = "", complete_text = "";
    for(var i = 0; i <= leng - 1; i++){
        key_code = element.charCodeAt(i);
        if(key_code > 122){
            complete_text = complete_text + "&#" + key_code + ";";
        }else{
            complete_text = complete_text + splitt[i];
        }
    }
	$("#title_div"+id).html(element);
	$.post("feedback_save.php", { id: id, text: $("#editor" + id).val(), title : complete_text, lang : $("#current_lang").val() },
		function(data){
			$("#spinner" + id).attr("src","images/spinner.gif").fadeIn("slow").delay(1000).fadeOut('fast', function(){
							$("#spinner" + id).attr("src","images/Check-icon.png").fadeIn();
							$("#spinner" + id).delay(600).fadeOut('slow',function(){
									data_transmit('content'+id,'editor'+id);
									$("#feedback"+id).fadeOut("slow", function(){
										$("#content"+id).fadeIn("slow");
									});
									$("#title"+id).fadeOut("slow", function(){
										$("#title_div"+id).fadeIn("slow");
									});	
								});
							
						});
		});
	}
function set_position(id){
		var offset = $("#img_holder"+id).position();
		$("#img_edit"+id).css({ 'top': offset.top + 10, 'left': offset.left + 155 + 'px'})
		$("#img_delete"+id).css({ 'top': offset.top + 10, 'left': offset.left + 175 + 'px'})
}
function img_buttons_position(action,id){
	$("#img_to_delete").val(id);
	if(action == 'in'){
		$("#img_edit"+id).animate({opacity: 1},{duration:'slow',queue:false});
		$("#img_delete"+id).animate({opacity: 1},{duration:'slow',queue:false});
		}else{
			$("#img_edit"+id).animate({opacity: 0},{duration:'slow',queue:false});
			$("#img_delete"+id).animate({opacity: 0},{duration:'slow',queue:false});
		}
}

function replaceDiv(div)
{
	$.post("getimg.php", {},function(data){
	for(i=1;i<=data;i++){
		if ($("."+i).exists()) {
			data_transmit('content'+i,'editor'+i);	
			var element = $("#title"+i).val();
			$("#title_div"+i).html(element);
			if(div == i){
				$("#title_div"+div).fadeOut("slow", function(){
					$("#title"+div).fadeIn("slow");
				});
				$("#content"+div).fadeOut("slow", function(){
					$("#feedback"+div).fadeIn("slow");
				});
				}else{
					$('#feedback'+i).fadeOut('slow');
					$("#title"+i).fadeOut("slow");
				}
		}
	}
	for(i=1;i<=data;i++){
		if ($("."+i).exists()) {
			if(i == div){
				}else{
					$("#content"+i).delay(600).fadeIn("slow");
					$("#title_div"+i).delay(600).fadeIn("slow");
				}
			}
		}
	});
}
function uploaders(){
    //var elemt = "";
    var page = $("#current_page").val();
	$('input.id_holder').each(function() {
			var id = $(this).attr('id');
			if(id != 'zero'){
                var smth, folder;
				if(page != "fixedguitars"){
				    set_position(id);
                }
				    
				$('#submit'+id).click(function(){
						feedback_submit(id);
					});
				$('#content' + id + ', #title_div' + id).dblclick(function(){
						replaceDiv(id);
					});
				if($('#upload_check'+id).val() != 'yes'){
				    if(page != "fixedguitars"){
				        smth = "#upload" + id;
                        folder = "/images/people";
					$('#div_holder'+id).mouseover(function(){
						set_position(id);
						img_buttons_position('in',id);
					});
					$('#feedback_delete'+id).mouseover(function(){
						$("#img_to_delete").val(id);
						$("#feedback_delete"+id).animate({opacity: 1},{duration:'slow',queue:false});
					});
					$('#feedback_delete'+id).mouseleave(function(){
						$("#feedback_delete"+id).animate({opacity: 0.4},{duration:'slow',queue:false});
					});
					$('#div_holder'+id).mouseleave(function(){
						img_buttons_position('out',id);
					});
					$('#title'+id).keypress(function(e){
					   //elemt = elemt + "&#" + e.which + ";";
					   $("#feedback_img"+id).attr({"title" : $('#title'+id).val()});
                       /*$('#title_holder').val(elemt);
                       alert($(this).val().charCodeAt(0));*/
					});
					/*$('#img_edit'+id).click(function(){
						$("#img_holder"+id).hide();
						$.post("getimg.php", {id: id},function(data){
								$("#jcrop"+id).attr({"src" : 'images/people/'+data});
							});
						
						$("#jcrop"+id).show();
						$("#border"+id).hide();
						$("#jcrop"+id).Jcrop();
					});*/
				}else{
				    smth = "#upload_before" + id + ", #upload_after" + id;
                    folder = "/fixes";
				}
						$(smth).uploadify({
									'uploader'  : '/upload/uploadify.swf',
									'script'    : '/upload/uploadify.php',
									'cancelImg' : '/upload/cancel.png',
									'folder'    : folder,
									'auto'      : true,
									'buttonText': 'Select Image',
									'fileExt'	: '*.jpg',
									'fileDesc'    : 'Image Files',
									'displayData' : 'speed',
									'onComplete'  : function(event, ID, fileObj, response, data) {
                                                        if(page == "fixedguitars"){
                                                            var upload_type = $("#upload_type").val(), tp;
                                                            if(upload_type == "before" + id){
                                                                tp = "before";
                                                            }else{
                                                                tp = "after";
                                                            }
                                                            $.post("fixes/resize.php", {img: fileObj.name, id: id, type : tp},function(data){
                                                                $("#" + upload_type).delay(600).hide('slow',function(){
        															$("#guitar_img_" + tp + id).attr("href",'fixes/'+data);
        															$("#img_holder_" + tp + id).attr("src",'fixes/timthumb.php?src='+data+'&w=300&q=100').delay(500).fadeIn("slow");
                                                                });
    														});
                                                        }else{
    														$.post("images/people/resize.php", {img: fileObj.name, id: id},function(data){
    															$("#uploader"+id).delay(600).hide('slow',function(){
        															$("#feedback_img"+id).attr("href",'images/people/'+data);
        															$("#img_holder"+id).attr("src",'images/people/timthumb.php?src='+data+'&w=200&q=100').fadeIn();
    															set_position(id);
    															});
    														});
                                                        }
													}
							});
					$('#upload_check'+id).val('yes');
				}
			}
	});
}
