// JavaScript Document

$(document).ready(function(){
    fixImages();
    modalConfirmBox();
    $('#header .quote,.quote_popup').colorbox({href:"/contact_form.php?ajax=1"});
    $('.gallery.group .image a').colorbox({width:550, height:550});
    $( ".datepickerSQL" ).datepicker({ dateFormat: 'yy-mm-dd' });
    $.get('/contact_form.php',{'ajax':1},function (data) {
    	$('.ajax_form').replaceWith(data);
    });
});

function fixImages(){
    $("div#content img").each(function(){
        var img = $(this);
        if(img.attr("src").indexOf('/UserFiles/') != -1){
        	img.attr("src",img.attr("src") + "?width="+img.width()+"&height="+img.height()+"");
        }
    });
}

/**
* Convert number of seconds into time object
*
* @param integer secs Number of seconds to convert
* @return object
*/
function secondsToTime(secs){
	var hours = Math.floor(secs / (60 * 60));
	var divisor_for_minutes = secs % (60 * 60);
	var minutes = Math.floor(divisor_for_minutes / 60);
	var divisor_for_seconds = divisor_for_minutes % 60;
	var seconds = Math.ceil(divisor_for_seconds);
	
	var obj = {
		"h": ((hours < 10)?'0'+hours:hours),
		"m": ((minutes < 10)?'0'+minutes:minutes),
		"s": ((seconds < 10)?'0'+seconds:seconds)
	};
	return obj;
}

/**
 * Convert number of bytes into human readable format
 *
 * @param integer bytes     Number of bytes to convert
 * @param integer precision Number of digits after the decimal separator
 * @return string
 */
function bytesToSize(bytes, precision){  
    var kilobyte = 1024;
    var megabyte = kilobyte * 1024;
    var gigabyte = megabyte * 1024;
    var terabyte = gigabyte * 1024;
    if ((bytes >= 0) && (bytes < kilobyte)) {
        return bytes + ' B';
    } else if ((bytes >= kilobyte) && (bytes < megabyte)) {
        return (bytes / kilobyte).toFixed(precision) + ' KB';
    } else if ((bytes >= megabyte) && (bytes < gigabyte)) {
        return (bytes / megabyte).toFixed(precision) + ' MB';
    } else if ((bytes >= gigabyte) && (bytes < terabyte)) {
        return (bytes / gigabyte).toFixed(precision) + ' GB';
    } else if (bytes >= terabyte) {
        return (bytes / terabyte).toFixed(precision) + ' TB';
    } else {
        return bytes + ' B';
    }
}

modalConfirmBox = function(){
	$("a[class*='confirmModal']").click(function(e){
  		e.preventDefault();
		var html = '<div id="modalDiv" title="Are You Sure?" style="text-align:center;">'
				 + 'This operation cannot be undone.<br><br>Do you wish to proceed?'
				 + '</div>';
		var link = $(this).attr("href");
		$(html).dialog({
			modal: true,    						
			position: ["center",150],
			resizable: false,
			buttons: { 				
                "Cancel": function() { 
                    $(this).dialog("close"); 
                },
                 "OK": function() { 
                     window.location = link;
                 }
             }
			
		});
	});
};

messageStack = {
	add : function(message,type){
		$('#jsMessageStack').html('<div class="messageStack"><div class="messageStack' + type + '"></span>'+ message +'</div></div>');
	},
	
	show : function(){
		$('#jsMessageStack').show();
	},
	
	remove : function(){
		if($("#stackCopy").length != 1){
			   $("body").prepend('<div id="stackCopy"></div>');
			}
			var msgs = $("#jsMessageStack div");
			var time = (msgs.length * 3000) + 1500;
			var newID = "cloneStack_" + Math.ceil(10000*Math.random());
			$("#stackCopy").prepend('<div id="'+ newID +'"></div>');	
			msgs.prependTo("#" + newID);	
			$("#jsMessageStack").hide().empty();
			$("#" + newID).fadeTo(time,1,function(){
		        $("#" + newID).fadeOut(2000, function(){
		            $("#" + newID).empty().remove();
		            if($("#stackCopy div").length < 1){
		                $("#stackCopy").remove();
		            }
		        });
		    });
	}
}
