﻿/***************************/
//@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.1"
		});
		$("#backgroundPopup").fadeIn("medium");
		$("#popupContact").fadeIn("medium");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("medium");
		$("#popupContact").fadeOut("medium");
		popupStatus = 0;
		$get('dImportContacts').style.display = "none";
		
	}

}

//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 + 40,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

function setupContainer() {

    //centering with css
    centerPopup();
    //load popup
    loadPopup();
    document.getElementById('result').innerHTML = "<img src=\"/Images/spinner4.gif\" />"
    document.getElementById('login').style.display = "block";
    document.getElementById('result').style.display = "none";
    document.getElementById('username').value = '';
    document.getElementById('password').value = '';
    document.getElementById('loginError').innerHTML = '';
    document.getElementById('username').focus();
    
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

    //LOADING POPUP
    //Click the button event!
    $("#gmailButton").click(function() {
        setupContainer();
        document.getElementById('title').innerHTML = 'Import Addresses From <img src=\'/Images/gmail.jpg\' />';
        document.getElementById('mailType').value = 'gmail';
    });

    $("#yahooButton").click(function() {
        setupContainer();
        document.getElementById('title').innerHTML = 'Import Addresses From <img src=\'/Images/yahoo.jpg\' />';
        document.getElementById('mailType').value = 'yahoo';
    });

    $("#hotmailButton").click(function() {
        setupContainer();
        document.getElementById('title').innerHTML = 'Import Addresses From <img src=\'/Images/hotmail.jpg\' />';
        document.getElementById('mailType').value = 'hotmail';
    });

    //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();
        }
    });

    $(document).keypress(function(e) {
        if (e.keyCode == 13 && popupStatus == 1) {
            if (document.getElementById('login').style.display == 'block') {
                GetContacts();
                return false;
            }
        }
    });

});
