//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var reload = 0;

//loading popup with jQuery for edit text widget!
function loadPopupEditText(id){
    var div='#widget_text_'+id;
    //$("#edit_form").val($(div).html());
    tinyMCE.get('edit_form').setContent($(div).html());
    $("#current_id").val(id);
    $("#widget_title").val($('#widget_title_'+id).html());
    //$(".hide-this").removeClass("shown");
    //$(".hide-this").addClass("hidden");
    $(".slide").addClass("hidden");
    centerPopup('popupEditText');
    loadPopup("popupEditText");
}
function loadPopupEditPhoto(id){
    $("#modified_photo_widget_id").val(id);
    $("#photo_widget_title").val($('#widget_title_'+id).html());
    $.post("edit-photo-widget", { site_id: $("#site_id").val(), page_id: $("#page_id").val(), widget_id: $("#modified_photo_widget_id").val()}, function (data) {
            $("#photoSelect").html(data);
            //$(".hide-this").removeClass("shown");
            //$(".hide-this").addClass("hidden");
            $(".slide").addClass("hidden");
            centerPopup('popupEditPhoto');
            loadPopup("popupEditPhoto");
    } );
}
function loadPopupEditGallery(id){
    $("#modified_gallery_widget_id").val(id);
    $("#gallery_widget_title").val($('#widget_title_'+id).html());
    $.post("edit-gallery-widget", { site_id: $("#site_id").val(), page_id: $("#page_id").val(), widget_id: $("#modified_gallery_widget_id").val()}, function (data) {
            $("#gallerySelect").html(data);
            //$(".hide-this").removeClass("shown");
            //$(".hide-this").addClass("hidden");
            $(".slide").addClass("hidden");
            centerPopup('popupEditGallery');
            loadPopup("popupEditGallery");
    } );
}

function loadPopupEditSlideshow(id){
    $("#modified_slideshow_widget_id").val(id);
    $("#slideshow_widget_title").val($('#widget_title_'+id).html());
    $("#slideshow_type").val($('#slideshow_type_h').val());
    $("#slideshow_random_p").val($('#slideshow_random_h').val());
    $("#slideshow_show_title_p").val($('#slideshow_show_title_h').val());
    $.post("edit-slideshow-widget", { site_id: $("#site_id").val(), page_id: $("#page_id").val(), widget_id: $("#modified_slideshow_widget_id").val()}, function (data) {
           // $(".hide-this").removeClass("shown");
           // $(".hide-this").addClass("hidden");
           $(".slide").addClass("hidden");
            $("#slideshowSelect").html(data);
            centerPopup('popupEditSlideshow');
            loadPopup("popupEditSlideshow");
    } );
}

function loadPopup(id){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#"+id).fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $("#popupEditText").fadeOut("slow");
        $("#popupAddPage").fadeOut("slow");
        $("#popupEditPage").fadeOut("slow");
        $("#popupEditPhoto").fadeOut("slow");
        $("#popupEditGallery").fadeOut("slow");
        $("#popupEditSlideshow").fadeOut("slow");
        $("#popupImage").fadeOut("slow");
        $("#popupCurrentCSS").fadeOut("slow");
        $("#popupCSSTips").fadeOut("slow");
        $(".slide").removeClass("hidden");
        //$(".hide-this").removeClass("hidden");
     //   $(".hide-this").addClass("shown");
        popupStatus = 0;
        if (reload == 1) {
            location.href = 'index?site_id='+$("#site_id").val()+'&page_id='+$("#page_id").val();
        }
    }
}

