jQuery.noConflict();
jQuery(document).ready(minisiteInit);
var iWin = null;
var iWin2 = null;
var current = 0;
function minisiteInit(){
	//Name minisite
	jQuery("#fname").click(function(){
		setParam("fname", jQuery(".fname").val());
	})
	jQuery(".sname").click(function(){
		changeVisible("name");
		jQuery(".savemini").show();
		jQuery(".publish").hide();
	});
	jQuery(".sname").hover(function(){jQuery(this).css("border","1px solid #9b9b9b")},function(){jQuery(this).css("border","1px solid white")});
	//Title minisite
	jQuery("#factivity").click(function(){
		setParam("factivity", jQuery(".factivity").val());
	})
	jQuery(".sactivity").click(function(){
		changeVisible("activity");
		jQuery(".savemini").show();
		jQuery(".publish").hide();
	});
	jQuery(".sactivity").hover(function(){jQuery(this).css("border","1px solid #9b9b9b")},function(){jQuery(this).css("border","1px solid white")});
	//Title title
	jQuery("#ftitle").click(function(){
		setParam("ftitle", jQuery(".ftitle").val());
	})
	jQuery(".stitle").click(function(){
		changeVisible("title");
		jQuery(".savemini").show();
		jQuery(".publish").hide();
	});
	jQuery(".stitle").hover(function(){jQuery(this).css("border","1px solid #9b9b9b")},function(){jQuery(this).css("border","1px solid white")});
	//image
	jQuery('.imgsbm').upload({
	     name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadlogo&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 setLogo(data);
	     }
	});
	//
	jQuery(".imageLogo").click(function(){
		 jQuery(".fimage").show();
		 jQuery("#fimage").show();
		 jQuery(".imageLogo").hide();
	});
	//text
	jQuery("#ftext").click(function(){
		ChangeText();
	});
	jQuery(".stext").click(function(){
		jQuery(".buttons").show();
		jQuery(".textsite").show();
		jQuery("#ftext").show();
		jQuery(".stext").hide();
		jQuery(".savemini").show();
		jQuery(".publish").hide();
	});
	jQuery(".stext").hover(function(){jQuery(this).css("border","1px solid #9b9b9b")},function(){jQuery(this).css("border","1px solid white")});
	//contact
	jQuery("#fcontact").click(function(){
		ChangeContactText();
	});
	jQuery(".scontact").click(function(){
		jQuery(".textcontact").show();
		jQuery(".scontact").hide();
		jQuery(".savemini").show();
		jQuery(".publish").hide();
	});
	jQuery(".scontact").hover(function(){jQuery(this).css("border","1px solid #9b9b9b")},function(){jQuery(this).css("border","1px solid white")});
	//scroll image
	jQuery(".rightlist").click(function(){
		if(current < 7){
			current++;
			jQuery(".phototable").animate({"left": "-=197px"}, "normal");
		}
		if(current > 0){
			jQuery(".leftlist").show();
			jQuery(".leftlisth").hide();
		}
		if(current >= 7){
			jQuery(".rightlist").hide();
			jQuery(".rightlisth").show();
		}
	});
	jQuery(".leftlist").click(function(){
		if(current > 0){
			current--;
			jQuery(".phototable").animate({"left": "+=197px"}, "normal");
		}
		if(current == 0){
			jQuery(".leftlist").hide();
			jQuery(".leftlisth").show();
		}
		if(current <= 7){
			jQuery(".rightlist").show();
			jQuery(".rightlisth").hide();
		}
	});
	//save all
	jQuery(".savemini").click(function(){
		setParam("fname", jQuery(".fname").val());
		setParam("factivity", jQuery(".factivity").val());
		setParam("ftitle", jQuery(".ftitle").val());
		ChangeText();
		ChangeContactText();
		jQuery(".savemini").hide();
		jQuery(".publish").show();
	});
	jQuery(".publish").click(function(){
		jQuery(".publish").hide();
		publishSite();
	});
	initUpload();
}
function setLogo(data){
	 var obj = eval("(" + data + ")");
	 if(!obj.error){
		 jQuery(".fimage").hide();
		 jQuery("#fimage").hide();
		 jQuery(".imageLogo").show();
		 jQuery(".imageLogo").attr('src', obj.image);
	 }
	 else{
		alert(obj.error); 
	 }
}
function ChangeText(){
	var posturl = "index.php";
	var text = iWin.document.body.innerHTML;
	jQuery.ajax({
		type: "POST",
		cache: false,
		dataType: "json",
		data: {option: "com_minisite", task: "settextvalue", tmpl: "component", "no_html":"1", value: text},
		url: posturl,
		success: setDataText
	});
}
function publishSite(){
	var posturl = "index.php";
	jQuery.ajax({
		type: "POST",
		cache: false,
		dataType: "json",
		data: {option: "com_minisite", task: "publishsite", tmpl: "component", "no_html":"1"},
		url: posturl,
		success: publishResult
	});
}
function publishResult(data){
	alert(data.msg);
	window.location.href = data.redirect;
}
function ChangeContactText(){
	var posturl = "index.php";
	var text = iWin2.document.body.innerHTML;
	jQuery.ajax({
		type: "POST",
		cache: false,
		dataType: "json",
		data: {option: "com_minisite", task: "settext2value", tmpl: "component", "no_html":"1", value: text},
		url: posturl,
		success: setContactText
	});
}
function setContactText(data){
	jQuery(".textcontact").hide();
	jQuery(".scontact").show();
	jQuery(".scontact").html(data.text);
}
function setDataText(data){
	jQuery(".buttons").hide();
	jQuery(".textsite").hide();
	jQuery("#ftext").hide();
	jQuery(".stext").show();
	jQuery(".stext").html(data.text);
}
function changeVisible(field){
	jQuery(".s"+field).hide();
	jQuery(".f"+field).show();
	jQuery("#f"+field).show();
}
function setParam(field, value){
	field = field.substring(1);
	var posturl = "index.php?option=com_minisite&task=setvalue&field="+field+"&value="+value+"&tmpl=component&no_html=1";
	jQuery.ajax({
		type: "POST",
		cache: false,
		dataType: "json",
		url: posturl,
		success: setData
	});
}
function setData(data){
	var field = data.field;
	var value = data.value;
	jQuery(".f"+field).hide();
	jQuery("#f"+field).hide();
	jQuery(".s"+field).show();
	jQuery(".s"+field).text(value);
	jQuery(".pathSite").text(data.alias);
}

