
var popupList = [];

function loadPopup(id){
	var		bg = jQuery("#backgroundPopup");
	var		pp = jQuery(id);

	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	pp.css({
		"top": (windowHeight-pp.height())/2,
		"left": (windowWidth-pp.width())/2

	});
	
	if(popupList.length==0){
		bg.css({
			"width": windowWidth,
			"height": "1500px",
			"opacity": "0.7"
		});
		bg.fadeIn("slow");
		pp.fadeIn("slow");
	} 
	if($.browser.msie) {
		pp.css({
			"display": "block",
			"position": "absolute"
		
		}
		);}
	else {
			pp.css({
				"display": "block"
			}
			);
	}

	++popupList.length;
	popupList[popupList.length-1] = id;
}
		
function disablePopup(id){
	if (popupList.length==0) return false;

	for (var i = popupList.length-1; i>0; --i) {
		jQuery(popupList[i]).css({
			"display": "none"
		});

		if (id==popupList[i]) {
			popupList.length = i;
			return false;
		}
	}

	jQuery(popupList[0]).fadeOut("slow");
	jQuery("#backgroundPopup").fadeOut("slow");
	popupList.length = 0;

	return false;
}
		
//CLOSING POPUP
//Click the x event!
/*jQuery("#popupContactClose").click(function(){
		disablePopup();
		
});
*/		
//Click out event!
jQuery("#backgroundPopup").click(function(){
	disablePopup();
});
		
//Press Escape event!
$(document).keypress(function(e){
	if(e.keyCode==27){
		disablePopup();
	}
});
		
function openWindow(that) {
    var popup = window.open(that.href, "popup", "height=700,width=1000, scrollbars=1");

    return false;
}

/**
jquery popup utility
 */

// Create a namespace for our utilities
var UTIL = UTIL || {};
UTIL.popup = UTIL.popup || {};

/**
 * Open popup window
 * 
 * Opens a popup window using as little as a URL. An optional params object can 
 * be passed.
 * 
 * @param {Object} href
 * @param {Object} params
 * @return {WindowObjectReference}
 */
UTIL.popup.open = function (href, params)
{
    // Defaults (don't leave it to the browser)
    var defaultParams = {
        "width":       "800",   // Window width
        "height":      "600",   // Window height 
        "top":         "0",     // Y offset (in pixels) from top of screen
        "left":        "0",     // X offset (in pixels) from left side of screen
        "directories": "no",    // Show directories/Links bar?
        "location":    "no",    // Show location/address bar?
        "resizeable":  "yes",   // Make the window resizable?
        "menubar":     "no",    // Show the menu bar?
        "toolbar":     "no",    // Show the tool (Back button etc.) bar?
        "scrollbars":  "yes",   // Show scrollbars?
        "status":      "no"     // Show the status bar?
    };
    
    var windowName = params["windowName"] || "new_window";
    
    var i, useParams = "";
    
    // Override defaults with custom values while we construct the params string
    for (i in defaultParams)
    {
        useParams += (useParams === "") ? "" : ",";
        useParams += i + "=";
        useParams += params[i] || defaultParams[i];
    }
    
    return window.open(href, windowName, useParams);
};




$(function(){ // Run this code when the document's done loading

    // Apply this code to each link with class="popup"
    $("span.popup").each(function (i){
		
		// Add an onClick behavior to this link 
        $(this).click(function(event) {
            
			// Prevent the browser's default onClick handler
            event.preventDefault();
    
            // Grab parameters using jQuery's data() method
            var params = $(this).data("popup") || {};
        
            // Use the target attribute as the window name
            if ($(this).attr("target"))
            {
                params.windowName = $(this).attr("target");
            }
        
            // Pop up the window
            var windowObject = UTIL.popup.open($(this).attr("href"), params);
			
            // Save the window object for other code to use
            $(this).data("windowObject", windowObject);
        });
    });
});

function reqJSON(url, params, func) {
	var callParams = {}; 
    callParams.type = params.Method || "GET"; 
    callParams.url = url; 
    callParams.processData = true; 
    callParams.data = params; 
    callParams.dataType = "json"; 
    callParams.success = func; 
    callParams.error = function(){
    	setTimeout(function(){
    		reqJSON(url, params, func, callParams.error);
    	}, 1000);
    };
    $.ajax(callParams);
}

(function (){

	jQuery("#popPostQuestion").click(function(){showPostQuestionDlg(); return false;});	

	reqJSON(_cvHost+'/partner/info2', {'name' : _partnerKey}, function(json){
		jQuery('#xenserverProductCount').text(json.product_count);
		jQuery('#xenserverVoteCount').text(json.vote_count);

	});
})();	// get CV counts

function encHtml(s) {
	return $('<div />').text(s).html();
}

function setupForum() {
	var html = '<option value="">--- Select a Forum ---</option>';
	var forumList = {
						'f102' : {
							'News, Announcements and Known Issues' : 502,
							'Server Installation' : 503,
							'Windows Guest Installation' : 504,
							'Linux Guest Installation' : 505,
							'Guest Runtime' : 506,
							'Networking' : 507,
							'Storage' : 508,
							'Administrator Console' : 509,
							'Command-Line Interface' : 510,
							'P2V/V2V Tools: XenConvert and v2xva' : 511,
							'Feature Requests and Suggestions' : 512,
							'HP Integrated' : 570,
							'Dell Integrated' : 571,
							'Miscellaneous' : 513,
							'XenServer Project George Beta' : 835,
							'XenServer/Essentials Compatibility' : 778
						},
	
						'f172' : {
							'News, Announcements and Known Issues' : 837,
							'StorageLink Manager' : 827,
							'StorageLink Command Line (CLI)' : 828,
							'StorageLink PowerShell' : 829,
							'Fabric and Array Support' :830,
							'Feature Requests/Suggestions' : 831
						},

						'f40' : {
							'Provisioning Server for Datacenters' : 170
						},

						'f168' : {
							'Lab Manager' : 822
						}

					}['f'+$(':input[name=postQuestionCategory]').val()] || [];
	for (var f in forumList) {
		html += '<option value="'+forumList[f]+'">'+encHtml(f)+'</option>'
	}

	var sel = $(':input[name=postQuestionForum]');
	sel.html(html);
	sel.selectedIndex = 0;
}

function showPostQuestionDlg() {
	var form = document.forms['postQuestionForm'];
	form.postQuestionCategory.selectedIndex = 0;
	setupForum();
	form.postQuestionIsQ.checked = true;
	form.postQuestionSubject.value = "";
	form.postQuestionBody.value = "";

	loadPopup("#postQuestionDlg");

	return false;
}

function postQuestion(form) {
	var forum = form.postQuestionForum;
	if (forum.selectedIndex<=0) {
		alert('please select a forum category');
		return;
	}
	var isQ = form.postQuestionIsQ.checked;
	var subject = form.postQuestionSubject.value;
	if (subject=='') {
		alert('please enter a subject');
		return;
	}
	var body = form.postQuestionBody.value;
	if (body=='') {
		alert('please enter your content');
		return;
	}

	forum = forum.options[forum.selectedIndex].value;

	disablePopup("#postQuestionDlg");

	jQuery.getJSON(_cvHost+'/partner/post', {'name' : _partnerKey, 'forum' : forum, 'subject' : _partnerKey+' '+subject, 'body' : body, 'isq' : isQ}, function(json){
		if (json.post_succeeded) {
			alert('Your question/comment has been posted. Please wait 20-30 minutes to see your post');
		}else{
			alert('Action Failed');
		}
	});	
}