﻿$(document).ready(
	function () {
        BindDragDrop();
	}
);


function BindDragDrop(){
		$('a.closeEl').bind('click', toggleContent);
		
		
//		
//		
//		$('div.groupWrapper').Sortable(
//			{
//				accept: 'groupItem',
//				helperclass: 'sortHelper',
//				activeclass : 	'sortableactive',
//				hoverclass : 	'sortablehover',
//				handle: 'div.itemHeader',
//				tolerance: 'pointer',
//				opacity:0.7,
//				onChange : function(ser)
//				{
//				},
//				onStart : function()
//				{
//					$.iAutoscroller.start(this, document.getElementsByTagName('body'));
//				},
//				onStop : function()
//				{
//					$.iAutoscroller.stop();
//					SetLayout()
//				}
//			}
//		);



			$('#phleft').sortable(
			{
			    connectWith: ['#phright'],
			    placeholder: 'sortHelper',
			    accept: 'groupItem',
			    forcePlaceholderSize: true,
			    activeclass: 'sortableactive',
			    hoverclass: 'sortablehover',
			    handle: 'div.itemHeader',
			    tolerance: 'pointer',
			    revert: true,
			    opacity: 0.7,
			    start: function() {

			    },
			    stop: function() {
			        //$.iAutoscroller.stop();
			        SetLayout();
			    }
			}
		);

			$('#phright').sortable(
			{
			    connectWith: ['#phleft'],
			    placeholder: 'sortHelper',
			    accept: 'groupItem',
			    forcePlaceholderSize: true,
			    activeclass: 'sortableactive',
			    hoverclass: 'sortablehover',
			    handle: 'div.itemHeader',
			    tolerance: 'pointer',
			    revert: true,
			    opacity: 0.7,
			    start: function() {

			    },
			    stop: function() {
			        SetLayout();
			    }
			}
		);
		
		
	
}


var toggleContent = function(e)
{
	var targetContent = $('div.itemContent', this.parentNode.parentNode);
	if (targetContent.css('display') == 'none') {
		targetContent.slideDown(300);
		$(this).html('[-]');
	} else {
		targetContent.slideUp(300);
		$(this).html('[+]');
	}
	return false;
};


function CloseApp(appname){
    $.ajax({
	    type:"GET",
	    url:"ajax/myway.ashx",
	    data: "appname=" + appname + "&action=remove&userid=" + REQUESTEDUSERID,
	    success:function(income){
	        $('#'+appname).fadeOut("slow",function(){
	            $('#'+appname).remove();
	            $('#chk'+ appname).attr('checked',false);
	        });
	    }
    });
}


function SetLayout(){
$.ajax({
    type: "GET",
    url: "ajax/myway.ashx",
    data: "L_AppNames=" + Serialize("#phleft") + "&R_AppNames=" +  Serialize("#phright") + "&action=reorder&userid=" + REQUESTEDUSERID,
    success: function(income) {
    }
});
   // BindDragDrop();
};



function Serialize(pholder) {
    var length = $(pholder +' > .groupItem').length;
    var serialized = "";
    for (var i = 0; i < length; i++) {
        serialized += $(pholder + ' > .groupItem')[i].id;
        if(i!=length-1){
            serialized += ',';
        }
    }
    return serialized;
}


function AddApp(appname){

    if ($('#chk' + appname).attr('checked') == true) {
        var lc=$('#phleft > .groupItem').length;
        var rc=$('#phright > .groupItem').length;
        var placeholdername="";
                
        if(lc<=rc){
            placeholdername='phleft';
        }else{
            placeholdername='phright';
        }
        $('#loader').show();
        $.ajax({
	        type:"GET",
	        url:"ajax/myway.ashx",
	        data: "appname=" + appname + '&placeholdername=' + placeholdername + "&action=add&userid=" + REQUESTEDUSERID,
	        success:function(income){
	            $('#'+appname).fadeIn("slow");
	            var htmlc=income;
                if(lc<=rc){
                    $('#phleft').append(htmlc);
                }else{
                    $('#phright').append(htmlc);
                }
                BindDragDrop();
                $('#loader').hide();
	        }
        });
    }else{
        CloseApp(appname);
    }
}
