﻿/**
 * Private Library
 * Library helped to useful things.
 * Library is compatibility with IE6
 *
 * Library use jQuery 1.x.x
 *
 * Copyright (c) 2010 Piotr Kowalski (piecioshka.pl)
 * License GNU
 *
 * @author Piotr Kowalski <piotr.kowalski@piecioshka.pl>
 *
 */
var Pklib = {

    version: "0.5.2",
    lastModified: document.lastModified,

    //@TODO: Not yet implemented
    ExceptionHandler: {},
    
    //@TODO: Not yet implemented
    Validate: {},

    Message: {
		
		id: "Pklib-Message-contener",
		
		show: function(content,type){
			
			var self = this;
			
			if(type == undefined) type = "info";
			
			var box = document.createElement("div");
			box.id = self.id;
			
			var contener = document.createElement("div");
			contener.id = self.id + '-contener';
			contener.innerHTML = content;
			
			var buttons = document.createElement("div");
			buttons.id = self.id + '-buttons';
			
			box.appendChild(contener);
			
			var buttonsElement = {
				
                ok: function(){
					var buttonOk = document.createElement("a");
                    buttonOk.href = "javascript:void(0);";
                    buttonOk.innerHTML = "Ok";
                    buttonOk.onclick = function(){
                        self.close(self);
                    };
                    return buttonOk;
                },
				
                cancel: function(){
                    var buttonCancel = document.createElement("a");
                    buttonCancel.href = "javascript:void(0);";
                    buttonCancel.innerHTML = "Cancel";
                    buttonCancel.onclick = function(){
                        self.close(self);
						return false;
                    };
                    return buttonCancel;
                }
			};
			
			switch(type){
				case 'warning':
					buttons.appendChild(buttonsElement.ok());
					break;
				default:
				case 'info':
					buttons.appendChild(buttonsElement.ok());
					buttons.appendChild(buttonsElement.cancel());
					break;
			}
			box.appendChild(buttons);
			
            document.getElementsByTagName("body")[0].appendChild(box);
		},
		
		close: function(self){
			if(self == undefined) self = this;
			var obj = document.getElementById(self.id);
            obj.parentNode.removeChild(obj);
		}
	},
    
    Loader: {
    	
    	src: "img/ajax-loader.gif",
    	id: "ajax-loader",
    	
    	show: function(section){
    		if(section == undefined ) section = "body";
    		var file = document.createElement('img');
    		file.src = this.src;
    		file.id = this.id;

    		$(section).append(file);
    	},
    	
    	close: function(){
    		document.getElementById(this.id).parentNode.removeChild(document.getElementById(this.id));
    	}
    
    },
    
    Utils: {
    
        outerlink: function(place){
            if (place == undefined) place = "";
            
            $(place + "a").each(function(){
                if ($(this).attr("rel") == "outerlink") {
                    $(this).unbind('click');
                    $(this).bind('click', function(){
                        window.open($(this).attr("href"));
                        return false;
                    });
                }
            });
        },
        
        confirm: function(object, text){
            if (text == undefined) text = "Are You sure?";
            
            object.bind('click', function(){
                if (confirm(text)) {
                    window.location = $(this).attr('href');
                }
                return false;
            });
            
        },
        
        clearfocus: function(objectToClear){
        
            objectToClear.each(function(k, v){
                v.defaultValue = $(v).val();
            });
            
            objectToClear.bind('focus', function(event){
                if ($(this).val() == $(this)[0].defaultValue) $(this).val('');
            }).bind('blur', function(event){
                if ($(this).val() == '') $(this).val($(this)[0].defaultValue);
            });
        },
        
        chars: [' ', '-', '_'],
        
        ltrim: function(word){
            for (var i = 0; i < this.chars.length; ++i) {
                while (true) {
                    if (word.substr(0, 1) == this.chars[i]) {
                        word = word.substr(1);
                    } else {
                        return word;
                    }
                }
            };
            
            return word;
        },
        
        rtrim: function(word){
            for (var i = 0; i < this.chars.length; ++i) {
                while (true) {
                    if (word.substr(word.length - 1) == this.chars[i]) {
                        word = word.substr(0, word.length - 1);
                    } else {
                        return word;
                    }
                }
            };
            
            return word;
        },
        
        trim: function(word){
            return this.ltrim(this.rtrim(word));
        },
        
        slug: function(text){
			var result = '';
            for (var i = 0; i < text.length; ++i) {
				var letter = text[i].toLowerCase();
				switch(letter){
					case 'e': result += "e"; break;
					case 'ó': result += "o"; break;
					case 'ą': result += "a"; break;
					case 'ś': result += "s"; break;
					case 'ł': result += "l"; break;
					case 'ż':
					case 'ź': result += "z"; break;
					case 'ć': result += "c"; break;
					case 'ń': result += "n"; break;
					
					case ' ':
					case '!':
					case '?':
					case '+': result += "-"; break;
					default: result += letter;
				}
            }
			return result;
        }
        
    },
    
    Glass: {
    
        opacity: 0.5,
		id: "glass",
        
        show: function(){
        
            var htmlObject = $('<div />').attr("id", this.id);
            
            var maxWidth = $(window).width();
            var maxHeight = $(window).height();
            
            if ($(document).width() > maxWidth) maxWidth = $(document).width();
            if ($(document).height() > maxHeight) maxHeight = $(document).height();
            
            htmlObject.css({
                height: maxHeight + "px",
                width: maxWidth + "px",
                position: "absolute",
                top: "0px",
                left: "0px",
                background: "#000000",
                overflow: 'hidden',
                opacity: this.opacity
            });
            
			document.getElementsByTagName("body")[0].appendChild($(htmlObject)[0]);
        },
        
        close: function(){
            document.getElementById(this.id).parentNode.removeChild(document.getElementById(this.id));
        }
    },
    
    Browser: {
    
        getName: function(){
            if ($.browser.safari == true) return 'safari';
            if ($.browser.opera == true) return 'opera';
            if ($.browser.msie == true) return 'msie';
            if ($.browser.mozilla == true) return 'mozilla';
            return null;
        },
        
        getVersion: function(){
            return $.browser.version;
        }
        
    },
    
    File: {
    
        attach: function(tag, src, type, section){
            if (tag == undefined) return false;
            if (src == undefined) return false;
            if (type == undefined) return false;
            if (section == undefined) section = 'head';
            
            var file = document.createElement(tag);
            file.src = src;
            file.type = type;
            
            document.getElementsByTagName(section)[0].appendChild(file);
        }
    }

};


