﻿// JScript File


/////// SubMenu Functionality

	$(document).ready(function() {


		function showDrop ()
		{
			$(".subMenu").slideUp("fast");
			
			//setTimeout("return true;", 5000);
			
			
			
			var showName = $(this).attr('id');
				
			var position = $(this).offset();
			
			var subMenu = showName + "_sm";
			
			var leftPos = position.left - 0;
			var topPos = position.top + 37;
			
			$("#" + subMenu).css({'top': topPos, 'left': leftPos});
			
			
			$("#" + subMenu).fadeIn(300);
		}
		
		function hideDrop()
		{
			
			
			return true;
			
		}

		var dropdownConfig = {
		interval: 100,
		sensitivity: 3,
		over: showDrop,
		out: hideDrop,
		timeout: 500
		};
		
		$("#topNav>li").hoverIntent(dropdownConfig)


		var hideConfig = {
		interval: 10,
		sensitivity: 1,
		out: hideDrop,
		timeout: 500
		};
		
		///$(".subMenu").hoverIntent(hideConfig)

		
		
		$(".subMenu").hover(function() 
			{
				var menuId = $(this).attr('id');
				var tabId = menuId.substring(0,  menuId.length - 3);
				var selector = "#topNav #" + tabId + " a";
				$(selector).addClass("topNavItemHover");
			},
			function()
			{
				var menuId = $(this).attr('id');
				var tabId = menuId.substring(0,  menuId.length - 3);
				var selector = "#topNav #" + tabId + " a";
				$(selector).removeClass("topNavItemHover");
				$(this).fadeOut(600);
			});
		
		
		$("#content").hover(function()
		{
		    $(".subMenu").fadeOut(600);
		},
		function()
		{
		});
		
	});

    ////// LightBox Modal Trigger

    function showModuleLightBox(moduleId) {
            
        
            $.fn.colorbox({ href: '/modules/myMedikeeper/LightBox/ViewModule.aspx?ModuleId='+ moduleId, opacity: 0.90, scrollbars: false, open: true, close: '', initialWidth: 330, initialHeight: 300, onClosed: function(){showCarouselModal()} });

    }
    
    
/////// Carousel Modal Trigger

    function showCarouselModal()
    {
        
        $.fn.colorbox({ href:"#PC", inline: true, initialWidth: 330, initialHeight: 300});
        
//        $.fn.colorbox({ href: '/modules/myMedikeeper/3dCarousel/Carousel.aspx', opacity: 0.90, iframe: true, scrollbars: false, open: true, close: '', initialWidth: 330, initialHeight: 300, innerWidth: 960, innerHeight: 420 }, function() {
//        });
    
    }

/////// Module Buttons Hover
	$(document).ready(function(){
				
		$(".moduleButtons a img").hover(function(){
			var s = $(this).attr('src');
			if (!s.match(/-dn\./)) {
				s = s.replace(/(\.[a-z]+)$/, '-dn$1');
				$(this).attr('src', s);
			}
		}, function() {
			var o = $(this).attr('src');
			o = o.replace(/-dn\./, '.');
			$(this).attr('src', o);} );
							   
							   
	});



////// Dynamic SubMenu Width
    $(document).ready(function () {
    
        $(".subMenu").each(function () {

           var h = $(this).height();
           
           if (h > 250)
           {
                //// Do Modifications for formatting
                $(this).find(".subNavContentWrapper").find(".subNavContentRight").find(".subNavContentGrad").find(".subNavContent").find(".subNavContentSpacer").find(".menuBlock").each(function(){
                    $(this).css("float", "left");
                });
                
           }
           else
           {
              //// Scale down menu size  
              $(this).css("width", "210px");
              $(this).find(".subNavTop").find(".subNavTopRight").css("width", "215px");
              $(this).find(".subNavContentWrapper").find(".subNavContentRight").css("width", "215px");
              $(this).find(".subNavContentWrapper").find(".subNavContentRight").find(".subNavContentGrad").find(".subNavContent").css("width","180px");
              $(this).find(".subNavBottom").find(".subNavBottomRight").css("width", "210px");
           }
         });
    
    });
    
