/*
    Document   : jquery.lbmodal
    Created on : 08-Jun-2010, 12:21:21
    Author     : Andreas Hadjigeorgiou
    Description:
        Purpose of the stylesheet follows.
*/
$jq = jQuery.noConflict();

$jq(document).ready(function(){
     //select all the a tag with name equal to modal
    $jq('a[name=modal]').click(function(e) {
            //Cancel the link behavior
            e.preventDefault();

            //Get the A tag
            var id = $jq(this).attr('href');

            //Get the screen height and width
            var maskHeight = $jq(document).height();
            var maskWidth = $jq(window).width();

            //Set heigth and width to mask to fill up the whole screen
            $jq('#mask').css({'width':maskWidth,'height':maskHeight});

            //transition effect
            $jq('#mask').fadeTo("fast",0.8).fadeIn(1000);
            //$jq('#mask').fadeTo("fast",0.8);

            //Get the window height and width
            var winH = $jq(window).height();
            var winW = $jq(window).width();
            var scrollTop = $jq(window).scrollTop();

            //Set the popup window to center
            $jq(id).css('top',  (winH/2-$jq(id).height()/2)+scrollTop);
            $jq(id).css('left', winW/2-$jq(id).width()/2);

            //transition effect
            $jq(id).fadeIn(200);

    });


    //if close button is clicked
    $jq('.window .close').click(function (e) {
            //Cancel the link behavior
            e.preventDefault();

            $jq('#mask').hide();
            $jq('.window').hide();
    });

    //if mask is clicked
    $jq('#mask').click(function () {
            $jq(this).hide();
            $jq('.window').hide();
    });

    // Share Specific Value
    $jq("#share_submit_btn").click(function() {

        var fromAddr = $jq("#share_from_address").val();
        var toAddr = $jq("#share_with_address").val();
        var comm = $jq("#share_comments").val();

        if(fromAddr == "") {
            alert("Your e-mail address is required");
            return false;
        }

        if(toAddr == "") {
            alert("Share with e-mail address is required");
            return false;
        }

        if(!isValidEmailAddress(fromAddr)) {
            alert("Invalid from e-mail address");
            return false;
        }
        if(!isValidEmailAddress(toAddr)) {
            alert("Invalid share with e-mail address");
            return false;
        }
        
        $jq("#share_status").html("Sending ...");

        $jq.ajax({
            type: "POST",
            url: "upfiles/File/objects/share.conn.php",
    		data: {action: "share", fromAddr: fromAddr, toAddr: toAddr, comm: comm, link: document.location.href },
    		success: function(data){
                    var results = eval('(' + data + ')');
                    //$jq("#login_loading").css("display", "none");
                    if(results.success) {
                        $jq("#share_status").html("Sent");
                        alert(results.text);
                        closeModal();
	            	//location.reload();
                    }
                    else {
                        $jq("#share_status").html(results.text);
                    }
    		}
    	});

        return false;
    });
});


function displayMask(modalid) {
    //Get the A tag
    var id = modalid;//$jq(this).attr('href');

    //Get the screen height and width
    var maskHeight = $jq(document).height();
    var maskWidth = $jq(window).width();

    //Set heigth and width to mask to fill up the whole screen
    $jq('#mask').css({'width':maskWidth,'height':maskHeight});

    //transition effect
    $jq('#mask').fadeIn(1000);
    $jq('#mask').fadeTo("slow",0.8);

    //Get the window height and width
    var winH = $jq(window).height();
    var winW = $jq(window).width();
    var scrollTop = $jq(window).scrollTop();

    //Set the popup window to center
    $jq(id).css('top',  (winH/2-$jq(id).height()/2)+scrollTop);
    $jq(id).css('left', winW/2-$jq(id).width()/2);

    //transition effect
    $jq(id).fadeIn(1000);
}

function closeModal() {
    $jq('#mask').hide();
    $jq('.window').hide();
}

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}



