/**
 * @repository-version 1.0.0
 */
var sRemoteServer = location.protocol + '//' + location.host + '/';

var Ui = {
    capsNotifier: false,
    layerBlock: function (id, text) {
        $('#'+id).block({
           message: '<h3 class="loader-text">'+text+'</h1>'
        });
    },
    layerUnblock: function(id) {
        $('#'+id).unblock();
    },
    collect: function(divID, attribute, escapeOutput) {
        
    if (attribute != undefined && attribute != '' && attribute != null) {
        var attr = attribute;
    } else {
        var attr = 'id';
    }
    
    var obj = {};

         var images = new Array();
         $('#'+divID+' .picturepreview').each(function() {
             var imageid = $(this).attr('imageid');
             alert(imageid);
             images.push(imageid);
         });
         obj.images = images;

	 $('#'+divID+' input').each(function() {
            var name = $(this).attr(attr);
            if (name != '') {
                var fieldType = $(this).attr('type');
                if (fieldType == 'text' || fieldType == 'hidden' || fieldType == 'password') {
                    obj[name] = JSHelper.addslashes($(this).val());
                } else if (fieldType == 'checkbox') {
                    obj[name] = $(this).attr('checked');
                } else if (fieldType == 'radio') {
                    if (this.checked)
                        obj[name] = $(this).val();
                }
            }
        });
        $('#'+divID+' select').each(function() {
            var name = $(this).attr(attr);
            if (name != '') {
                var sel = this;
                if (sel != null) {
                    if (sel.selectedIndex < 0) obj[name] = 'null';else {
                        var opt = sel.options[sel.selectedIndex];
                        obj[name] = opt.value || opt.text;
                    }
                }
            }
        });
        $('#'+divID+' textarea').each(function() {
            var name = $(this).attr(attr);
            if (name != '') {
                var id = $(this).attr('id');
                var cssClass = $(this).attr('class');
                if (cssClass == 'ckeditor') {
                    obj[name] = CKHelper.getContent(id);
                } else            
                    obj[name] = $(this).val();
            }
        });
        var jsonText = JSON.stringify(obj, function (key, value) {
            if (typeof value === 'number' && !isFinite(value)) {
                return String(value);
            }
        return value;
        });

    if (escapeOutput == undefined || escapeOutput == true) {
        return escape(jsonText);
    } else {
        return jsonText;
    }
    return jsonText;
    }
}

var DOMHelper = {
    removeChilds: function(element) {
            
            if (element != undefined) {
                
                if ( element.hasChildNodes && element.hasChildNodes() )
                {
                    while ( element.childNodes.length >= 1 )
                    {
                        this.removeChilds(element.firstChild);
                    }
                } else {
                    var parent = element.parentNode;
                    if (parent != undefined) parent.removeChild(element);
                }
            }
        },
   removeNode: function(element) {
       if (element != undefined) {
           var parent = element.parentNode;
           if (parent != undefined) parent.removeChild(element);
       }
   }
}