//////////////// IE 7 Workarounds

// Returns the version of Windows Internet Explorer or a -1
// (indicating the use of another browser).
function getInternetExplorerVersion()
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    
        if (re.exec(ua) != null)
            rv = parseFloat( RegExp.$1 );
    }
    return rv;
}

function isIE7orBelow()
{
    var ver = getInternetExplorerVersion();

    if ( ver> -1 )
    {
        if ( ver< 8.0 )
        {
            return true;
        }
    }
    
    return false;
}




$(document).ready(function(){

    

    
    if (isIE7orBelow() == true) //// It's not IE 8
    {
        ///// Sets Module Buttons to the correct spot
        $(".moduleButtons").css("top", "-18px");
        $(".moduleButtons").css("right", "5px");
    }

});


$(document).ready(function () {

    $(".calendarTrigger").attr("readonly", true);

    $(".calendarTrigger").datepicker({ showOn: 'focus', dateFormat: 'mm/dd/yy', showAnim: 'fadeIn', changeMonth: true, showOn: 'both', buttonImage: '/images/calendar.gif', buttonImageOnly: true, changeYear: true, yearRange: '-100:+1' });


    $(".calendarTriggerAus").attr("readonly", true);

    $(".calendarTriggerAus").datepicker({ showOn: 'focus', dateFormat: 'dd/mm/yy', showAnim: 'fadeIn', changeMonth: true, showOn: 'both', buttonImage: '/images/calendar.gif', buttonImageOnly: true, changeYear: true, yearRange: '-100:+1' });

    var date = new Date();
    var d = date.getDate();
    var day = (d < 10) ? '0' + d : d;
    var m = date.getMonth() + 1;
    var month = (m < 10) ? '0' + m : m;
    var yy = date.getYear();
    var year = (yy < 1000) ? yy + 1900 : yy;


    /*var txtDate = getElementByClassName("calendarTriggerAus");
    //txtDate = $(".calendarTriggerAus");

    for(var i=0;i<txtDate.length;i++)
    {
    if (txtDate[i].value == "") {
    $(txtDate[i]).attr("value", day + "/" + month + "/" + year)}
    }*/

    $(".calendarTriggerAus").filter('[value=""]').val(day + "/" + month + "/" + year);


});

//required to show date locally for different users
function getLocalDate(textBox) {
    var date = new Date();
    var d = date.getDate();
    var day = (d < 10) ? '0' + d : d;
    var m = date.getMonth() + 1;
    var month = (m < 10) ? '0' + m : m;
    var yy = date.getYear();
    var year = (yy < 1000) ? yy + 1900 : yy;


    $(".calendarTriggerAus").attr("readonly", true);

    $(".calendarTriggerAus").datepicker({ showOn: 'focus', dateFormat: 'dd/mm/yy', showAnim: 'fadeIn', changeMonth: true, showOn: 'both', buttonImage: '/images/calendar.gif', buttonImageOnly: true, changeYear: true, yearRange: 'c-100:c+1' });


    $(textBox).filter('[value=""]').val(day + "/" + month + "/" + year);
}
///// MediPortal Scripts


function changeModulePosition(columnNumber, position, moduleID)
{
    var modId = moduleID.substring(3, moduleID.length);
    var colNum = columnNumber;
    var posNum = position;
    
    
    ///// Ajax Post Call to Update the modules position
   $.post("/modules/myMedikeeper/ChangeModulePosition.aspx", {moduleid: modId, column: colNum, position: posNum});
}

function moduleChange(moduleId, changeType)
{
        var modId = moduleId.substring(3, moduleId.length);
        var ctype = changeType;
        
        
        $.post("/modules/myMedikeeper/MoveModule.aspx", {moduleid: modId, type: ctype});
        
        
        
    
}