function initContactEditor(text){
	document.write("<iframe scrolling='no' frameborder='no' src='#' id='contact' name='contact'></iframe><br/>");
	var isGecko = navigator.userAgent.toLowerCase().indexOf("gecko") != -1;
	var iframe = (isGecko) ? document.getElementById("contact") : frames["contact"];
	iWin2 = (isGecko) ? iframe.contentWindow : iframe.window;
	var iDoc = (isGecko) ? iframe.contentDocument : iframe.document;
	iHTML = "<html><head>\n";
	iHTML += "<style>\n";
	iHTML += "body, div, p, td {font-size: 16px; font-family: Trebuchet MS; margin:0px; padding:0px; color: #373636;}";
	iHTML += "body {margin: 5px;}";
	iHTML += "font {text-decoration: line-through;}";
	iHTML += "</style>\n";
	iHTML += "<body>"+text+"</body>";
	iHTML += "</html>";
	iDoc.open();
	iDoc.write(iHTML);
	iDoc.close();
	if (!iDoc.designMode) alert("Визуальный режим редактирования не поддерживается Вашим браузером");
	else iDoc.designMode = (isGecko) ? "on" : "On";
}

function initEditor(text){
	document.write("<iframe scrolling='no' frameborder='no' src='#' id='text' name='text'></iframe><br/>");
	var isGecko = navigator.userAgent.toLowerCase().indexOf("gecko") != -1;
	var iframe = (isGecko) ? document.getElementById("text") : frames["text"];
	iWin = (isGecko) ? iframe.contentWindow : iframe.window;
	var iDoc = (isGecko) ? iframe.contentDocument : iframe.document;
	iHTML = "<html><head>\n";
	iHTML += "<style>\n";
	iHTML += "body, div, p, td {font-size: 16px; font-family: Trebuchet MS; margin:0px; padding:0px; color: #373636;}";
	iHTML += "body {margin: 5px;}";
	iHTML += "font {text-decoration: line-through;}";
	iHTML += "</style>\n";
	iHTML += "<body>"+text+"</body>";
	iHTML += "</html>";
	iDoc.open();
	iDoc.write(iHTML);
	iDoc.close();
	if (!iDoc.designMode) alert("Визуальный режим редактирования не поддерживается Вашим браузером");
	else iDoc.designMode = (isGecko) ? "on" : "On";
}
function setBold() {
	iWin.focus();
	iWin.document.execCommand("bold", null, "");
}
function setItal() {
	iWin.focus();
	iWin.document.execCommand("italic", null, "");
}
function setUnder() {
	iWin.focus();
	iWin.document.execCommand("underline", null, "");
}
function setList(){
	iWin.focus();
	iWin.document.execCommand("InsertUnorderedList", null, "");
}
function setUrl(){
	var url = prompt("Введите URL:", "http://");
    if (!url) return;
    iWin.focus();
    iWin.document.execCommand("CreateLink", null, url);
}
function initUpload(){
	//image1
	jQuery("#img1").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=1&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(1, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	//image2
	jQuery("#img2").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=2&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(2, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	//image3
	jQuery("#img3").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=3&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(3, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	//image4
	jQuery("#img4").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=4&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(4, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	//image5
	jQuery("#img5").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=5&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(5, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	//image6
	jQuery("#img6").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=6&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(6, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	//image7
	jQuery("#img7").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=7&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(7, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	//image8
	jQuery("#img8").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=8&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(8, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	//image9
	jQuery("#img9").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=9&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(9, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	//image10
	jQuery("#img10").upload({
		 name: 'image',
	     method: 'post',
	     enctype: 'multipart/form-data',
	     action: 'index.php?option=com_minisite&view=edit&task=uploadphoto&number=10&tmpl=component&no_html=1',
	     onSubmit: function() {
	     },
	     onComplete: function(data) {
	    	 var obj = eval("(" + data + ")");
	    	 if(!obj.error){ setImage(10, obj.image); }
	    	 else{	alert(obj.error); }
		}
	});
	jQuery("#uimg1").click(function(){
		jQuery("#upload1").show();
		jQuery("#uimg1").hide();
	});
	jQuery("#uimg2").click(function(){
		jQuery("#upload2").show();
		jQuery("#uimg2").hide();
	});
	jQuery("#uimg3").click(function(){
		jQuery("#upload3").show();
		jQuery("#uimg3").hide();
	});
	jQuery("#uimg4").click(function(){
		jQuery("#upload4").show();
		jQuery("#uimg4").hide();
	});
	jQuery("#uimg5").click(function(){
		jQuery("#upload5").show();
		jQuery("#uimg5").hide();
	});
	jQuery("#uimg6").click(function(){
		jQuery("#upload6").show();
		jQuery("#uimg6").hide();
	});
	jQuery("#uimg7").click(function(){
		jQuery("#upload7").show();
		jQuery("#uimg7").hide();
	});
	jQuery("#uimg8").click(function(){
		jQuery("#upload8").show();
		jQuery("#uimg8").hide();
	});
	jQuery("#uimg9").click(function(){
		jQuery("#upload9").show();
		jQuery("#uimg9").hide();
	});
	jQuery("#uimg10").click(function(){
		jQuery("#upload10").show();
		jQuery("#uimg10").hide();
	});
}
function setImage(id, image){
	jQuery("#uimg"+id).show();
	jQuery("#upload"+id).hide();
	jQuery("#nimg"+id).attr("src", image);
}