var JSHelper = {
   addScript: function(jsname, pos) {
       var th = document.getElementsByTagName(pos)[0];
       var s = document.createElement('script');
       s.setAttribute('type','text/javascript');
       s.setAttribute('src',jsname);
       th.appendChild(s);
   },
   is_object: function (mixed_var) {
    // *     example 1: is_object('23');
    // *     returns 1: false
    // *     example 2: is_object({foo: 'bar'});
    // *     returns 2: true
    // *     example 3: is_object(null);
    // *     returns 3: false
        if (mixed_var instanceof Array) {
            return false;
        } else {
            return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
        }
   },
   strip_tags: function(str, allowed_tags) {
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
    str += '';
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
        html = matches[key].toString();
        allowed = false;
        for (k in allowed_array) {
            allowed_tag = allowed_array[k];
            i = -1;
            if (i != 0) {i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) {i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) {i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
            if (i == 0) {
                allowed = true;
                break;
            }
        }
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
    return str;
    },
    addslashes: function(str) {
        return (str+'').replace(/([\\"'])/g, "\\$1").replace(/\u0000/g, "\\0");
    },
    implode: function(glue, pieces) {
        var i = '', retVal='', tGlue='';
        if (arguments.length === 1) {pieces = glue;
            glue = '';
        }
        if (typeof(pieces) === 'object') {
            if (pieces instanceof Array) {return pieces.join(glue);
            }
            else {
                for (i in pieces) {
                    retVal += tGlue + pieces[i];tGlue = glue;
                }
                return retVal;
            }
        } else {
            return pieces;
        }
    },
    explode: function(delimiter, string, limit) {
         var emptyArray = {0: ''};

        // third argument is not required
        if ( arguments.length < 2 ||
            typeof arguments[0] == 'undefined' ||        typeof arguments[1] == 'undefined' )
        {
            return null;
        }
         if ( delimiter === '' ||
            delimiter === false ||
            delimiter === null )
        {
            return false;}

        if ( typeof delimiter == 'function' ||
            typeof delimiter == 'object' ||
            typeof string == 'function' ||        typeof string == 'object' )
        {
            return emptyArray;
        }
         if ( delimiter === true ) {
            delimiter = '1';
        }

        if (!limit) {return string.toString().split(delimiter.toString());
        } else {
            // support for limit argument
            var splitted = string.toString().split(delimiter.toString());
            var partA = splitted.splice(0, limit - 1);var partB = splitted.join(delimiter.toString());
            partA.push(partB);
            return partA;
        }
    },
    array_unique: function(inputArr) {
        var key = '', tmp_arr2 = {}, val = '';
        var __array_search = function (needle, haystack) {
            var fkey = '';
            for (fkey in haystack) {
                if (haystack.hasOwnProperty) {if ((haystack[fkey] + '') === (needle + '')) {
                        return fkey;
                    }
                }
            }return false;
        };

        for (key in inputArr) {
            if (inputArr.hasOwnProperty) {val = inputArr[key];
                if (false === __array_search(val, tmp_arr2)) {
                    tmp_arr2[key] = val;
                }
            }}

        return tmp_arr2;
    },
    array_intersect: function() {
        // Returns the entries of arr1 that have values which are present in all the other arguments
        //
        // version: 909.322
        // discuss at: http://phpjs.org/functions/array_intersect    // +   original by: Brett Zamir (http://brett-zamir.me)
        // %        note 1: These only output associative arrays (would need to be
        // %        note 1: all numeric and counting from zero to be numeric)
        // *     example 1: $array1 = {'a' : 'green', 0:'red', 1: 'blue'};
        // *     example 1: $array2 = {'b' : 'green', 0:'yellow', 1:'red'};    // *     example 1: $array3 = ['green', 'red'];
        // *     example 1: $result = array_intersect($array1, $array2, $array3);
        // *     returns 1: {0: 'red', a: 'green'}
        var arr1 = arguments[0], retArr = {};
        var k1 = '', arr = {}, i = 0, k = '';
        arr1keys:
        for (k1 in arr1) {
            arrs:
            for (i=1; i < arguments.length; i++) {arr = arguments[i];
                for (k in arr) {
                    if (arr[k] === arr1[k1]) {
                        if (i === arguments.length-1) {
                            retArr[k1] = arr1[k1];}
                        // If the innermost loop always leads at least once to an equal value, continue the loop until done
                        continue arrs;
                    }
                }            // If it reaches here, it wasn't found in at least one array, so try next value
                continue arr1keys;
            }
        }
         return retArr;
    },
    trim: function(str, chars) {
	return this.ltrim(this.rtrim(str, chars), chars);
    },
    ltrim:function(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
    },
    rtrim:function(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
    },
    str_replace: function (search, replace, subject, count) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   input by: Oleg Eremeev
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Oleg Eremeev
    // %          note 1: The count parameter must be passed as a string in order
    // %          note 1:  to find a global variable in which the result will be given
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'

    var i = 0, j = 0, temp = '', repl = '', sl = 0, fl = 0,
            f = [].concat(search),
            r = [].concat(replace),
            s = subject,
            ra = r instanceof Array, sa = s instanceof Array;
    s = [].concat(s);
    if (count) {
        this.window[count] = 0;
    }

    for (i=0, sl=s.length; i < sl; i++) {
        if (s[i] === '') {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) {
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) {
                this.window[count] += (temp.length-s[i].length)/f[j].length;}
        }
    }
    return sa ? s : s[0];
    },
    eval: function(output) {
        if (output != '')
        var json = eval('(' + output + ')');
        if (json != undefined)
            return json;
        else
            return false;
    }
}

var FormValidation = {
    clearField : true,
    email: function(id) {
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        var val = JSHelper.trim(document.getElementById(id).value);
        if (reg.test(val) == false) {
           if (this.clearField) document.getElementById(id).value = '';
           return false;
        }
        document.getElementById(id).value = val;
        return true;
    },
    string: function(id) {
        var val = document.getElementById(id).value;
        val = JSHelper.trim(JSHelper.strip_tags(val));
        if (val != '') {
            document.getElementById(id).value = val;
            return true;
        } else {
            if (this.clearField) document.getElementById(id).value = '';
            return false;
        }
    },
    real: function(id, decimals) {
      var val = document.getElementById(id).value;
      if (val != '') {
        val = val.replace(/[^\d,\.]/g, '');
        val = parseFloat(JSHelper.str_replace(',', '.', val));
        if (isNaN(val)) {
            if (this.clearField) document.getElementById(id).value = '';
            return false;
        } else {
            if (decimals == undefined)
                document.getElementById(id).value = val;
            else
                document.getElementById(id).value = val.toFixed(decimals);
            return true;
        }
      }
      return false;
    },
    integer: function(id, min, max) {
      var val = document.getElementById(id).value;
      if (val != '') {
        val = val.replace(/[^\d]/g, '');
        val = parseInt(val);
        if (isNaN(val)) {
            if (this.clearField) document.getElementById(id).value = '';
            return false;
        } else {
            if (min != undefined && max != undefined) {
                if (val >= min && val <= max) {
                    document.getElementById(id).value = val;
                    return true;
                } else {
                    if (this.clearField) document.getElementById(id).value = '';
                    return false;
                }
            } else if (min != undefined && max == undefined) {
                if (val >= min) {
                    document.getElementById(id).value = val;
                    return true;
                } else {
                    if (this.clearField) document.getElementById(id).value = '';
                    return false;
                }
            } else if (min == undefined && max != undefined) {
                if (val <= max) {
                    document.getElementById(id).value = val;
                    return true;
                } else {
                    if (this.clearField) document.getElementById(id).value = '';
                    return false;
                }
            } else {
                document.getElementById(id).value = val;
                return true;
            }
        }
      }
      return false;
    }
}

var Viewport = {
    getSizes: function() {
      var screenSize = {};
      if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        screenSize.width = window.innerWidth;
        screenSize.height = window.innerHeight;
      } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        screenSize.width = document.documentElement.clientWidth;
        screenSize.height = document.documentElement.clientHeight;
      } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        screenSize.width = document.body.clientWidth;
        screenSize.height = document.body.clientHeight;
      }
      return screenSize;
    }
}