function moduleColorChange(moduleID, colorClass)
{
        var modId = moduleID;
        var colorCl = colorClass;
        
        $.post("/modules/myMedikeeper/ChangeModuleDetails.aspx", {moduleid: modId, colorclass: colorCl});
}

function moduleColorChangeAll(colorClass)
{
        var colorCl = colorClass;
        
        $.post("/modules/myMedikeeper/ChangeModuleDetails.aspx", {colorclass: colorCl, changeall: "true"});
}


$(document).ready(function () {
	
	
	//// Get Items that are sortable	
	//// Basically as long as they don't have the className staticModule
	var sItems = $('.column > li:not(.static-module)');
	
	var currentPos;
	var currentCol;
	
	//$(sItems).css({cursor: 'move' });
	
	
	
	
	$(".column").sortable({
		
		connectWith: '.column',
		handle: '.module-header',
		placeholder: 'module-placeholder',
		forcePlaceholderSize: true,
		items: sItems,
		revert: 300,
		delay: 50,
		start: function (e, ui){
			ht = $(ui.item).height();
			wd = $(ui.item).width();
			$('.module-placeholder').css({width: wd + 'px', height: ht + 'px'})
		},
		stop: function (e, ui) {
			var mid = $(ui.item).attr("id");
			var cid = $(ui.item).parent().attr("id");
			var pos = "";
			$("#"+ cid +' li').each( function () {
				if (!($(this).hasClass("static-module")))
				{
					pos += $(this).attr("id") + "|";
				}
			});
			
			pos = pos.substring(0, pos.length-1);
			cid = cid.substring(cid.length-1, cid.length);
			
			
			
			
			changeModulePosition(cid, pos, mid);
			
		}
	});
	
	
	$(".module").addClass("ui-helper-clearfix")
			.find(".module-header").find(".module-header-content")
			    .prepend('<span href="#" class="edit">EDIT</span>')
			    .prepend('<span href="#" class="move">MOVE</span>')
				.end()
			.find(".module-content");

    //// Removes Controls for Static Modules
    
    $(".static-module").find(".module-header").find(".module-header-content").find("span.move").remove();
    $(".static-module").find(".module-header").find(".module-header-content").find("span.edit").remove();


    //// Add Edit Box Colors To Chose From
    $(".module-header").find("span.edit").parent().append('<ul class="editModule">' +
                                                                '<li><ul class="moduleColor">' +
                                                                    '<li class="module-colorWheel"></li><li class="moduleChoice-default" title="module-default"></li><li class="moduleChoice-blue" title="module-blue"></li><li class="moduleChoice-green" title="module-green"></li><li class="moduleChoice-yellow" title="module-yellow"></li><li class="moduleChoice-red" title="module-red"><li class="moduleChoice-gray" title="module-gray"></li></ul></li>' +
                                                                '<li class="applyAll"><a href="#" class="applyToAll">Apply Color to All</li>' +
                                                                '<li class="close"><a href="#" class="closeModule">Close Module</a></li>' +
                                                                '<li class="minimize"><a href="#" class="minimizeModule">Minimize Module</a></li>' +
                                                            '</ul>');


	///// Maximize/Minimize Functionality
	$("ul.editModule li.minimize").click(function(e) {
	        e.preventDefault();
			$(this).find("a").toggleClass("minimizeModule").toggleClass("maximizeModule");
			$(this).parents(".module:first").find(".module-content").slideToggle();
			if ($(this).find("a").hasClass("minimizeModule"))
			    $(this).find("a").text("Minimize Module");
			else
			    $(this).find("a").text("Maximize Module");
			var mid = $(this).parents(".module:first").attr("id");
			moduleChange(mid, "state");
		});
		
		
		//// Close Functionality
	$("ul.editModule li.close").click(function(e) {
			e.preventDefault();
			var modID = $(this).parents(".module:first").attr("id");
			
			$(this).parents(".module:first").animate({
				opacity: 0
			}, function(){
				$(this).slideUp("slow", function (){
					$(this).remove();
				})
				
			});
			
			moduleChange(modID, "close");
		});

		$(".column").disableSelection();


		
        ///// Minimize Modules that are set to minimize
        $(".isMin").find("div.module-content").css({"display": "none"});
        $(".isMin").find("ul.editModule li.minimize a.minimizeModule").toggleClass("minimizeModule").toggleClass("maximizeModule");
        $(".isMin").find("ul.editModule li.minimize a.maximizeModule").text("Maximize Module");


        ///// Handle Edit Menu Drop Funcitonality
        $(".module-header span.edit").click(function (){
            
            //// Determine Location to Drop
            var position = $(this).offset();
            var leftPos = position.left - 40;
			var topPos = position.top+20;
			
            $(this).parent().find("ul.editModule").css({'top': topPos, 'left': leftPos}).fadeIn('fast').show();
            
            
            $(this).parent().hover (function () {
            }, function () {
                $(this).parent().find("ul.editModule").fadeOut('slow');
            });
            
            
	    });
	    
	    ///// Handle Edit Menu Color Click Functionality
	    $("ul.moduleColor li").click(function (e) {
	        if ($(this).attr("class") != 'module-colorWheel')
	        {
	            //// Gets the Desired Color
	            var classVal = $(this).attr("title");
	            //// Targets the Module
	            var $target = $(this).parent().parent().parent().parent().parent().parent();
	            //// Removes all module-xxxx classes
	            var classes = $target[0].className.replace(/module-\w+/gi, '');
	            //// Adds the Classes we want to keep
	            $target.attr('class', classes);
	            //// Then Adds the desired color class
	            $target.addClass(classVal);
    	        
	            ///// Call to Change Color in DB
	            var mid = $(this).parents(".module:first").attr("id");
	            moduleColorChange(mid, classVal);
	        }
	        
	    });


        //// Edit Menu Applyto All Functionality
        $("ul.editModule li.applyAll").click(function (e) {
        
                e.preventDefault();
        
                //// Get this modules color class
                var classString = $(this).parent().parent().parent().parent().attr('class');
                var rx = new RegExp(/module-\w+/g);
                var classVal = rx.exec(classString);
                var $target = $(".module");
                
                //// Strip classes Down
                var classes = $(".module")[0].className.replace(/module-\w+/gi,'');
                
                //// Set Modules to Same Color
                $target.attr('class', (classes + ' ' + classVal));
                
                
                /// Call to Change Modules Color in DB
                moduleColorChangeAll(classVal.toString());
                
        });	    
	    
	    
	    //////// Make Edit Menu items clickable 
	    $("ul.editModule li.applyAll,ul.editModule li.minimize, ul.editModule li.close").css("cursor", "pointer");
	        
	    
	    
	
});


function openBox(url, w, h, close) {

    

    $.fancybox({

        href: url, type: 'iframe', width: w, height: h, closeBtn: close, closeClick: close

    });

}


$(document).ready(function () {
    $(".modalPopup").click(function (e) {
        e.preventDefault();
        
        var url = $(this).attr("href");
        var bwidth = $(this).attr("bwidth");
        var bheight = $(this).attr("bheight");
        
        $.colorbox({href: url, opacity: 0.90, overlayClose: false, initialWidth: 330, initialHeight: 300, iframe: true, width: bwidth, height: bheight });
    });

});


function openModal(url, w, h)
{
   $(document).ready(function () {
        var bwidth = w;
        var bheight = h;

        $.colorbox({ href: url, opacity: 0.90, overlayClose: false, initialWidth: 330, initialHeight: 300, iframe: true, width: bwidth, height: bheight });
    });

} 


function loadingModal(id)
{
    $("#cboxContent div#cboxClose").remove();
    $.colorbox({opacity: 0.95, overlayClose: false, href: id, width: 275, height: 205, escKey: false, inline: true});
}