//centering popup
function centerPopup(id){
    //request data for centering
    var popupHeight = $("#"+id).height();
    var popupWidth = $("#"+id).width();
//    var windowWidth = f_filterResults(f_clientWidth());
//    var left = (windowWidth-popupWidth)/2  + f_filterResults(f_scrollLeft());
    var windowWidth = document.documentElement.clientWidth;
    var left = windowWidth/2-popupWidth/2;
    var windowHeight = f_filterResults(f_clientHeight());
    var top = (windowHeight-popupHeight)/2 + f_filterResults(f_scrollTop())+25;
    if (top < 0) {
        top = 10;
    }
    //centering
    $("#"+id).css({
        "position": "absolute",
        "top": top,
        "left": left
    });
    //only need force for IE6
    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

function centerImage(id){
    //request data for centering
    var popupHeight = 700;
    var popupWidth = 700;
    // var windowWidth = f_filterResults(f_clientWidth());
    // var left = (windowWidth-popupWidth)/2 + f_filterResults(f_scrollLeft());
    var windowWidth = document.documentElement.clientWidth;
    var left = windowWidth/2-popupWidth/2;
    var windowHeight = f_filterResults(f_clientHeight());
    var top = (windowHeight-popupHeight)/2 + f_filterResults(f_scrollTop())+25;
    if (top < 0) {
        top = 10;
    }
    //centering
    $("#"+id).css({
        "position": "absolute",
        "top": top,
        "left": left 
    });
    //only need force for IE6
    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

function showImage (id) {
  //  $(".hide-this").removeClass("shown");
    //$(".hide-this").addClass("hidden");
    $(".slide").addClass("hidden");
    $.post("show-image", { photo_id: id}, function (data) {
    //            $("#fullsize").html("<img src='http://gallery.photo.net/photo/"+id+"-md.jpg'>");
        $("#fullsize").html(data);
        centerImage('popupImage');
        loadPopup("popupImage");
    } );

}
$(document).ready(function(){
    //CLOSING POPUP
    //Click the x event!
    $(".popupClose").click(function(){
        disablePopup();
        return false;
    });
    $("#imageClose").click(function(){
        disablePopup();
        return false;
    });
    $(".popupButtonClose").click(function(){
        disablePopup();
        return false;
    });
    //Click out event!
    $("#backgroundPopup").click(function(){
        disablePopup();
    });

    $(".addPageButton").click(function(){
        //centering with css
        centerPopup('popupAddPage');
        //$(".hide-this").removeClass("shown");
        //$(".hide-this").addClass("hidden");
        $(".slide").addClass("hidden");
        //load popup
        loadPopup("popupAddPage");
        $('#popupAddPage, #title').focus();
    });
    $('#popupAddPage, #title').keyup(function(e) {
        //
        if(e.keyCode == 13) {
            $("#popupButtonAddPage").click();
            return false;
        }
    });
    $("#popupButtonAddPage").click(function(){
        $.post("add-page-2.tcl", { site_id: $("#site_id").val(), title: $("#title").val()}, function (data) {
            var new_nav = $("#pages").html() +  "<div class=\"pagetab\" id=\"page_"+data+"\"><a href=\"?site_id="+$("#site_id").val()+"&page_id="+data+"\">"+$("#title").val()+"</a></div>";
            $("#pages").html(new_nav);
            disablePopup();
        } );
    });
    $(".editPageButton").click(function(){
        //centering with css
        centerPopup('popupEditPage');
        $(".slide").addClass("hidden");
        //load popup
        loadPopup("popupEditPage");
        $('#popupEditPage, #page_title').focus();
    });
    $('#popupEditPage, #page_title').keyup(function(e) {
        //
        if(e.keyCode == 13) {
            $("#popupButtonEditPage").click();
            return false;
        }
    });
    $("#popupButtonEditPage").click(function(){
        $.post("edit-page-2.tcl", { page_id: $("#page_id").val(), page_title: $("#page_title").val()}, function (data) {
            var new_nav = $("#pages").html() +  "<div class=\"pagetab\" id=\"page_"+data+"\"><a href=\"?site_id="+$("#site_id").val()+"&page_id="+data+"\">"+$("#title").val()+"</a></div>";
            $("#pages").html(new_nav);
            disablePopup();
            location.href = 'index?site_id='+$("#site_id").val()+'&page_id='+$("#page_id").val();
        } );
    });

});


function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

