﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

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

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});

}

// resize myLinks container and nav pane
function ResizeMyLinksContainer() {
    var h = $(window).height();
    if ((h - 50) > 0)
    { $("#container").css('height', h - 50);  }
    else
    { $("#container").css('height', h); }

    if ((h - 125) > 0) {
        $("#leftnav").css('height', h - 125);
    }
    else {
        $("#leftnav").css('height', h);
    }
}

// hook into these dom events so my links container div resizes ....
jQuery.event.add(window, "load", ResizeMyLinksContainer);
jQuery.event.add(window, "resize", ResizeMyLinksContainer); 

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {
    // this is hooking up the 'add new link' link's click event
    $("#newLink").click(function() {
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });

    //CLOSING POPUP
    //Click the x event!
    $("#popupContactClose").click(function() {
        disablePopup();
    });
    //Click out event!
    $("#backgroundPopup").click(function() {
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });

    // hook up the 'add button' click event to actually add a new link
    $("#btnAdd").click(function() {
        var outerCat = getOuterCatValue();
        var innerCat = getInnerCatValue();
        var display = $("#txtDisplay").val();
        var linkurl = $("#txtUrl").val();
        // innerCat is allowed to be 'blank', link tied directly to outercat
        if (outerCat.length == 0 || display.length == 0 || linkurl.length == 0) {
            disablePopup();
            $("#ErrMsg").html("Please enter all values dude-man");
            return;
        }
        $.post("MyLinkOperations.aspx",
           { id: "0", oc: outerCat, ic: innerCat, d: display, linkurl: linkurl },
   	        function(data) {
   	            disablePopup();
   	            var msg = " .. new link successfully added [ " + display + " ]";
   	            $("#ErrMsg").html(msg);

   	        } // callback delegate
        ); // .post
        //GoReloadTree();
    });

});

function getOuterCatValue() {
    var val;
    val = "";
    if ($("#txtOuterOpt").val() != null && $("#txtOuterOpt").val().length > 0) {
        val = $("#txtOuterOpt").val();
    }
    else {
        //val = $("#drpOuter").selectedValues()[0];
        val = $('#drpOuter option:selected').val();
        //console.log($("#foo").val());
        //console.log($("#foo option:selected").text());
    } 
    return val;
}

function getInnerCatValue() {
    var val;
    val = "";
    if ($('#ckBlankInner').is(':checked')) {
        //alert("please keep inner blank!");
        val = "";
    }
    else {
        if ($("#txtInnerOpt").val() != null && $("#txtInnerOpt").val().length > 0) {
            val = $("#txtInnerOpt").val();
        }
        else {
            val = $('#drpInner option:selected').val();
        }
    }
    return val;
}

function RemoveLink(linkID) {
    if (linkID == null)
        return;
    //alert("id = " + linkID);
    $.post("MyLinkOperations.aspx",
           { id: linkID, oc: "REMOVE" },
   	        function(data) {
   	            disablePopup();
   	            var msg = "..link successfully removed [ id : " + linkID + " ]";
   	            $("#ErrMsg").html(msg);
   	            //GoReloadTree(); 
   	        } // callback delegate
        );  // .post
}