var p_globals = function () {

	function getHiddenLink(link) {		
		return link.replace(/hsdf~sd34dxc~/g,"/");		
	}
	
    function newWindow(image, title) {
        var width = 200;
        var height = 200;
        var wind_img = window.open('', '_blank', 'width=' + width + ',height=' + height + ',scrollbars=no,toolbar=no');
        wind_img.focus();
        wind_img.document.open();
        wind_img.document.write('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />');
        wind_img.document.write('<title>' + title + '</title></head><body style:"padding:0 0 0 0">');
        wind_img.document.write('<a href="javascript:self.close();"><img title="' + title + '" alt="' + title + '" src=' + image + ' border="0"></a>');
        wind_img.document.write('</body></html>');
        wind_img.document.close();
    }

    function confirmToUrl(link, text) {
        if (confirm(text)) {
            window.location.href = link;
        }
    }

    function findParentById(elem, findid) {
        if (elem.parentNode != null) {
            if (elem.parentNode.id != findid)
                return findParentById(elem.parentNode, findid);
            else
                return elem.parentNode;
        }
        return null;
    }

    function closeAndRefresh() {
        self.parent.tb_remove();
        self.parent.window.location.href = self.parent.window.location.href;
    }

    function confirmToUrlAndRefresh(link, title) {
        if (confirm(title)) {
            $.get(link, null, function (data) {
                window.location.href = window.location.href;
            });
        }
    }
    
    function highlightList() {
    	$(".singleFirm").hover(function () {
    		$(this).addClass('singleFirmSelected');
    		$(".singleFirmLine",this).addClass('singleFirmLineSelected');
    		//$(this).removeClass('adNoHover');
    	}, function() {
    		$(this).removeClass('singleFirmSelected');
    		$(".singleFirmLine",this).removeClass('singleFirmLineSelected');
    		//$(this).addClass('adNoHover');
    	});     
    	$(".singlePrice").hover(function () {
    		$(this).addClass('singlePriceSelected');
    		$(".singlePriceLine",this).addClass('singlePriceLineSelected');
    		//$(this).removeClass('adNoHover');
    	}, function() {
    		$(this).removeClass('singlePriceSelected');
    		$(".singlePriceLine",this).removeClass('singlePriceLineSelected');
    		//$(this).addClass('adNoHover');
    	}); 
    	$(".singleComment").hover(function () {
    		$(this).addClass('singleCommentSelected');
    		$(".singleCommentLine",this).addClass('singleCommentLineSelected');
    		//$(this).removeClass('adNoHover');
    	}, function() {
    		$(this).removeClass('singleCommentSelected');
    		$(".singleCommentLine",this).removeClass('singleCommentLineSelected');
    		//$(this).addClass('adNoHover');
    	}); 
    	$(".addToFav").colorbox({title:"Запомнить фирму",width:"500",height:"340",initialWidth:"100",initialHeight:"100",iframe:true});
    	$(".addComment").colorbox({title:"Добавить/изменить комментарий",width:"500",height:"340",initialWidth:"100",initialHeight:"100",iframe:true});
    	$(".addRespReply").colorbox({title:"Добавить ответ на отзыв",width:"600",height:"500",initialWidth:"100",initialHeight:"100",iframe:true});
    	$(".updateResp").colorbox({title:"Отредактировать отзыв/ответ",width:"600",height:"500",initialWidth:"100",initialHeight:"100",iframe:true});
    	$(".myMenuButton").hover(function () {
    		$(this).addClass('myMenuButtonSelected');
    		//$(".singleFirmLine",this).addClass('singleFirmLineSelected');
    		//$(this).removeClass('adNoHover');
    	}, function() {
    		$(this).removeClass('myMenuButtonSelected');
    		//$(".singleFirmLine",this).removeClass('singleFirmLineSelected');
    		//$(this).addClass('adNoHover');
    	});         	
    }

    function gotoUrl(link) {
        window.location.href = link;
    }
    
    function deleteComment(id) {
    	$.post("/user/deletecomment",{id:id},function(data){
    		window.location.href=window.location.href;
    	});
    }
    
    function deleteFavorites(id) {
    	$.post("/user/deletefav",{id:id},function(data){
    		window.location.href=window.location.href;
    	});    	
    }
    
    function markResponseAsSpam(link) {
    	$.post(link,null,function (data) {
    		if (data=='ok') alert('Отзыв помечен как СПАМ!');    		
    	});
    }

	function openLoginBox(link,act) {
		link=link.replace('#','%23');
		$.fn.colorbox({open:true,href:"/user/loginbox?act="+act+"&back_link="+link,title:"Вход в личный кабинет",width:"500",height:"320",initialWidth:"100",initialHeight:"100",iframe:true});
	}

	function getLinkWithoutAnchor(link) {
		var l1=link.split("#");
		return l1[0];		
	}
    //----------------------------------------------------------------------------------------
    // Interface
    //----------------------------------------------------------------------------------------
    return {
    	getLinkWithoutAnchor:function (link) {
    		return getLinkWithoutAnchor(link);
    	},
    	
    	openLoginBox:function (link,act) {
    		return openLoginBox(link,act);
    	},
    	
    	markResponseAsSpam: function (link) {
    		return markResponseAsSpam(link);
    	},
        findParentById: function (elem, findid) {
            return findParentById(elem, findid);

        },
        highlightList: function () {
        	highlightList();
        },
        confirmToUrl: function (link, text) {
            return confirmToUrl(link, text);
        },
        confirmToUrlAndRefresh: function (link, text) {
            return confirmToUrlAndRefresh(link, text);
        },
        newWindow: function (link, title) {
            return newWindow(link, title);
        },
        gotoUrl: function (link) {
            return gotoUrl(link);
        },
        closeAndRefresh: function () {
            return closeAndRefresh();
        },
        deleteComment: function (id) {
        	return deleteComment(id);
        },
        deleteFavorites: function (id) {
        	return deleteFavorites(id);
        },
        getHiddenLink: function (link) {
        	return getHiddenLink(link);
        }
    };
} ();

