﻿var popup=null;

var main = {

    id: 0,
    account: {id:0,name:null},
    visitor: {id:0,name:null},
    focusedField: "",
    mouseX: 0,
    mouseY: 0,
    xhr: window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"),
    
    fixString: function(str) {
        var newstr = str.replace(/ /g,"[s]");
	    newstr = newstr.replace(/[\r\n]/g, "[br]");
	    newstr = newstr.replace(/&/g, "[and]");
	    newstr = newstr.replace(/%/g, "[pr]");
	    newstr = newstr.replace(/\+/g, "[pl]");
    	
	    return newstr;
    },
    
    encode_utf8: function ( s )
    {
      return unescape( encodeURIComponent( s ) );
    },

    decode_utf8: function ( s )
    {
      return decodeURIComponent( escape( s ) );
    },
    
    trimString: function(str){
	    return str.replace(/^\s+|\s+$/g, '');
    },
    
    logout: function() {
    
        var data = 'action=logout';
       
	    jQuery.post('Ajax/Login.aspx',data,function(data) {
	    
            if (data.success) {
                location.href="Start.aspx";
            }
	             
	    },"json");
       
    },
    
    showBlock: function(blockId) {
        document.getElementById(blockId).style.display='';
    },
    
    hideBlock: function(blockId) {
        document.getElementById(blockId).style.display='none';
    },
    
    //this will be overridden
    submit: null,
	
	initWysiwyg: function(){

        
		tinyMCE.init({
	        mode : "textareas",
	        theme : "advanced",
        	editor_selector : "wysiwyg",
        	
	        theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,fontselect,fontsizeselect,|,bullist,numlist,|,outdent,indent,|,link,unlink,|,justifyleft,justifycenter,justifyright",
	        theme_advanced_buttons2 : "",
	        theme_advanced_buttons3 : "",
        	
	        theme_advanced_toolbar_location : "top",
	        theme_advanced_toolbar_align : "left",
	        theme_advanced_statusbar_location : "bottom",
	        theme_advanced_resizing : true,
	        theme_advanced_resize_horizontal : false,
	        content_css : "Resources/tiny_mce.css"
	
	    });
		
		
    },
    createCookie: function(name,value,days) {
	    if (days) {
		    var date = new Date();
		    date.setTime(date.getTime()+(days*24*60*60*1000));
		    var expires = "; expires="+date.toGMTString();
	    }
	    else var expires = "";
	    document.cookie = name+"="+value+expires+"; path=/";
    },

     readCookie: function(name) {
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++) {
		    var c = ca[i];
		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
		    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
    },

    eraseCookie: function(name) {
	    createCookie(name,"",-1);
    },
    
    selectText:function(element)
    {
        var jElement = $(element);
        if(jElement.html().indexOf("input")==-1)
        {
            var text = $(element).html().trim();
            var input = $("<input/>").val(text);
            $(element).html(input);
            input.select();
        }
        else
        {
            jElement.find("input").select();
        }
        
    }
    
    

};

var dropdown = {
    timer: null,
    show: function(dropdownId, setPos, relativeTo, offsetTop, offsetLeft) {
    
        clearTimeout(document.getElementById(dropdownId).timer);

        if (relativeTo) {
            //hide again if button is moused out
            $(relativeTo).bind("mouseout", function(e) {
                dropdown.hide(dropdownId);
            });
        }

        if (setPos) {
            var top = 0;
            var left = 0;
            if (main.browser != "IE6") {
                top = $(relativeTo).offset().top + $(relativeTo).height() + 5 - $(window).scrollTop();
                left = $(relativeTo).offset().left;
            }
            else {
                top = main.mouseY - $(relativeTo).offset().top / 2 + $(window).scrollTop() - 20;
                left = main.mouseX - $(relativeTo).offset().left / 2;
            }

            if (offsetTop)
                top += offsetTop;

            if (offsetLeft)
                left += offsetLeft;

            $("#" + dropdownId).css("top", top).css("left", left).css("z-index", "200");

        }

        $("#" + dropdownId).fadeIn(200);

        return false;
    },
    hide: function(dropdownId) {
        clearTimeout(document.getElementById(dropdownId).timer);
        document.getElementById(dropdownId).timer = setTimeout(function() { $("#" + dropdownId).fadeOut(200); }, 500);
        
        $("#"+dropdownId).unbind("mouseover").unbind("mouseout");
        
        //keep showing dropdowns while mousing over them
        $("#"+dropdownId).mouseover(function(e) {
            clearTimeout(document.getElementById(dropdownId).timer);
        });
        
        //hide dropdowns when they are moused out
        $("#"+dropdownId).mouseout(function(e) {
            dropdown.hide(dropdownId);
        });
    },

    hideAll: function() {
        $(".dropdown").hide();
    },
    
    
};


//init popup grabbing
jQuery(document).ready(function() {
    $().mousemove(function(e) {
        main.mouseX = e.clientX;
        main.mouseY = e.clientY;

        if (popup && popup.visible && popup.grabbed)
            popup.move();
            
        if(typeof(itemlist)!="undefined" && itemlist.owner)
            itemlist.move();
    });

    $(window).bind("scroll", function(e) {
        dropdown.hideAll();
    });
});