/**
 * I18n translation class.
 *
 * @author gzagrobelny
 * @since 02.02.2010
 */
var i18n = {
    VARIANT_FE: 2,
    VARIANT_BE: 3,
    t: function(sSource, iVariant) {

        if (!this.isset(iVariant)) iVariant = this.VARIANT_FE;
            
        if (this.isdefined('i18n_translations') && this.isset(i18n_translations[sSource])) {
            return i18n_translations[sSource];
        } else {
           $.post(sRemoteServer, {
              request: 'i18n/saveJs',
              variant: iVariant,
              source: sSource
           });
           return sSource;
        }
    },
    isset: function(variable) {
       return (typeof(variable) != 'undefined');
    },
    isdefined: function(variable) {
       return (typeof(window[variable]) == "undefined")?  false: true;
    }
}

/**
 * Alias for i18n.t function with variant Front-End.
 */
function t(sWord) {
    return i18n.t(sWord);
}
/**
 * Alias for i18n.t function with variant Back-End.
 */
function ta(sWord) {
    return i18n.t(sWord, i18n.VARIANT_BE);
}

/**
 * CKEditor helper class
 * @author Grzegorz Zagrobelny
 * @since 04-02-2010
 */
var CKHelper = {
    hasInstance: function(id) {
        var instance = CKEDITOR.instances[id];
        if (instance)
            return true;
        else
            return false;
    },
    rebuild: function(id, config) {
        var instance = CKEDITOR.instances[id];
        if (instance) CKEDITOR.remove(instance);
        if (config != undefined)
            CKEDITOR.replace(id, config);
        else
            CKEDITOR.replace(id);
    },
    test: function(id) {
        var instance = CKEDITOR.instances[id];
        if (instance) instance.resetDirty();
    },
    rebuildAll: function() {
        var $editors = $("textarea.ckeditor");
        if ($editors.length) {
            $editors.each(function() {
                var editorID = $(this).attr('id');
                var instance = CKEDITOR.instances[editorID];
                if (instance) {CKEDITOR.remove(instance);}
                CKEDITOR.replace(editorID);
            });
        }
    },
    getContent: function(id) {
        var instance = CKEDITOR.instances[id];
        if (instance)
            return instance.getData();
        else
            return false;
    },
    setContent: function(id, content) {
        var instance = CKEDITOR.instances[id];
        if (instance)
            return instance.setData(content);
        else
            return false;
    }
}




